jsx-boolean-value.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * @fileoverview Enforce boolean attributes notation in JSX
  3. * @author Yannick Croissant
  4. */
  5. 'use strict';
  6. const docsUrl = require('../util/docsUrl');
  7. // ------------------------------------------------------------------------------
  8. // Rule Definition
  9. // ------------------------------------------------------------------------------
  10. const exceptionsSchema = {
  11. type: 'array',
  12. items: {type: 'string', minLength: 1},
  13. uniqueItems: true
  14. };
  15. const ALWAYS = 'always';
  16. const NEVER = 'never';
  17. const errorData = new WeakMap();
  18. function getErrorData(exceptions) {
  19. if (!errorData.has(exceptions)) {
  20. const exceptionProps = Array.from(exceptions, (name) => `\`${name}\``).join(', ');
  21. const exceptionsMessage = exceptions.size > 0 ? ` for the following props: ${exceptionProps}` : '';
  22. errorData.set(exceptions, {exceptionsMessage});
  23. }
  24. return errorData.get(exceptions);
  25. }
  26. function isAlways(configuration, exceptions, propName) {
  27. const isException = exceptions.has(propName);
  28. if (configuration === ALWAYS) {
  29. return !isException;
  30. }
  31. return isException;
  32. }
  33. function isNever(configuration, exceptions, propName) {
  34. const isException = exceptions.has(propName);
  35. if (configuration === NEVER) {
  36. return !isException;
  37. }
  38. return isException;
  39. }
  40. module.exports = {
  41. meta: {
  42. docs: {
  43. description: 'Enforce boolean attributes notation in JSX',
  44. category: 'Stylistic Issues',
  45. recommended: false,
  46. url: docsUrl('jsx-boolean-value')
  47. },
  48. fixable: 'code',
  49. messages: {
  50. omitBoolean: 'Value must be omitted for boolean attributes{{exceptionsMessage}}',
  51. omitBoolean_noMessage: 'Value must be omitted for boolean attributes',
  52. setBoolean: 'Value must be set for boolean attributes{{exceptionsMessage}}',
  53. setBoolean_noMessage: 'Value must be set for boolean attributes'
  54. },
  55. schema: {
  56. anyOf: [{
  57. type: 'array',
  58. items: [{enum: [ALWAYS, NEVER]}],
  59. additionalItems: false
  60. }, {
  61. type: 'array',
  62. items: [{
  63. enum: [ALWAYS]
  64. }, {
  65. type: 'object',
  66. additionalProperties: false,
  67. properties: {
  68. [NEVER]: exceptionsSchema
  69. }
  70. }],
  71. additionalItems: false
  72. }, {
  73. type: 'array',
  74. items: [{
  75. enum: [NEVER]
  76. }, {
  77. type: 'object',
  78. additionalProperties: false,
  79. properties: {
  80. [ALWAYS]: exceptionsSchema
  81. }
  82. }],
  83. additionalItems: false
  84. }]
  85. }
  86. },
  87. create(context) {
  88. const configuration = context.options[0] || NEVER;
  89. const configObject = context.options[1] || {};
  90. const exceptions = new Set((configuration === ALWAYS ? configObject[NEVER] : configObject[ALWAYS]) || []);
  91. return {
  92. JSXAttribute(node) {
  93. const propName = node.name && node.name.name;
  94. const value = node.value;
  95. if (isAlways(configuration, exceptions, propName) && value === null) {
  96. const data = getErrorData(exceptions);
  97. context.report({
  98. node,
  99. messageId: data.exceptionsMessage ? 'setBoolean' : 'setBoolean_noMessage',
  100. data,
  101. fix(fixer) {
  102. return fixer.insertTextAfter(node, '={true}');
  103. }
  104. });
  105. }
  106. if (isNever(configuration, exceptions, propName) && value && value.type === 'JSXExpressionContainer' && value.expression.value === true) {
  107. const data = getErrorData(exceptions);
  108. context.report({
  109. node,
  110. messageId: data.exceptionsMessage ? 'omitBoolean' : 'omitBoolean_noMessage',
  111. data,
  112. fix(fixer) {
  113. return fixer.removeRange([node.name.range[1], value.range[1]]);
  114. }
  115. });
  116. }
  117. }
  118. };
  119. }
  120. };