index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 'use strict';
  2. const fromEntries = require('object.fromentries');
  3. const entries = require('object.entries');
  4. const allRules = require('./lib/rules');
  5. function filterRules(rules, predicate) {
  6. return fromEntries(entries(rules).filter((entry) => predicate(entry[1])));
  7. }
  8. /**
  9. * @param {object} rules - rules object mapping rule name to rule module
  10. * @returns {Record<string, SEVERITY_ERROR | 'error'>}
  11. */
  12. function configureAsError(rules) {
  13. return fromEntries(Object.keys(rules).map((key) => [`react/${key}`, 2]));
  14. }
  15. /** @type {Partial<typeof allRules>} */
  16. const activeRules = filterRules(allRules, (rule) => !rule.meta.deprecated);
  17. /** @type {Record<keyof typeof activeRules, 2 | 'error'>} */
  18. const activeRulesConfig = configureAsError(activeRules);
  19. /** @type {Partial<typeof allRules>} */
  20. const deprecatedRules = filterRules(allRules, (rule) => rule.meta.deprecated);
  21. /** @type {['react']} */
  22. // for legacy config system
  23. const plugins = [
  24. 'react',
  25. ];
  26. // TODO: with TS 4.5+, inline this
  27. const SEVERITY_ERROR = /** @type {2} */ (2);
  28. const SEVERITY_OFF = /** @type {0} */ (0);
  29. const configs = {
  30. recommended: {
  31. plugins,
  32. parserOptions: {
  33. ecmaFeatures: {
  34. jsx: true,
  35. },
  36. },
  37. rules: {
  38. 'react/display-name': SEVERITY_ERROR,
  39. 'react/jsx-key': SEVERITY_ERROR,
  40. 'react/jsx-no-comment-textnodes': SEVERITY_ERROR,
  41. 'react/jsx-no-duplicate-props': SEVERITY_ERROR,
  42. 'react/jsx-no-target-blank': SEVERITY_ERROR,
  43. 'react/jsx-no-undef': SEVERITY_ERROR,
  44. 'react/jsx-uses-react': SEVERITY_ERROR,
  45. 'react/jsx-uses-vars': SEVERITY_ERROR,
  46. 'react/no-children-prop': SEVERITY_ERROR,
  47. 'react/no-danger-with-children': SEVERITY_ERROR,
  48. 'react/no-deprecated': SEVERITY_ERROR,
  49. 'react/no-direct-mutation-state': SEVERITY_ERROR,
  50. 'react/no-find-dom-node': SEVERITY_ERROR,
  51. 'react/no-is-mounted': SEVERITY_ERROR,
  52. 'react/no-render-return-value': SEVERITY_ERROR,
  53. 'react/no-string-refs': SEVERITY_ERROR,
  54. 'react/no-unescaped-entities': SEVERITY_ERROR,
  55. 'react/no-unknown-property': SEVERITY_ERROR,
  56. 'react/no-unsafe': SEVERITY_OFF,
  57. 'react/prop-types': SEVERITY_ERROR,
  58. 'react/react-in-jsx-scope': SEVERITY_ERROR,
  59. 'react/require-render-return': SEVERITY_ERROR,
  60. },
  61. },
  62. all: {
  63. plugins,
  64. parserOptions: {
  65. ecmaFeatures: {
  66. jsx: true,
  67. },
  68. },
  69. rules: activeRulesConfig,
  70. },
  71. 'jsx-runtime': {
  72. plugins,
  73. parserOptions: {
  74. ecmaFeatures: {
  75. jsx: true,
  76. },
  77. jsxPragma: null, // for @typescript/eslint-parser
  78. },
  79. rules: {
  80. 'react/react-in-jsx-scope': SEVERITY_OFF,
  81. 'react/jsx-uses-react': SEVERITY_OFF,
  82. },
  83. },
  84. flat: /** @type {Record<string, ReactFlatConfig>} */ ({
  85. __proto__: null,
  86. }),
  87. };
  88. /** @typedef {{ plugins: { react: typeof plugin }, rules: import('eslint').Linter.RulesRecord, languageOptions: { parserOptions: import('eslint').Linter.ParserOptions } }} ReactFlatConfig */
  89. /** @type {{ deprecatedRules: typeof deprecatedRules, rules: typeof allRules, configs: typeof configs & { flat: Record<string, ReactFlatConfig> }}} */
  90. const plugin = {
  91. deprecatedRules,
  92. rules: allRules,
  93. configs,
  94. };
  95. Object.assign(configs.flat, {
  96. recommended: {
  97. plugins: { react: plugin },
  98. rules: configs.recommended.rules,
  99. languageOptions: { parserOptions: configs.recommended.parserOptions },
  100. },
  101. all: {
  102. plugins: { react: plugin },
  103. rules: configs.all.rules,
  104. languageOptions: { parserOptions: configs.all.parserOptions },
  105. },
  106. 'jsx-runtime': {
  107. plugins: { react: plugin },
  108. rules: configs['jsx-runtime'].rules,
  109. languageOptions: { parserOptions: configs['jsx-runtime'].parserOptions },
  110. },
  111. });
  112. module.exports = plugin;