eslint-plugin-react-features 1.2.9 → 1.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/package.json +2 -2
  2. package/src/index.js +1 -179
  3. package/src/oxlint.js +1 -3
  4. package/src/rules/component-api/no-arbitrary-token-class.js +1 -74
  5. package/src/rules/component-api/no-default-test-id.js +1 -46
  6. package/src/rules/component-api/no-inline-style.js +1 -57
  7. package/src/rules/component-api/no-is-prefix-prop.js +1 -59
  8. package/src/rules/component-api/no-kind-prop-discriminator.js +1 -57
  9. package/src/rules/component-api/no-raw-color-literal.js +1 -55
  10. package/src/rules/component-api/no-wrapper-sub-component.js +1 -95
  11. package/src/rules/component-api/require-data-slot.js +1 -40
  12. package/src/rules/migration/react-class-to-hooks.js +1 -159
  13. package/src/rules/performance/no-unnecessary-rerenders.js +1 -185
  14. package/src/rules/performance/react-no-inline-functions.js +1 -187
  15. package/src/rules/performance/react-render-optimization.js +1 -256
  16. package/src/rules/react/checked-requires-onchange-or-readonly.js +1 -68
  17. package/src/rules/react/default-props-match-prop-types.js +1 -99
  18. package/src/rules/react/display-name.js +1 -133
  19. package/src/rules/react/hooks-exhaustive-deps.js +1 -466
  20. package/src/rules/react/jsx-handler-names.js +1 -50
  21. package/src/rules/react/jsx-key.js +1 -323
  22. package/src/rules/react/jsx-max-depth.js +1 -71
  23. package/src/rules/react/jsx-no-bind.js +1 -55
  24. package/src/rules/react/jsx-no-duplicate-props.js +1 -66
  25. package/src/rules/react/jsx-no-literals.js +1 -79
  26. package/src/rules/react/jsx-no-script-url.js +1 -119
  27. package/src/rules/react/jsx-no-target-blank.js +1 -163
  28. package/src/rules/react/no-access-state-in-setstate.js +1 -80
  29. package/src/rules/react/no-adjacent-inline-elements.js +1 -72
  30. package/src/rules/react/no-arrow-function-lifecycle.js +1 -57
  31. package/src/rules/react/no-children-prop.js +1 -54
  32. package/src/rules/react/no-danger-with-children.js +1 -99
  33. package/src/rules/react/no-danger.js +1 -43
  34. package/src/rules/react/no-deprecated.js +1 -130
  35. package/src/rules/react/no-did-mount-set-state.js +1 -50
  36. package/src/rules/react/no-did-update-set-state.js +1 -71
  37. package/src/rules/react/no-direct-mutation-state.js +3 -211
  38. package/src/rules/react/no-find-dom-node.js +1 -50
  39. package/src/rules/react/no-invalid-html-attribute.js +1 -80
  40. package/src/rules/react/no-is-mounted.js +1 -46
  41. package/src/rules/react/no-multi-comp.js +1 -150
  42. package/src/rules/react/no-namespace.js +1 -37
  43. package/src/rules/react/no-object-type-as-default-prop.js +1 -53
  44. package/src/rules/react/no-redundant-should-component-update.js +1 -50
  45. package/src/rules/react/no-render-return-value.js +1 -67
  46. package/src/rules/react/no-set-state.js +1 -45
  47. package/src/rules/react/no-string-refs.js +1 -61
  48. package/src/rules/react/no-this-in-sfc.js +1 -55
  49. package/src/rules/react/no-typos.js +1 -90
  50. package/src/rules/react/no-unescaped-entities.js +1 -44
  51. package/src/rules/react/no-unknown-property.js +1 -124
  52. package/src/rules/react/no-unsafe.js +1 -106
  53. package/src/rules/react/prefer-es6-class.js +1 -50
  54. package/src/rules/react/prefer-stateless-function.js +1 -126
  55. package/src/rules/react/prop-types.js +1 -143
  56. package/src/rules/react/react-in-jsx-scope.js +1 -86
  57. package/src/rules/react/require-default-props.js +1 -134
  58. package/src/rules/react/require-optimization.js +1 -397
  59. package/src/rules/react/require-render-return.js +1 -79
  60. package/src/rules/react/required-attributes.js +1 -185
  61. package/src/rules/react/sort-comp.js +1 -146
  62. package/src/rules/react/state-in-constructor.js +1 -95
  63. package/src/rules/react/static-property-placement.js +1 -122
  64. package/src/rules/react/void-dom-elements-no-children.js +1 -89
  65. package/src/types/index.js +1 -2
@@ -1,55 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.noRawColorLiteral = void 0;
4
- const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
- const COLOR_PATTERN = /(#[0-9a-fA-F]{3,8}\b|\b(?:rgba?|hsla?|oklch|oklab)\s*\()/;
6
- const isColorString = (s) => COLOR_PATTERN.test(s);
7
- const reportIfColorLiteral = (context, node, value) => {
8
- if (typeof value === 'string' && isColorString(value)) {
9
- context.report({ node, messageId: 'rawColor' });
10
- }
11
- };
12
- exports.noRawColorLiteral = (0, eslint_devkit_1.createRule)({
13
- name: 'no-raw-color-literal',
14
- meta: {
15
- type: 'problem',
16
- docs: {
17
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/component-api/no-raw-color-literal.md',
18
- description: 'No raw hex / rgb / hsl / oklch color literals in source — use design tokens (R19)',
19
- },
20
- schema: [],
21
- messages: {
22
- rawColor: (0, eslint_devkit_1.formatLLMMessage)({
23
- icon: eslint_devkit_1.MessageIcons.WARNING,
24
- issueName: 'Raw color literal',
25
- description: 'Raw color literal in source — use a design token (R19)',
26
- severity: 'MEDIUM',
27
- fix: 'Replace with a CSS custom property (`var(--your-token)`) or a Tailwind theme class wired to it.',
28
- documentationLink: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/component-api/no-raw-color-literal.md',
29
- }),
30
- },
31
- },
32
- defaultOptions: [],
33
- create(context) {
34
- return {
35
- 'JSXAttribute > Literal'(node) {
36
- reportIfColorLiteral(context, node, node.value);
37
- },
38
- 'JSXExpressionContainer ObjectExpression > Property > Literal'(node) {
39
- reportIfColorLiteral(context, node, node.value);
40
- },
41
- CallExpression(node) {
42
- if (node.callee.type !== 'Identifier')
43
- return;
44
- const name = node.callee.name;
45
- if (name !== 'cn' && name !== 'clsx' && name !== 'twMerge' && name !== 'classNames')
46
- return;
47
- for (const arg of node.arguments) {
48
- if (arg.type === 'Literal') {
49
- reportIfColorLiteral(context, arg, arg.value);
50
- }
51
- }
52
- },
53
- };
54
- },
55
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noRawColorLiteral=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const COLOR_PATTERN=/(#[0-9a-fA-F]{3,8}\b|\b(?:rgba?|hsla?|oklch|oklab)\s*\()/;const isColorString=s=>COLOR_PATTERN.test(s);const reportIfColorLiteral=(context,node,value)=>{if(typeof value==="string"&&isColorString(value)){context.report({node,messageId:"rawColor"})}};exports.noRawColorLiteral=(0,eslint_devkit_1.createRule)({name:"no-raw-color-literal",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/component-api/no-raw-color-literal.md",description:"No raw hex / rgb / hsl / oklch color literals in source \u2014 use design tokens (R19)"},schema:[],messages:{rawColor:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.WARNING,issueName:"Raw color literal",description:"Raw color literal in source \u2014 use a design token (R19)",severity:"MEDIUM",fix:"Replace with a CSS custom property (`var(--your-token)`) or a Tailwind theme class wired to it.",documentationLink:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/component-api/no-raw-color-literal.md"})}},defaultOptions:[],create(context){return{"JSXAttribute > Literal"(node){reportIfColorLiteral(context,node,node.value)},"JSXExpressionContainer ObjectExpression > Property > Literal"(node){reportIfColorLiteral(context,node,node.value)},CallExpression(node){if(node.callee.type!=="Identifier")return;const name=node.callee.name;if(name!=="cn"&&name!=="clsx"&&name!=="twMerge"&&name!=="classNames")return;for(const arg of node.arguments){if(arg.type==="Literal"){reportIfColorLiteral(context,arg,arg.value)}}}}}});
@@ -1,95 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.noWrapperSubComponent = void 0;
4
- const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
- const isPassthroughBody = (body) => {
6
- if (body.type === 'JSXElement')
7
- return body;
8
- if (body.type !== 'BlockStatement')
9
- return null;
10
- if (body.body.length !== 1)
11
- return null;
12
- const stmt = body.body[0];
13
- if (stmt.type !== 'ReturnStatement')
14
- return null;
15
- if (!stmt.argument || stmt.argument.type !== 'JSXElement')
16
- return null;
17
- return stmt.argument;
18
- };
19
- const isSpreadOnlyJsx = (el) => {
20
- const attrs = el.openingElement.attributes;
21
- if (attrs.length !== 1)
22
- return false;
23
- const attr = attrs[0];
24
- if (attr.type !== 'JSXSpreadAttribute')
25
- return false;
26
- if (attr.argument.type !== 'Identifier')
27
- return false;
28
- return el.children.length === 0;
29
- };
30
- const isPascalCaseIdentifier = (s) => /^[A-Z]/.test(s);
31
- const isPascalCaseJsxName = (name) => {
32
- if (name.type === 'JSXIdentifier')
33
- return isPascalCaseIdentifier(name.name);
34
- if (name.type === 'JSXMemberExpression')
35
- return true;
36
- return false;
37
- };
38
- exports.noWrapperSubComponent = (0, eslint_devkit_1.createRule)({
39
- name: 'no-wrapper-sub-component',
40
- meta: {
41
- type: 'problem',
42
- docs: {
43
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/component-api/no-wrapper-sub-component.md',
44
- description: 'Ban pure-passthrough wrapper sub-components — slot the primitive directly (R12)',
45
- },
46
- schema: [],
47
- messages: {
48
- wrapperSubComponent: (0, eslint_devkit_1.formatLLMMessage)({
49
- icon: eslint_devkit_1.MessageIcons.WARNING,
50
- issueName: 'Passthrough wrapper component',
51
- description: 'Component `{{name}}` is a pure passthrough wrapper — slot the primitive directly (R12)',
52
- severity: 'MEDIUM',
53
- fix: 'Delete the wrapper and let consumers import the primitive. If the wrapper must exist, give it structural behavior (a slot, a default prop, or composition logic).',
54
- documentationLink: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/component-api/no-wrapper-sub-component.md',
55
- }),
56
- },
57
- },
58
- defaultOptions: [],
59
- create(context) {
60
- const checkFn = (node, name) => {
61
- if (!isPascalCaseIdentifier(name))
62
- return;
63
- const jsx = isPassthroughBody(node.body);
64
- if (!jsx)
65
- return;
66
- if (!isSpreadOnlyJsx(jsx))
67
- return;
68
- if (!isPascalCaseJsxName(jsx.openingElement.name))
69
- return;
70
- context.report({
71
- node,
72
- messageId: 'wrapperSubComponent',
73
- data: { name },
74
- });
75
- };
76
- return {
77
- FunctionDeclaration(node) {
78
- if (!node.id)
79
- return;
80
- checkFn(node, node.id.name);
81
- },
82
- VariableDeclarator(node) {
83
- if (node.id.type !== 'Identifier')
84
- return;
85
- if (!node.init)
86
- return;
87
- if (node.init.type !== 'ArrowFunctionExpression' &&
88
- node.init.type !== 'FunctionExpression') {
89
- return;
90
- }
91
- checkFn(node.init, node.id.name);
92
- },
93
- };
94
- },
95
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noWrapperSubComponent=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const isPassthroughBody=body=>{if(body.type==="JSXElement")return body;if(body.type!=="BlockStatement")return null;if(body.body.length!==1)return null;const stmt=body.body[0];if(stmt.type!=="ReturnStatement")return null;if(!stmt.argument||stmt.argument.type!=="JSXElement")return null;return stmt.argument};const isSpreadOnlyJsx=el=>{const attrs=el.openingElement.attributes;if(attrs.length!==1)return false;const attr=attrs[0];if(attr.type!=="JSXSpreadAttribute")return false;if(attr.argument.type!=="Identifier")return false;return el.children.length===0};const isPascalCaseIdentifier=s=>/^[A-Z]/.test(s);const isPascalCaseJsxName=name=>{if(name.type==="JSXIdentifier")return isPascalCaseIdentifier(name.name);if(name.type==="JSXMemberExpression")return true;return false};exports.noWrapperSubComponent=(0,eslint_devkit_1.createRule)({name:"no-wrapper-sub-component",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/component-api/no-wrapper-sub-component.md",description:"Ban pure-passthrough wrapper sub-components \u2014 slot the primitive directly (R12)"},schema:[],messages:{wrapperSubComponent:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.WARNING,issueName:"Passthrough wrapper component",description:"Component `{{name}}` is a pure passthrough wrapper \u2014 slot the primitive directly (R12)",severity:"MEDIUM",fix:"Delete the wrapper and let consumers import the primitive. If the wrapper must exist, give it structural behavior (a slot, a default prop, or composition logic).",documentationLink:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/component-api/no-wrapper-sub-component.md"})}},defaultOptions:[],create(context){const checkFn=(node,name)=>{if(!isPascalCaseIdentifier(name))return;const jsx=isPassthroughBody(node.body);if(!jsx)return;if(!isSpreadOnlyJsx(jsx))return;if(!isPascalCaseJsxName(jsx.openingElement.name))return;context.report({node,messageId:"wrapperSubComponent",data:{name}})};return{FunctionDeclaration(node){if(!node.id)return;checkFn(node,node.id.name)},VariableDeclarator(node){if(node.id.type!=="Identifier")return;if(!node.init)return;if(node.init.type!=="ArrowFunctionExpression"&&node.init.type!=="FunctionExpression"){return}checkFn(node.init,node.id.name)}}}});
@@ -1,40 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.requireDataSlot = void 0;
4
- const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
- const hasNamedAttribute = (node, name) => node.attributes.some((attr) => attr.type === 'JSXAttribute' &&
6
- attr.name.type === 'JSXIdentifier' &&
7
- attr.name.name === name);
8
- exports.requireDataSlot = (0, eslint_devkit_1.createRule)({
9
- name: 'require-data-slot',
10
- meta: {
11
- type: 'problem',
12
- docs: {
13
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/component-api/require-data-slot.md',
14
- description: 'Named composition parts must carry data-slot (R6)',
15
- },
16
- schema: [],
17
- messages: {
18
- missingDataSlot: (0, eslint_devkit_1.formatLLMMessage)({
19
- icon: eslint_devkit_1.MessageIcons.WARNING,
20
- issueName: 'Missing data-slot',
21
- description: 'JSX element has data-testid but no data-slot (R6)',
22
- severity: 'MEDIUM',
23
- fix: 'Add `data-slot="<part-name>"` so consumers can style by slot.',
24
- documentationLink: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/component-api/require-data-slot.md',
25
- }),
26
- },
27
- },
28
- defaultOptions: [],
29
- create(context) {
30
- return {
31
- JSXOpeningElement(node) {
32
- if (!hasNamedAttribute(node, 'data-testid'))
33
- return;
34
- if (hasNamedAttribute(node, 'data-slot'))
35
- return;
36
- context.report({ node, messageId: 'missingDataSlot' });
37
- },
38
- };
39
- },
40
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.requireDataSlot=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const hasNamedAttribute=(node,name)=>node.attributes.some(attr=>attr.type==="JSXAttribute"&&attr.name.type==="JSXIdentifier"&&attr.name.name===name);exports.requireDataSlot=(0,eslint_devkit_1.createRule)({name:"require-data-slot",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/component-api/require-data-slot.md",description:"Named composition parts must carry data-slot (R6)"},schema:[],messages:{missingDataSlot:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.WARNING,issueName:"Missing data-slot",description:"JSX element has data-testid but no data-slot (R6)",severity:"MEDIUM",fix:'Add `data-slot="<part-name>"` so consumers can style by slot.',documentationLink:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/component-api/require-data-slot.md"})}},defaultOptions:[],create(context){return{JSXOpeningElement(node){if(!hasNamedAttribute(node,"data-testid"))return;if(hasNamedAttribute(node,"data-slot"))return;context.report({node,messageId:"missingDataSlot"})}}}});
@@ -1,159 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.reactClassToHooks = void 0;
4
- const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
- const eslint_devkit_2 = require("@interlace/eslint-devkit");
6
- exports.reactClassToHooks = (0, eslint_devkit_2.createRule)({
7
- name: 'react-class-to-hooks',
8
- meta: {
9
- type: 'suggestion',
10
- docs: {
11
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/react-class-to-hooks.md',
12
- description: 'Suggest migrating React class components to hooks with detailed migration path',
13
- cwe: 'CWE-1078',
14
- cvss: 5,
15
- },
16
- fixable: 'code',
17
- hasSuggestions: true,
18
- messages: {
19
- migrateToHooks: (0, eslint_devkit_1.formatLLMMessage)({
20
- icon: eslint_devkit_1.MessageIcons.MIGRATION,
21
- issueName: 'React class component',
22
- cwe: 'CWE-1078',
23
- description: 'React class component detected',
24
- severity: 'MEDIUM',
25
- fix: 'Use functional component with useEffect/useState (Complexity: {{complexity}})',
26
- documentationLink: 'https://react.dev/reference/react/hooks',
27
- }),
28
- convertToFunction: (0, eslint_devkit_1.formatLLMMessage)({
29
- icon: eslint_devkit_1.MessageIcons.INFO,
30
- issueName: 'Convert to Functional',
31
- description: 'Convert to functional component with hooks',
32
- severity: 'LOW',
33
- fix: 'function Component() { const [state, setState] = useState() }',
34
- documentationLink: 'https://react.dev/reference/react/hooks',
35
- }),
36
- viewMigrationGuide: (0, eslint_devkit_1.formatLLMMessage)({
37
- icon: eslint_devkit_1.MessageIcons.INFO,
38
- issueName: 'Migration Guide',
39
- description: 'View detailed migration guide',
40
- severity: 'LOW',
41
- fix: 'Follow React hooks migration guide',
42
- documentationLink: 'https://react.dev/reference/react/hooks',
43
- }),
44
- },
45
- schema: [
46
- {
47
- type: 'object',
48
- properties: {
49
- ignorePureRenderComponents: {
50
- type: 'boolean',
51
- default: false,
52
- },
53
- allowComplexLifecycle: {
54
- type: 'boolean',
55
- default: false,
56
- },
57
- },
58
- additionalProperties: false,
59
- },
60
- ],
61
- },
62
- defaultOptions: [
63
- {
64
- ignorePureRenderComponents: false,
65
- allowComplexLifecycle: false,
66
- },
67
- ],
68
- create(context) {
69
- const options = context.options[0] || {};
70
- const { allowComplexLifecycle = false } = options;
71
- const sourceCode = context.sourceCode;
72
- const isReactComponent = (node) => {
73
- if (!node.superClass)
74
- return false;
75
- if (node.superClass.type === 'Identifier') {
76
- return (node.superClass.name === 'Component' ||
77
- node.superClass.name === 'PureComponent');
78
- }
79
- if (node.superClass.type === 'MemberExpression') {
80
- if (node.superClass.object.type === 'Identifier' &&
81
- node.superClass.object.name === 'React' &&
82
- node.superClass.property.type === 'Identifier') {
83
- return (node.superClass.property.name === 'Component' ||
84
- node.superClass.property.name === 'PureComponent');
85
- }
86
- }
87
- return false;
88
- };
89
- const analyzeLifecycleMethods = (node) => {
90
- const lifecycleMethods = [];
91
- const classBody = node.body.body;
92
- const lifecycleMap = {
93
- componentDidMount: 'useEffect',
94
- componentDidUpdate: 'useEffect',
95
- componentWillUnmount: 'useEffect cleanup',
96
- shouldComponentUpdate: 'React.memo',
97
- getDerivedStateFromProps: 'useState + useEffect',
98
- getSnapshotBeforeUpdate: 'useEffect',
99
- };
100
- for (const member of classBody) {
101
- if (member.type === 'MethodDefinition' &&
102
- member.key.type === 'Identifier' &&
103
- lifecycleMap[member.key.name]) {
104
- lifecycleMethods.push(member.key.name);
105
- }
106
- }
107
- const suggestedHooks = [
108
- ...new Set(lifecycleMethods.map((m) => lifecycleMap[m])),
109
- ];
110
- let complexity = 'simple';
111
- if (lifecycleMethods.length > 2)
112
- complexity = 'medium';
113
- if (lifecycleMethods.includes('getDerivedStateFromProps') ||
114
- lifecycleMethods.includes('getSnapshotBeforeUpdate')) {
115
- complexity = 'complex';
116
- }
117
- return { methods: lifecycleMethods, suggestedHooks, complexity };
118
- };
119
- return {
120
- ClassDeclaration(node) {
121
- if (!isReactComponent(node))
122
- return;
123
- if (!node.id)
124
- return;
125
- const componentName = node.id.name;
126
- const { complexity, methods: lifecycleMethods } = analyzeLifecycleMethods(node);
127
- const hasRenderMethod = node.body.body.some((member) => member.type === 'MethodDefinition' &&
128
- member.key.type === 'Identifier' &&
129
- member.key.name === 'render');
130
- if (complexity === 'complex' && allowComplexLifecycle) {
131
- return;
132
- }
133
- context.report({
134
- node,
135
- messageId: 'migrateToHooks',
136
- data: {
137
- componentName,
138
- complexity,
139
- },
140
- ...(complexity === 'simple' && lifecycleMethods.length === 0 && !hasRenderMethod ? {
141
- suggest: [
142
- {
143
- messageId: 'convertToFunction',
144
- fix: (fixer) => {
145
- const classText = sourceCode.getText(node);
146
- const funcText = classText
147
- .replace(/class\s+(\w+)\s+extends\s+(\w+\.)?\w+/, 'function $1(props)')
148
- .replace(/this\.props\./g, 'props.')
149
- .replace(/this\.state\.(\w+)/g, (_, name) => name);
150
- return fixer.replaceText(node, funcText);
151
- },
152
- }
153
- ]
154
- } : {}),
155
- });
156
- },
157
- };
158
- },
159
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.reactClassToHooks=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");exports.reactClassToHooks=(0,eslint_devkit_2.createRule)({name:"react-class-to-hooks",meta:{type:"suggestion",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/react-class-to-hooks.md",description:"Suggest migrating React class components to hooks with detailed migration path",cwe:"CWE-1078",cvss:5},fixable:"code",hasSuggestions:true,messages:{migrateToHooks:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.MIGRATION,issueName:"React class component",cwe:"CWE-1078",description:"React class component detected",severity:"MEDIUM",fix:"Use functional component with useEffect/useState (Complexity: {{complexity}})",documentationLink:"https://react.dev/reference/react/hooks"}),convertToFunction:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Convert to Functional",description:"Convert to functional component with hooks",severity:"LOW",fix:"function Component() { const [state, setState] = useState() }",documentationLink:"https://react.dev/reference/react/hooks"}),viewMigrationGuide:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Migration Guide",description:"View detailed migration guide",severity:"LOW",fix:"Follow React hooks migration guide",documentationLink:"https://react.dev/reference/react/hooks"})},schema:[{type:"object",properties:{ignorePureRenderComponents:{type:"boolean",default:false},allowComplexLifecycle:{type:"boolean",default:false}},additionalProperties:false}]},defaultOptions:[{ignorePureRenderComponents:false,allowComplexLifecycle:false}],create(context){const options=context.options[0]||{};const{allowComplexLifecycle=false}=options;const sourceCode=context.sourceCode;const isReactComponent=node=>{if(!node.superClass)return false;if(node.superClass.type==="Identifier"){return node.superClass.name==="Component"||node.superClass.name==="PureComponent"}if(node.superClass.type==="MemberExpression"){if(node.superClass.object.type==="Identifier"&&node.superClass.object.name==="React"&&node.superClass.property.type==="Identifier"){return node.superClass.property.name==="Component"||node.superClass.property.name==="PureComponent"}}return false};const analyzeLifecycleMethods=node=>{const lifecycleMethods=[];const classBody=node.body.body;const lifecycleMap={componentDidMount:"useEffect",componentDidUpdate:"useEffect",componentWillUnmount:"useEffect cleanup",shouldComponentUpdate:"React.memo",getDerivedStateFromProps:"useState + useEffect",getSnapshotBeforeUpdate:"useEffect"};for(const member of classBody){if(member.type==="MethodDefinition"&&member.key.type==="Identifier"&&lifecycleMap[member.key.name]){lifecycleMethods.push(member.key.name)}}const suggestedHooks=[...new Set(lifecycleMethods.map(m=>lifecycleMap[m]))];let complexity="simple";if(lifecycleMethods.length>2)complexity="medium";if(lifecycleMethods.includes("getDerivedStateFromProps")||lifecycleMethods.includes("getSnapshotBeforeUpdate")){complexity="complex"}return{methods:lifecycleMethods,suggestedHooks,complexity}};return{ClassDeclaration(node){if(!isReactComponent(node))return;if(!node.id)return;const componentName=node.id.name;const{complexity,methods:lifecycleMethods}=analyzeLifecycleMethods(node);const hasRenderMethod=node.body.body.some(member=>member.type==="MethodDefinition"&&member.key.type==="Identifier"&&member.key.name==="render");if(complexity==="complex"&&allowComplexLifecycle){return}context.report({node,messageId:"migrateToHooks",data:{componentName,complexity},...complexity==="simple"&&lifecycleMethods.length===0&&!hasRenderMethod?{suggest:[{messageId:"convertToFunction",fix:fixer=>{const classText=sourceCode.getText(node);const funcText=classText.replace(/class\s+(\w+)\s+extends\s+(\w+\.)?\w+/,"function $1(props)").replace(/this\.props\./g,"props.").replace(/this\.state\.(\w+)/g,(_,name)=>name);return fixer.replaceText(node,funcText)}}]}:{}})}}}});
@@ -1,185 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.noUnnecessaryRerenders = void 0;
4
- const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
- const eslint_devkit_2 = require("@interlace/eslint-devkit");
6
- function isInReactRenderContext(node) {
7
- let current = node;
8
- let depth = 0;
9
- const maxDepth = 15;
10
- while (depth < maxDepth) {
11
- const parent = current.parent;
12
- if (!parent)
13
- break;
14
- if (parent.type === 'JSXElement' || parent.type === 'JSXFragment') {
15
- return true;
16
- }
17
- if (parent.type === 'JSXAttribute') {
18
- return true;
19
- }
20
- if (parent.type === 'FunctionDeclaration' || parent.type === 'FunctionExpression' ||
21
- parent.type === 'ArrowFunctionExpression') {
22
- if (parent.type === 'FunctionDeclaration' && parent.id) {
23
- const name = parent.id.name;
24
- if (name && /^[A-Z]/.test(name)) {
25
- return true;
26
- }
27
- }
28
- const body = parent.type === 'FunctionDeclaration' ? parent.body :
29
- parent.type === 'FunctionExpression' ? parent.body :
30
- parent.body;
31
- if (body && body.type === 'BlockStatement') {
32
- const hasJSX = body.body.some((stmt) => stmt.type === 'ReturnStatement' &&
33
- stmt.argument &&
34
- (stmt.argument.type === 'JSXElement' || stmt.argument.type === 'JSXFragment'));
35
- if (hasJSX) {
36
- return true;
37
- }
38
- }
39
- }
40
- current = parent;
41
- depth++;
42
- }
43
- return false;
44
- }
45
- function shouldBeMemoized(node, minSize = 5) {
46
- if (node.type === 'ObjectExpression') {
47
- const properties = node.properties.length;
48
- return properties >= minSize;
49
- }
50
- if (node.type === 'ArrayExpression') {
51
- const elements = node.elements.filter((el) => el !== null).length;
52
- return elements >= minSize;
53
- }
54
- if (node.type === 'ArrowFunctionExpression' || node.type === 'FunctionExpression') {
55
- return true;
56
- }
57
- return false;
58
- }
59
- exports.noUnnecessaryRerenders = (0, eslint_devkit_2.createRule)({
60
- name: 'no-unnecessary-rerenders',
61
- meta: {
62
- type: 'suggestion',
63
- docs: {
64
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/no-unnecessary-rerenders.md',
65
- description: 'Detects prevented re-renders in React',
66
- },
67
- hasSuggestions: true,
68
- messages: {
69
- unnecessaryRerender: (0, eslint_devkit_1.formatLLMMessage)({
70
- icon: eslint_devkit_1.MessageIcons.PERFORMANCE,
71
- issueName: 'Unnecessary re-render',
72
- description: '{{expression}} causes unnecessary re-renders',
73
- severity: 'MEDIUM',
74
- fix: 'Use useMemo or useCallback to memoize {{expression}}',
75
- documentationLink: 'https://react.dev/reference/react/useMemo',
76
- }),
77
- useMemo: (0, eslint_devkit_1.formatLLMMessage)({
78
- icon: eslint_devkit_1.MessageIcons.INFO,
79
- issueName: 'Use useMemo',
80
- description: 'Memoize with useMemo',
81
- severity: 'LOW',
82
- fix: 'const memoized = useMemo(() => value, [deps])',
83
- documentationLink: 'https://react.dev/reference/react/useMemo',
84
- }),
85
- useCallback: (0, eslint_devkit_1.formatLLMMessage)({
86
- icon: eslint_devkit_1.MessageIcons.INFO,
87
- issueName: 'Use useCallback',
88
- description: 'Memoize callback with useCallback',
89
- severity: 'LOW',
90
- fix: 'const handler = useCallback(() => {}, [deps])',
91
- documentationLink: 'https://react.dev/reference/react/useCallback',
92
- }),
93
- extractToVariable: (0, eslint_devkit_1.formatLLMMessage)({
94
- icon: eslint_devkit_1.MessageIcons.INFO,
95
- issueName: 'Extract Variable',
96
- description: 'Extract to variable outside render',
97
- severity: 'LOW',
98
- fix: 'Define outside component or use useMemo',
99
- documentationLink: 'https://react.dev/learn/render-and-commit',
100
- }),
101
- },
102
- schema: [
103
- {
104
- type: 'object',
105
- properties: {
106
- ignoreInTests: {
107
- type: 'boolean',
108
- default: true,
109
- },
110
- minSize: {
111
- type: 'number',
112
- default: 5,
113
- minimum: 1,
114
- },
115
- },
116
- additionalProperties: false,
117
- },
118
- ],
119
- },
120
- defaultOptions: [
121
- {
122
- ignoreInTests: true,
123
- minSize: 5,
124
- },
125
- ],
126
- create(context, [options = {}]) {
127
- const { ignoreInTests = true, minSize = 5 } = options || {};
128
- const filename = context.filename;
129
- const isTestFile = ignoreInTests && /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
130
- if (isTestFile) {
131
- return {};
132
- }
133
- const sourceCode = context.sourceCode;
134
- function checkJSXAttribute(node) {
135
- try {
136
- if (!node.value) {
137
- return;
138
- }
139
- const value = node.value.type === 'JSXExpressionContainer'
140
- ? node.value.expression
141
- : null;
142
- if (!value) {
143
- return;
144
- }
145
- if (!isInReactRenderContext(value)) {
146
- return;
147
- }
148
- if (shouldBeMemoized(value, minSize)) {
149
- let expressionText = '';
150
- try {
151
- expressionText = sourceCode.getText(value).substring(0, 50);
152
- }
153
- catch {
154
- expressionText = 'expression';
155
- }
156
- const isFunction = value.type === 'ArrowFunctionExpression' || value.type === 'FunctionExpression';
157
- const firstSuggestionMessageId = isFunction ? 'useCallback' : 'useMemo';
158
- context.report({
159
- node: value,
160
- messageId: 'unnecessaryRerender',
161
- data: {
162
- expression: expressionText,
163
- },
164
- suggest: [
165
- {
166
- messageId: firstSuggestionMessageId,
167
- fix: () => null,
168
- },
169
- {
170
- messageId: 'extractToVariable',
171
- fix: () => null,
172
- },
173
- ],
174
- });
175
- }
176
- }
177
- catch {
178
- return;
179
- }
180
- }
181
- return {
182
- JSXAttribute: checkJSXAttribute,
183
- };
184
- },
185
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noUnnecessaryRerenders=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");function isInReactRenderContext(node){let current=node;let depth=0;const maxDepth=15;while(depth<maxDepth){const parent=current.parent;if(!parent)break;if(parent.type==="JSXElement"||parent.type==="JSXFragment"){return true}if(parent.type==="JSXAttribute"){return true}if(parent.type==="FunctionDeclaration"||parent.type==="FunctionExpression"||parent.type==="ArrowFunctionExpression"){if(parent.type==="FunctionDeclaration"&&parent.id){const name=parent.id.name;if(name&&/^[A-Z]/.test(name)){return true}}const body=parent.type==="FunctionDeclaration"?parent.body:parent.type==="FunctionExpression"?parent.body:parent.body;if(body&&body.type==="BlockStatement"){const hasJSX=body.body.some(stmt=>stmt.type==="ReturnStatement"&&stmt.argument&&(stmt.argument.type==="JSXElement"||stmt.argument.type==="JSXFragment"));if(hasJSX){return true}}}current=parent;depth++}return false}function shouldBeMemoized(node,minSize=5){if(node.type==="ObjectExpression"){const properties=node.properties.length;return properties>=minSize}if(node.type==="ArrayExpression"){const elements=node.elements.filter(el=>el!==null).length;return elements>=minSize}if(node.type==="ArrowFunctionExpression"||node.type==="FunctionExpression"){return true}return false}exports.noUnnecessaryRerenders=(0,eslint_devkit_2.createRule)({name:"no-unnecessary-rerenders",meta:{type:"suggestion",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/no-unnecessary-rerenders.md",description:"Detects prevented re-renders in React"},hasSuggestions:true,messages:{unnecessaryRerender:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.PERFORMANCE,issueName:"Unnecessary re-render",description:"{{expression}} causes unnecessary re-renders",severity:"MEDIUM",fix:"Use useMemo or useCallback to memoize {{expression}}",documentationLink:"https://react.dev/reference/react/useMemo"}),useMemo:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Use useMemo",description:"Memoize with useMemo",severity:"LOW",fix:"const memoized = useMemo(() => value, [deps])",documentationLink:"https://react.dev/reference/react/useMemo"}),useCallback:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Use useCallback",description:"Memoize callback with useCallback",severity:"LOW",fix:"const handler = useCallback(() => {}, [deps])",documentationLink:"https://react.dev/reference/react/useCallback"}),extractToVariable:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Extract Variable",description:"Extract to variable outside render",severity:"LOW",fix:"Define outside component or use useMemo",documentationLink:"https://react.dev/learn/render-and-commit"})},schema:[{type:"object",properties:{ignoreInTests:{type:"boolean",default:true},minSize:{type:"number",default:5,minimum:1}},additionalProperties:false}]},defaultOptions:[{ignoreInTests:true,minSize:5}],create(context,[options={}]){const{ignoreInTests=true,minSize=5}=options||{};const filename=context.filename;const isTestFile=ignoreInTests&&/\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);if(isTestFile){return{}}const sourceCode=context.sourceCode;function checkJSXAttribute(node){try{if(!node.value){return}const value=node.value.type==="JSXExpressionContainer"?node.value.expression:null;if(!value){return}if(!isInReactRenderContext(value)){return}if(shouldBeMemoized(value,minSize)){let expressionText="";try{expressionText=sourceCode.getText(value).substring(0,50)}catch{expressionText="expression"}const isFunction=value.type==="ArrowFunctionExpression"||value.type==="FunctionExpression";const firstSuggestionMessageId=isFunction?"useCallback":"useMemo";context.report({node:value,messageId:"unnecessaryRerender",data:{expression:expressionText},suggest:[{messageId:firstSuggestionMessageId,fix:()=>null},{messageId:"extractToVariable",fix:()=>null}]})}}catch{return}}return{JSXAttribute:checkJSXAttribute}}});