eslint-plugin-react-features 1.2.8 → 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 -3
  2. package/src/index.js +1 -239
  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,86 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.reactInJsxScope = void 0;
4
- const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
- const eslint_devkit_2 = require("@interlace/eslint-devkit");
6
- exports.reactInJsxScope = (0, eslint_devkit_1.createRule)({
7
- name: 'react-in-jsx-scope',
8
- meta: {
9
- type: 'problem',
10
- docs: {
11
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/react-in-jsx-scope.md',
12
- description: 'Ensure React is in scope',
13
- },
14
- schema: [],
15
- messages: {
16
- reactInJsxScope: (0, eslint_devkit_2.formatLLMMessage)({
17
- icon: eslint_devkit_2.MessageIcons.WARNING,
18
- issueName: 'React Not in Scope',
19
- description: 'React must be in scope when using JSX',
20
- severity: 'CRITICAL',
21
- fix: 'Add import React from "react" at the top of the file',
22
- documentationLink: 'https://react.dev/learn/add-react-to-a-website#optional-using-jsx-without-a-build-step',
23
- }),
24
- },
25
- },
26
- defaultOptions: [],
27
- create(context) {
28
- let hasReactImport = false;
29
- let hasJsxUsage = false;
30
- return {
31
- ImportDeclaration(node) {
32
- if (node.source.value === 'react') {
33
- for (const specifier of node.specifiers) {
34
- if (specifier.type === 'ImportDefaultSpecifier' &&
35
- specifier.local.name === 'React') {
36
- hasReactImport = true;
37
- break;
38
- }
39
- if (specifier.type === 'ImportNamespaceSpecifier' &&
40
- specifier.local.name === 'React') {
41
- hasReactImport = true;
42
- break;
43
- }
44
- }
45
- }
46
- },
47
- JSXElement() {
48
- hasJsxUsage = true;
49
- },
50
- JSXFragment() {
51
- hasJsxUsage = true;
52
- },
53
- 'Program:exit'(node) {
54
- if (hasJsxUsage && !hasReactImport) {
55
- for (const statement of node.body) {
56
- if (statement.type === 'ExpressionStatement' && statement.expression.type === 'JSXElement') {
57
- context.report({
58
- node: statement.expression.openingElement.name,
59
- messageId: 'reactInJsxScope',
60
- });
61
- break;
62
- }
63
- if (statement.type === 'VariableDeclaration') {
64
- for (const declarator of statement.declarations) {
65
- if (declarator.init && declarator.init.type === 'JSXElement') {
66
- context.report({
67
- node: declarator.init.openingElement.name,
68
- messageId: 'reactInJsxScope',
69
- });
70
- return;
71
- }
72
- }
73
- }
74
- if (statement.type === 'ReturnStatement' && statement.argument && statement.argument.type === 'JSXElement') {
75
- context.report({
76
- node: statement.argument.openingElement.name,
77
- messageId: 'reactInJsxScope',
78
- });
79
- break;
80
- }
81
- }
82
- }
83
- },
84
- };
85
- },
86
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.reactInJsxScope=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");exports.reactInJsxScope=(0,eslint_devkit_1.createRule)({name:"react-in-jsx-scope",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/react-in-jsx-scope.md",description:"Ensure React is in scope"},schema:[],messages:{reactInJsxScope:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.WARNING,issueName:"React Not in Scope",description:"React must be in scope when using JSX",severity:"CRITICAL",fix:'Add import React from "react" at the top of the file',documentationLink:"https://react.dev/learn/add-react-to-a-website#optional-using-jsx-without-a-build-step"})}},defaultOptions:[],create(context){let hasReactImport=false;let hasJsxUsage=false;return{ImportDeclaration(node){if(node.source.value==="react"){for(const specifier of node.specifiers){if(specifier.type==="ImportDefaultSpecifier"&&specifier.local.name==="React"){hasReactImport=true;break}if(specifier.type==="ImportNamespaceSpecifier"&&specifier.local.name==="React"){hasReactImport=true;break}}}},JSXElement(){hasJsxUsage=true},JSXFragment(){hasJsxUsage=true},"Program:exit"(node){if(hasJsxUsage&&!hasReactImport){for(const statement of node.body){if(statement.type==="ExpressionStatement"&&statement.expression.type==="JSXElement"){context.report({node:statement.expression.openingElement.name,messageId:"reactInJsxScope"});break}if(statement.type==="VariableDeclaration"){for(const declarator of statement.declarations){if(declarator.init&&declarator.init.type==="JSXElement"){context.report({node:declarator.init.openingElement.name,messageId:"reactInJsxScope"});return}}}if(statement.type==="ReturnStatement"&&statement.argument&&statement.argument.type==="JSXElement"){context.report({node:statement.argument.openingElement.name,messageId:"reactInJsxScope"});break}}}}}}});
@@ -1,134 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.requireDefaultProps = void 0;
4
- const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
- const eslint_devkit_2 = require("@interlace/eslint-devkit");
6
- exports.requireDefaultProps = (0, eslint_devkit_1.createRule)({
7
- name: 'require-default-props',
8
- meta: {
9
- type: 'problem',
10
- docs: {
11
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/require-default-props.md',
12
- description: 'Require default props',
13
- },
14
- messages: {
15
- requireDefaultProps: (0, eslint_devkit_2.formatLLMMessage)({
16
- icon: eslint_devkit_2.MessageIcons.WARNING,
17
- issueName: 'Missing Default Props',
18
- description: 'Prop is not required but has no default value',
19
- severity: 'MEDIUM',
20
- fix: 'Add defaultProps or make prop required in propTypes',
21
- documentationLink: 'https://react.dev/learn/components-and-props#specifying-a-default-prop',
22
- }),
23
- },
24
- schema: [
25
- {
26
- type: 'object',
27
- properties: {
28
- forbidDefaultForRequired: {
29
- type: 'boolean',
30
- },
31
- },
32
- additionalProperties: false,
33
- },
34
- ],
35
- },
36
- defaultOptions: [{}],
37
- create(context) {
38
- const [options] = context.options;
39
- const forbidDefaultForRequired = options?.forbidDefaultForRequired ?? false;
40
- const propTypes = new Map();
41
- const defaultProps = new Map();
42
- const reactComponents = new Set();
43
- return {
44
- ClassDeclaration(node) {
45
- if (isReactComponent(node) && node.id?.name) {
46
- reactComponents.add(node.id.name);
47
- }
48
- },
49
- 'PropertyDefinition[key.name="propTypes"]'(node) {
50
- if (node.value && node.value.type === 'ObjectExpression') {
51
- const componentName = getComponentName(node);
52
- if (componentName) {
53
- const propsMap = new Map();
54
- for (const prop of node.value.properties) {
55
- if (prop.type === 'Property' && prop.key.type === 'Identifier') {
56
- propsMap.set(prop.key.name, prop);
57
- }
58
- }
59
- propTypes.set(componentName, propsMap);
60
- }
61
- }
62
- },
63
- 'PropertyDefinition[key.name="defaultProps"]'(node) {
64
- if (node.value && node.value.type === 'ObjectExpression') {
65
- const componentName = getComponentName(node);
66
- if (componentName) {
67
- const propsMap = new Map();
68
- for (const prop of node.value.properties) {
69
- if (prop.type === 'Property' && prop.key.type === 'Identifier') {
70
- propsMap.set(prop.key.name, prop);
71
- }
72
- }
73
- defaultProps.set(componentName, propsMap);
74
- }
75
- }
76
- },
77
- 'Program:exit'() {
78
- for (const [componentName, propTypesMap] of propTypes) {
79
- if (!reactComponents.has(componentName)) {
80
- continue;
81
- }
82
- const defaultsMap = defaultProps.get(componentName);
83
- for (const [propName, propType] of propTypesMap) {
84
- const hasDefault = defaultsMap?.has(propName);
85
- const isRequired = isRequiredProp(propType);
86
- if (!isRequired && !hasDefault) {
87
- context.report({
88
- node: propType.key,
89
- messageId: 'requireDefaultProps',
90
- });
91
- }
92
- if (forbidDefaultForRequired && isRequired && defaultsMap) {
93
- const defaultProp = defaultsMap.get(propName);
94
- if (defaultProp) {
95
- context.report({
96
- node: defaultProp.key,
97
- messageId: 'requireDefaultProps',
98
- });
99
- }
100
- }
101
- }
102
- }
103
- },
104
- };
105
- function getComponentName(node) {
106
- if (node.parent.type === 'ClassBody' && node.parent.parent.type === 'ClassDeclaration') {
107
- return node.parent.parent.id?.name || null;
108
- }
109
- return null;
110
- }
111
- function isReactComponent(node) {
112
- if (!node.superClass)
113
- return false;
114
- if (node.superClass.type === 'Identifier') {
115
- return node.superClass.name === 'Component' || node.superClass.name === 'PureComponent';
116
- }
117
- if (node.superClass.type === 'MemberExpression') {
118
- return (node.superClass.object.type === 'Identifier' &&
119
- node.superClass.object.name === 'React' &&
120
- node.superClass.property.type === 'Identifier' &&
121
- (node.superClass.property.name === 'Component' || node.superClass.property.name === 'PureComponent'));
122
- }
123
- return false;
124
- }
125
- function isRequiredProp(propType) {
126
- if (propType.value.type === 'MemberExpression' &&
127
- propType.value.property.type === 'Identifier' &&
128
- propType.value.property.name === 'isRequired') {
129
- return true;
130
- }
131
- return false;
132
- }
133
- },
134
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.requireDefaultProps=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");exports.requireDefaultProps=(0,eslint_devkit_1.createRule)({name:"require-default-props",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/require-default-props.md",description:"Require default props"},messages:{requireDefaultProps:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.WARNING,issueName:"Missing Default Props",description:"Prop is not required but has no default value",severity:"MEDIUM",fix:"Add defaultProps or make prop required in propTypes",documentationLink:"https://react.dev/learn/components-and-props#specifying-a-default-prop"})},schema:[{type:"object",properties:{forbidDefaultForRequired:{type:"boolean"}},additionalProperties:false}]},defaultOptions:[{}],create(context){const[options]=context.options;const forbidDefaultForRequired=options?.forbidDefaultForRequired??false;const propTypes=new Map;const defaultProps=new Map;const reactComponents=new Set;return{ClassDeclaration(node){if(isReactComponent(node)&&node.id?.name){reactComponents.add(node.id.name)}},'PropertyDefinition[key.name="propTypes"]'(node){if(node.value&&node.value.type==="ObjectExpression"){const componentName=getComponentName(node);if(componentName){const propsMap=new Map;for(const prop of node.value.properties){if(prop.type==="Property"&&prop.key.type==="Identifier"){propsMap.set(prop.key.name,prop)}}propTypes.set(componentName,propsMap)}}},'PropertyDefinition[key.name="defaultProps"]'(node){if(node.value&&node.value.type==="ObjectExpression"){const componentName=getComponentName(node);if(componentName){const propsMap=new Map;for(const prop of node.value.properties){if(prop.type==="Property"&&prop.key.type==="Identifier"){propsMap.set(prop.key.name,prop)}}defaultProps.set(componentName,propsMap)}}},"Program:exit"(){for(const[componentName,propTypesMap]of propTypes){if(!reactComponents.has(componentName)){continue}const defaultsMap=defaultProps.get(componentName);for(const[propName,propType]of propTypesMap){const hasDefault=defaultsMap?.has(propName);const isRequired=isRequiredProp(propType);if(!isRequired&&!hasDefault){context.report({node:propType.key,messageId:"requireDefaultProps"})}if(forbidDefaultForRequired&&isRequired&&defaultsMap){const defaultProp=defaultsMap.get(propName);if(defaultProp){context.report({node:defaultProp.key,messageId:"requireDefaultProps"})}}}}}};function getComponentName(node){if(node.parent.type==="ClassBody"&&node.parent.parent.type==="ClassDeclaration"){return node.parent.parent.id?.name||null}return null}function 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"){return node.superClass.object.type==="Identifier"&&node.superClass.object.name==="React"&&node.superClass.property.type==="Identifier"&&(node.superClass.property.name==="Component"||node.superClass.property.name==="PureComponent")}return false}function isRequiredProp(propType){if(propType.value.type==="MemberExpression"&&propType.value.property.type==="Identifier"&&propType.value.property.name==="isRequired"){return true}return false}}});
@@ -1,397 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.requireOptimization = void 0;
4
- const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
- const eslint_devkit_2 = require("@interlace/eslint-devkit");
6
- exports.requireOptimization = (0, eslint_devkit_1.createRule)({
7
- name: 'require-optimization',
8
- meta: {
9
- type: 'suggestion',
10
- docs: {
11
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/require-optimization.md',
12
- description: 'Require performance optimizations for React components based on usage patterns',
13
- },
14
- hasSuggestions: true,
15
- messages: {
16
- considerMemo: (0, eslint_devkit_2.formatLLMMessage)({
17
- icon: eslint_devkit_2.MessageIcons.PERFORMANCE,
18
- issueName: 'Component Re-rendering',
19
- description: 'Component may benefit from React.memo optimization',
20
- severity: 'MEDIUM',
21
- fix: 'Wrap with React.memo() to prevent unnecessary re-renders',
22
- documentationLink: 'https://react.dev/reference/react/memo',
23
- }),
24
- considerUseMemo: (0, eslint_devkit_2.formatLLMMessage)({
25
- icon: eslint_devkit_2.MessageIcons.PERFORMANCE,
26
- issueName: 'Expensive Computation',
27
- description: 'Expensive computation should use useMemo',
28
- severity: 'MEDIUM',
29
- fix: 'Wrap computation with useMemo() to cache results',
30
- documentationLink: 'https://react.dev/reference/react/useMemo',
31
- }),
32
- considerUseCallback: (0, eslint_devkit_2.formatLLMMessage)({
33
- icon: eslint_devkit_2.MessageIcons.PERFORMANCE,
34
- issueName: 'Function Re-creation',
35
- description: 'Event handlers should use useCallback',
36
- severity: 'MEDIUM',
37
- fix: 'Wrap event handler with useCallback() to maintain reference equality',
38
- documentationLink: 'https://react.dev/reference/react/useCallback',
39
- }),
40
- considerLazy: (0, eslint_devkit_2.formatLLMMessage)({
41
- icon: eslint_devkit_2.MessageIcons.PERFORMANCE,
42
- issueName: 'Bundle Size',
43
- description: 'Large component should use React.lazy for code splitting',
44
- severity: 'LOW',
45
- fix: 'Use React.lazy() and Suspense for code splitting',
46
- documentationLink: 'https://react.dev/reference/react/lazy',
47
- }),
48
- considerPureComponent: (0, eslint_devkit_2.formatLLMMessage)({
49
- icon: eslint_devkit_2.MessageIcons.PERFORMANCE,
50
- issueName: 'Class Component',
51
- description: 'Class component may benefit from PureComponent',
52
- severity: 'MEDIUM',
53
- fix: 'Extend PureComponent instead of Component for shallow comparison',
54
- documentationLink: 'https://react.dev/reference/react/PureComponent',
55
- }),
56
- performanceOptimization: (0, eslint_devkit_2.formatLLMMessage)({
57
- icon: eslint_devkit_2.MessageIcons.PERFORMANCE,
58
- issueName: 'Performance Opportunity',
59
- description: 'Performance optimization opportunity detected',
60
- severity: 'MEDIUM',
61
- fix: 'Apply appropriate React optimization based on usage patterns',
62
- documentationLink: 'https://react.dev/learn/render-and-commit',
63
- }),
64
- },
65
- schema: [
66
- {
67
- type: 'object',
68
- properties: {
69
- suggestMemo: {
70
- type: 'boolean',
71
- default: true,
72
- },
73
- suggestUseMemo: {
74
- type: 'boolean',
75
- default: true,
76
- },
77
- suggestUseCallback: {
78
- type: 'boolean',
79
- default: true,
80
- },
81
- minPropsForMemo: {
82
- type: 'number',
83
- minimum: 1,
84
- default: 3,
85
- },
86
- },
87
- additionalProperties: false,
88
- },
89
- ],
90
- },
91
- defaultOptions: [{ suggestMemo: true, suggestUseMemo: true, suggestUseCallback: true, minPropsForMemo: 3 }],
92
- create(context) {
93
- const [options] = context.options;
94
- const { suggestMemo = true, suggestUseMemo = true, suggestUseCallback = true, minPropsForMemo = 3, } = options || {};
95
- const components = new Map();
96
- function analyzeFunctionComponent(node) {
97
- const componentName = node.type === 'FunctionDeclaration' ? node.id?.name : 'AnonymousComponent';
98
- if (!componentName)
99
- return;
100
- const linesOfCode = node.loc ? node.loc.end.line - node.loc.start.line + 1 : 0;
101
- let propsCount = 0;
102
- if (node.params.length > 0) {
103
- const firstParam = node.params[0];
104
- if (firstParam.type === 'ObjectPattern') {
105
- propsCount = firstParam.properties.length;
106
- }
107
- else if (firstParam.type === 'Identifier' && firstParam.name === 'props') {
108
- propsCount = 1;
109
- }
110
- }
111
- let hasExpensiveComputations = false;
112
- let hasEventHandlers = false;
113
- let hasInlineFunctions = false;
114
- let renderStatements = 0;
115
- function analyzeNode(currentNode, depth = 0, visited = new Set()) {
116
- if (depth > 10 || visited.has(currentNode)) {
117
- return;
118
- }
119
- visited.add(currentNode);
120
- if (currentNode.type === 'CallExpression' &&
121
- currentNode.callee.type === 'MemberExpression' &&
122
- currentNode.callee.property.type === 'Identifier' &&
123
- ['map', 'filter', 'reduce', 'find'].includes(currentNode.callee.property.name)) {
124
- hasExpensiveComputations = true;
125
- }
126
- if (currentNode.type === 'JSXAttribute' &&
127
- currentNode.name.type === 'JSXIdentifier' &&
128
- currentNode.name.name.startsWith('on') &&
129
- currentNode.value?.type === 'JSXExpressionContainer') {
130
- hasEventHandlers = true;
131
- const value = currentNode.value.expression;
132
- if (value.type === 'ArrowFunctionExpression' ||
133
- value.type === 'FunctionExpression') {
134
- hasInlineFunctions = true;
135
- }
136
- else if (value.type === 'CallExpression' &&
137
- value.callee.type === 'ArrowFunctionExpression') {
138
- hasInlineFunctions = true;
139
- }
140
- }
141
- if (currentNode.type === 'ReturnStatement' ||
142
- currentNode.type === 'JSXElement' ||
143
- currentNode.type === 'JSXFragment') {
144
- renderStatements++;
145
- }
146
- if (depth < 10) {
147
- if (currentNode.type === 'BlockStatement' && currentNode.body) {
148
- currentNode.body.forEach((stmt) => {
149
- analyzeNode(stmt, depth + 1, visited);
150
- });
151
- }
152
- else if (currentNode.type === 'ReturnStatement' && currentNode.argument) {
153
- analyzeNode(currentNode.argument, depth + 1, visited);
154
- }
155
- else if (currentNode.type === 'JSXElement') {
156
- if (currentNode.openingElement && currentNode.openingElement.attributes) {
157
- currentNode.openingElement.attributes.forEach((attr) => {
158
- if (attr.type === 'JSXAttribute') {
159
- analyzeNode(attr, depth + 1, visited);
160
- }
161
- });
162
- }
163
- if (currentNode.children) {
164
- currentNode.children.forEach((child) => {
165
- analyzeNode(child, depth + 1, visited);
166
- });
167
- }
168
- }
169
- else if (currentNode.type === 'CallExpression' && currentNode.arguments) {
170
- currentNode.arguments.forEach((arg) => {
171
- if (arg.type !== 'SpreadElement') {
172
- analyzeNode(arg, depth + 1, visited);
173
- }
174
- });
175
- }
176
- else if (currentNode.type === 'VariableDeclaration' && currentNode.declarations) {
177
- currentNode.declarations.forEach((decl) => {
178
- if (decl.init) {
179
- analyzeNode(decl.init, depth + 1, visited);
180
- }
181
- });
182
- }
183
- }
184
- }
185
- const body = node.body;
186
- if (body) {
187
- analyzeNode(body);
188
- }
189
- const componentData = {
190
- name: componentName,
191
- type: node.type === 'ArrowFunctionExpression' ? 'arrow' : 'function',
192
- node,
193
- propsCount,
194
- hasExpensiveComputations,
195
- hasEventHandlers,
196
- hasInlineFunctions,
197
- renderStatements,
198
- linesOfCode,
199
- };
200
- components.set(componentName, componentData);
201
- analyzeComponentOptimizations(componentName, componentData);
202
- }
203
- function analyzeClassComponent(node) {
204
- const componentName = node.id?.name;
205
- if (!componentName)
206
- return;
207
- let extendsComponent = false;
208
- let extendsPureComponent = false;
209
- if (node.superClass) {
210
- if (node.superClass.type === 'Identifier') {
211
- extendsComponent = node.superClass.name === 'Component';
212
- extendsPureComponent = node.superClass.name === 'PureComponent';
213
- }
214
- else if (node.superClass.type === 'MemberExpression' &&
215
- node.superClass.object.type === 'Identifier' &&
216
- node.superClass.object.name === 'React') {
217
- extendsComponent = node.superClass.property.type === 'Identifier' &&
218
- node.superClass.property.name === 'Component';
219
- extendsPureComponent = node.superClass.property.type === 'Identifier' &&
220
- node.superClass.property.name === 'PureComponent';
221
- }
222
- }
223
- if (!extendsComponent && !extendsPureComponent) {
224
- return;
225
- }
226
- let propsCount = 0;
227
- let hasExpensiveComputations = false;
228
- let hasEventHandlers = false;
229
- let renderStatements = 0;
230
- let linesOfCode = 0;
231
- for (const member of node.body.body) {
232
- if (member.type === 'MethodDefinition' &&
233
- member.key.type === 'Identifier' &&
234
- member.key.name === 'render' &&
235
- member.value.body) {
236
- const body = member.value.body;
237
- linesOfCode = body.loc ? body.loc.end.line - body.loc.start.line + 1 : 0;
238
- function analyzeRenderNode(currentNode, depth = 0, visited = new Set()) {
239
- if (depth > 10 || visited.has(currentNode)) {
240
- return;
241
- }
242
- visited.add(currentNode);
243
- if (currentNode.type === 'VariableDeclaration') {
244
- currentNode.declarations.forEach((decl) => {
245
- if (decl.id.type === 'ObjectPattern' && decl.init) {
246
- if (decl.init.type === 'MemberExpression' &&
247
- decl.init.object.type === 'ThisExpression' &&
248
- decl.init.property.type === 'Identifier' &&
249
- decl.init.property.name === 'props') {
250
- propsCount = decl.id.properties.length;
251
- }
252
- }
253
- });
254
- }
255
- if (currentNode.type === 'MemberExpression' &&
256
- currentNode.object.type === 'ThisExpression' &&
257
- currentNode.property.type === 'Identifier' &&
258
- currentNode.property.name === 'props') {
259
- if (propsCount === 0)
260
- propsCount = 1;
261
- }
262
- if (currentNode.type === 'CallExpression' &&
263
- currentNode.callee.type === 'MemberExpression' &&
264
- currentNode.callee.property.type === 'Identifier' &&
265
- ['map', 'filter', 'reduce', 'sort'].includes(currentNode.callee.property.name)) {
266
- hasExpensiveComputations = true;
267
- }
268
- if (currentNode.type === 'JSXAttribute' &&
269
- currentNode.name.type === 'JSXIdentifier' &&
270
- currentNode.name.name.startsWith('on')) {
271
- hasEventHandlers = true;
272
- }
273
- if (currentNode.type === 'ReturnStatement' ||
274
- currentNode.type === 'JSXElement') {
275
- renderStatements++;
276
- }
277
- if (depth < 10) {
278
- if (currentNode.type === 'BlockStatement' && currentNode.body) {
279
- currentNode.body.forEach((stmt) => {
280
- analyzeRenderNode(stmt, depth + 1, visited);
281
- });
282
- }
283
- else if (currentNode.type === 'ReturnStatement' && currentNode.argument) {
284
- analyzeRenderNode(currentNode.argument, depth + 1, visited);
285
- }
286
- else if (currentNode.type === 'JSXElement') {
287
- if (currentNode.children) {
288
- currentNode.children.forEach((child) => {
289
- analyzeRenderNode(child, depth + 1, visited);
290
- });
291
- }
292
- }
293
- else if (currentNode.type === 'VariableDeclaration') {
294
- currentNode.declarations.forEach((decl) => {
295
- if (decl.init) {
296
- analyzeRenderNode(decl.init, depth + 1, visited);
297
- }
298
- });
299
- }
300
- }
301
- }
302
- analyzeRenderNode(member.value.body);
303
- }
304
- }
305
- components.set(componentName, {
306
- name: componentName,
307
- type: 'class',
308
- node,
309
- propsCount,
310
- hasExpensiveComputations,
311
- hasEventHandlers,
312
- hasInlineFunctions: false,
313
- renderStatements,
314
- linesOfCode,
315
- });
316
- if (extendsComponent && !extendsPureComponent) {
317
- const minPropsForPureComponent = 3;
318
- if (propsCount >= minPropsForPureComponent || hasExpensiveComputations) {
319
- context.report({
320
- node: node.superClass,
321
- messageId: 'considerPureComponent',
322
- data: {
323
- componentName,
324
- reason: propsCount >= minPropsForPureComponent
325
- ? `has ${propsCount} props and may benefit from shallow comparison`
326
- : 'contains expensive computations',
327
- },
328
- });
329
- }
330
- }
331
- return;
332
- }
333
- function analyzeComponentOptimizations(componentName, component) {
334
- const { type, node, propsCount, hasExpensiveComputations, hasInlineFunctions, linesOfCode } = component;
335
- if (suggestMemo && type !== 'class' && propsCount >= minPropsForMemo) {
336
- context.report({
337
- node,
338
- messageId: 'considerMemo',
339
- data: {
340
- componentName,
341
- propsCount,
342
- reason: `receives ${propsCount} props and may re-render unnecessarily`,
343
- },
344
- });
345
- }
346
- if (suggestUseMemo && hasExpensiveComputations && type !== 'class') {
347
- context.report({
348
- node,
349
- messageId: 'considerUseMemo',
350
- data: {
351
- componentName,
352
- reason: 'contains array operations that could be expensive',
353
- },
354
- });
355
- }
356
- if (suggestUseCallback && hasInlineFunctions && type !== 'class') {
357
- context.report({
358
- node,
359
- messageId: 'considerUseCallback',
360
- data: {
361
- componentName,
362
- reason: 'has inline event handlers that create new functions on each render',
363
- },
364
- });
365
- }
366
- if (linesOfCode > 50) {
367
- context.report({
368
- node,
369
- messageId: 'considerLazy',
370
- data: {
371
- componentName,
372
- linesOfCode,
373
- reason: `component has ${linesOfCode} lines and may benefit from code splitting`,
374
- },
375
- });
376
- }
377
- }
378
- return {
379
- FunctionDeclaration(node) {
380
- if (node.id?.name && /^[A-Z]/.test(node.id.name)) {
381
- analyzeFunctionComponent(node);
382
- }
383
- },
384
- ArrowFunctionExpression(node) {
385
- const parent = node.parent;
386
- if (parent?.type === 'VariableDeclarator' &&
387
- parent.id?.type === 'Identifier' &&
388
- /^[A-Z]/.test(parent.id.name)) {
389
- analyzeFunctionComponent(node);
390
- }
391
- },
392
- ClassDeclaration(node) {
393
- analyzeClassComponent(node);
394
- },
395
- };
396
- },
397
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.requireOptimization=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");exports.requireOptimization=(0,eslint_devkit_1.createRule)({name:"require-optimization",meta:{type:"suggestion",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/require-optimization.md",description:"Require performance optimizations for React components based on usage patterns"},hasSuggestions:true,messages:{considerMemo:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.PERFORMANCE,issueName:"Component Re-rendering",description:"Component may benefit from React.memo optimization",severity:"MEDIUM",fix:"Wrap with React.memo() to prevent unnecessary re-renders",documentationLink:"https://react.dev/reference/react/memo"}),considerUseMemo:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.PERFORMANCE,issueName:"Expensive Computation",description:"Expensive computation should use useMemo",severity:"MEDIUM",fix:"Wrap computation with useMemo() to cache results",documentationLink:"https://react.dev/reference/react/useMemo"}),considerUseCallback:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.PERFORMANCE,issueName:"Function Re-creation",description:"Event handlers should use useCallback",severity:"MEDIUM",fix:"Wrap event handler with useCallback() to maintain reference equality",documentationLink:"https://react.dev/reference/react/useCallback"}),considerLazy:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.PERFORMANCE,issueName:"Bundle Size",description:"Large component should use React.lazy for code splitting",severity:"LOW",fix:"Use React.lazy() and Suspense for code splitting",documentationLink:"https://react.dev/reference/react/lazy"}),considerPureComponent:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.PERFORMANCE,issueName:"Class Component",description:"Class component may benefit from PureComponent",severity:"MEDIUM",fix:"Extend PureComponent instead of Component for shallow comparison",documentationLink:"https://react.dev/reference/react/PureComponent"}),performanceOptimization:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.PERFORMANCE,issueName:"Performance Opportunity",description:"Performance optimization opportunity detected",severity:"MEDIUM",fix:"Apply appropriate React optimization based on usage patterns",documentationLink:"https://react.dev/learn/render-and-commit"})},schema:[{type:"object",properties:{suggestMemo:{type:"boolean",default:true},suggestUseMemo:{type:"boolean",default:true},suggestUseCallback:{type:"boolean",default:true},minPropsForMemo:{type:"number",minimum:1,default:3}},additionalProperties:false}]},defaultOptions:[{suggestMemo:true,suggestUseMemo:true,suggestUseCallback:true,minPropsForMemo:3}],create(context){const[options]=context.options;const{suggestMemo=true,suggestUseMemo=true,suggestUseCallback=true,minPropsForMemo=3}=options||{};const components=new Map;function analyzeFunctionComponent(node){const componentName=node.type==="FunctionDeclaration"?node.id?.name:"AnonymousComponent";if(!componentName)return;const linesOfCode=node.loc?node.loc.end.line-node.loc.start.line+1:0;let propsCount=0;if(node.params.length>0){const firstParam=node.params[0];if(firstParam.type==="ObjectPattern"){propsCount=firstParam.properties.length}else if(firstParam.type==="Identifier"&&firstParam.name==="props"){propsCount=1}}let hasExpensiveComputations=false;let hasEventHandlers=false;let hasInlineFunctions=false;let renderStatements=0;function analyzeNode(currentNode,depth=0,visited=new Set){if(depth>10||visited.has(currentNode)){return}visited.add(currentNode);if(currentNode.type==="CallExpression"&&currentNode.callee.type==="MemberExpression"&&currentNode.callee.property.type==="Identifier"&&["map","filter","reduce","find"].includes(currentNode.callee.property.name)){hasExpensiveComputations=true}if(currentNode.type==="JSXAttribute"&&currentNode.name.type==="JSXIdentifier"&&currentNode.name.name.startsWith("on")&&currentNode.value?.type==="JSXExpressionContainer"){hasEventHandlers=true;const value=currentNode.value.expression;if(value.type==="ArrowFunctionExpression"||value.type==="FunctionExpression"){hasInlineFunctions=true}else if(value.type==="CallExpression"&&value.callee.type==="ArrowFunctionExpression"){hasInlineFunctions=true}}if(currentNode.type==="ReturnStatement"||currentNode.type==="JSXElement"||currentNode.type==="JSXFragment"){renderStatements++}if(depth<10){if(currentNode.type==="BlockStatement"&&currentNode.body){currentNode.body.forEach(stmt=>{analyzeNode(stmt,depth+1,visited)})}else if(currentNode.type==="ReturnStatement"&&currentNode.argument){analyzeNode(currentNode.argument,depth+1,visited)}else if(currentNode.type==="JSXElement"){if(currentNode.openingElement&&currentNode.openingElement.attributes){currentNode.openingElement.attributes.forEach(attr=>{if(attr.type==="JSXAttribute"){analyzeNode(attr,depth+1,visited)}})}if(currentNode.children){currentNode.children.forEach(child=>{analyzeNode(child,depth+1,visited)})}}else if(currentNode.type==="CallExpression"&&currentNode.arguments){currentNode.arguments.forEach(arg=>{if(arg.type!=="SpreadElement"){analyzeNode(arg,depth+1,visited)}})}else if(currentNode.type==="VariableDeclaration"&&currentNode.declarations){currentNode.declarations.forEach(decl=>{if(decl.init){analyzeNode(decl.init,depth+1,visited)}})}}}const body=node.body;if(body){analyzeNode(body)}const componentData={name:componentName,type:node.type==="ArrowFunctionExpression"?"arrow":"function",node,propsCount,hasExpensiveComputations,hasEventHandlers,hasInlineFunctions,renderStatements,linesOfCode};components.set(componentName,componentData);analyzeComponentOptimizations(componentName,componentData)}function analyzeClassComponent(node){const componentName=node.id?.name;if(!componentName)return;let extendsComponent=false;let extendsPureComponent=false;if(node.superClass){if(node.superClass.type==="Identifier"){extendsComponent=node.superClass.name==="Component";extendsPureComponent=node.superClass.name==="PureComponent"}else if(node.superClass.type==="MemberExpression"&&node.superClass.object.type==="Identifier"&&node.superClass.object.name==="React"){extendsComponent=node.superClass.property.type==="Identifier"&&node.superClass.property.name==="Component";extendsPureComponent=node.superClass.property.type==="Identifier"&&node.superClass.property.name==="PureComponent"}}if(!extendsComponent&&!extendsPureComponent){return}let propsCount=0;let hasExpensiveComputations=false;let hasEventHandlers=false;let renderStatements=0;let linesOfCode=0;for(const member of node.body.body){if(member.type==="MethodDefinition"&&member.key.type==="Identifier"&&member.key.name==="render"&&member.value.body){let analyzeRenderNode=function(currentNode,depth=0,visited=new Set){if(depth>10||visited.has(currentNode)){return}visited.add(currentNode);if(currentNode.type==="VariableDeclaration"){currentNode.declarations.forEach(decl=>{if(decl.id.type==="ObjectPattern"&&decl.init){if(decl.init.type==="MemberExpression"&&decl.init.object.type==="ThisExpression"&&decl.init.property.type==="Identifier"&&decl.init.property.name==="props"){propsCount=decl.id.properties.length}}})}if(currentNode.type==="MemberExpression"&&currentNode.object.type==="ThisExpression"&&currentNode.property.type==="Identifier"&&currentNode.property.name==="props"){if(propsCount===0)propsCount=1}if(currentNode.type==="CallExpression"&&currentNode.callee.type==="MemberExpression"&&currentNode.callee.property.type==="Identifier"&&["map","filter","reduce","sort"].includes(currentNode.callee.property.name)){hasExpensiveComputations=true}if(currentNode.type==="JSXAttribute"&&currentNode.name.type==="JSXIdentifier"&&currentNode.name.name.startsWith("on")){hasEventHandlers=true}if(currentNode.type==="ReturnStatement"||currentNode.type==="JSXElement"){renderStatements++}if(depth<10){if(currentNode.type==="BlockStatement"&&currentNode.body){currentNode.body.forEach(stmt=>{analyzeRenderNode(stmt,depth+1,visited)})}else if(currentNode.type==="ReturnStatement"&&currentNode.argument){analyzeRenderNode(currentNode.argument,depth+1,visited)}else if(currentNode.type==="JSXElement"){if(currentNode.children){currentNode.children.forEach(child=>{analyzeRenderNode(child,depth+1,visited)})}}else if(currentNode.type==="VariableDeclaration"){currentNode.declarations.forEach(decl=>{if(decl.init){analyzeRenderNode(decl.init,depth+1,visited)}})}}};const body=member.value.body;linesOfCode=body.loc?body.loc.end.line-body.loc.start.line+1:0;analyzeRenderNode(member.value.body)}}components.set(componentName,{name:componentName,type:"class",node,propsCount,hasExpensiveComputations,hasEventHandlers,hasInlineFunctions:false,renderStatements,linesOfCode});if(extendsComponent&&!extendsPureComponent){const minPropsForPureComponent=3;if(propsCount>=minPropsForPureComponent||hasExpensiveComputations){context.report({node:node.superClass,messageId:"considerPureComponent",data:{componentName,reason:propsCount>=minPropsForPureComponent?`has ${propsCount} props and may benefit from shallow comparison`:"contains expensive computations"}})}}return}function analyzeComponentOptimizations(componentName,component){const{type,node,propsCount,hasExpensiveComputations,hasInlineFunctions,linesOfCode}=component;if(suggestMemo&&type!=="class"&&propsCount>=minPropsForMemo){context.report({node,messageId:"considerMemo",data:{componentName,propsCount,reason:`receives ${propsCount} props and may re-render unnecessarily`}})}if(suggestUseMemo&&hasExpensiveComputations&&type!=="class"){context.report({node,messageId:"considerUseMemo",data:{componentName,reason:"contains array operations that could be expensive"}})}if(suggestUseCallback&&hasInlineFunctions&&type!=="class"){context.report({node,messageId:"considerUseCallback",data:{componentName,reason:"has inline event handlers that create new functions on each render"}})}if(linesOfCode>50){context.report({node,messageId:"considerLazy",data:{componentName,linesOfCode,reason:`component has ${linesOfCode} lines and may benefit from code splitting`}})}}return{FunctionDeclaration(node){if(node.id?.name&&/^[A-Z]/.test(node.id.name)){analyzeFunctionComponent(node)}},ArrowFunctionExpression(node){const parent=node.parent;if(parent?.type==="VariableDeclarator"&&parent.id?.type==="Identifier"&&/^[A-Z]/.test(parent.id.name)){analyzeFunctionComponent(node)}},ClassDeclaration(node){analyzeClassComponent(node)}}}});