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,133 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.displayName = void 0;
4
- const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
- const eslint_devkit_2 = require("@interlace/eslint-devkit");
6
- exports.displayName = (0, eslint_devkit_1.createRule)({
7
- name: 'display-name',
8
- meta: {
9
- type: 'problem',
10
- docs: {
11
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/display-name.md',
12
- description: 'Enforce component display names',
13
- },
14
- schema: [],
15
- messages: {
16
- displayName: (0, eslint_devkit_2.formatLLMMessage)({
17
- icon: eslint_devkit_2.MessageIcons.WARNING,
18
- issueName: 'Missing Display Name',
19
- description: 'Component missing display name',
20
- severity: 'LOW',
21
- fix: 'Add displayName static property or named export',
22
- documentationLink: 'https://react.dev/learn/components-and-props#displaying-a-custom-component',
23
- }),
24
- },
25
- },
26
- defaultOptions: [],
27
- create(context) {
28
- return {
29
- ClassDeclaration(node) {
30
- if (isReactComponent(node)) {
31
- const hasDisplayName = hasDisplayNameProperty(node);
32
- if (!hasDisplayName) {
33
- context.report({
34
- node: node.id || node,
35
- messageId: 'displayName',
36
- });
37
- }
38
- }
39
- },
40
- 'VariableDeclarator, FunctionDeclaration'(node) {
41
- const component = getComponentFromDeclaration(node);
42
- if (component && isReactComponentFunction(component) && !hasDisplayNameInScope()) {
43
- context.report({
44
- node: getComponentNameNode(node),
45
- messageId: 'displayName',
46
- });
47
- }
48
- },
49
- };
50
- },
51
- });
52
- function isReactComponent(node) {
53
- if (!node.superClass)
54
- return false;
55
- if (node.superClass.type === 'Identifier') {
56
- return node.superClass.name === 'Component' || node.superClass.name === 'PureComponent';
57
- }
58
- if (node.superClass.type === 'MemberExpression') {
59
- return (node.superClass.object.type === 'Identifier' &&
60
- node.superClass.object.name === 'React' &&
61
- node.superClass.property.type === 'Identifier' &&
62
- (node.superClass.property.name === 'Component' || node.superClass.property.name === 'PureComponent'));
63
- }
64
- return false;
65
- }
66
- function hasDisplayNameProperty(node) {
67
- for (const member of node.body.body) {
68
- if (member.type === 'PropertyDefinition' &&
69
- member.key.type === 'Identifier' &&
70
- member.key.name === 'displayName' &&
71
- member.static) {
72
- return true;
73
- }
74
- }
75
- return false;
76
- }
77
- function getComponentFromDeclaration(node) {
78
- if (node.type === 'FunctionDeclaration') {
79
- return node;
80
- }
81
- if (node.type === 'VariableDeclarator' && node.init) {
82
- if (node.init.type === 'FunctionExpression' || node.init.type === 'ArrowFunctionExpression') {
83
- return node.init;
84
- }
85
- }
86
- return null;
87
- }
88
- function isReactComponentFunction(node) {
89
- const body = node.type === 'FunctionDeclaration' ? node.body : node.body;
90
- return containsJSX(body);
91
- }
92
- function hasDisplayNameInScope() {
93
- return false;
94
- }
95
- function getComponentNameNode(node) {
96
- if (node.type === 'FunctionDeclaration') {
97
- return node.id || node;
98
- }
99
- return node.id || node;
100
- }
101
- function containsJSX(node, visited = new Set()) {
102
- if (!node)
103
- return false;
104
- if (visited.has(node))
105
- return false;
106
- visited.add(node);
107
- if (node.type === 'JSXElement' || node.type === 'JSXFragment') {
108
- return true;
109
- }
110
- const skipKeys = new Set(['parent', 'range', 'loc', 'start', 'end', 'tokens', 'comments']);
111
- for (const key in node) {
112
- if (skipKeys.has(key))
113
- continue;
114
- const child = node[key];
115
- if (child && typeof child === 'object') {
116
- if (Array.isArray(child)) {
117
- for (const item of child) {
118
- if (item && typeof item === 'object' && 'type' in item) {
119
- if (containsJSX(item, visited)) {
120
- return true;
121
- }
122
- }
123
- }
124
- }
125
- else if ('type' in child) {
126
- if (containsJSX(child, visited)) {
127
- return true;
128
- }
129
- }
130
- }
131
- }
132
- return false;
133
- }
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.displayName=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");exports.displayName=(0,eslint_devkit_1.createRule)({name:"display-name",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/display-name.md",description:"Enforce component display names"},schema:[],messages:{displayName:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.WARNING,issueName:"Missing Display Name",description:"Component missing display name",severity:"LOW",fix:"Add displayName static property or named export",documentationLink:"https://react.dev/learn/components-and-props#displaying-a-custom-component"})}},defaultOptions:[],create(context){return{ClassDeclaration(node){if(isReactComponent(node)){const hasDisplayName=hasDisplayNameProperty(node);if(!hasDisplayName){context.report({node:node.id||node,messageId:"displayName"})}}},"VariableDeclarator, FunctionDeclaration"(node){const component=getComponentFromDeclaration(node);if(component&&isReactComponentFunction(component)&&!hasDisplayNameInScope()){context.report({node:getComponentNameNode(node),messageId:"displayName"})}}}}});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 hasDisplayNameProperty(node){for(const member of node.body.body){if(member.type==="PropertyDefinition"&&member.key.type==="Identifier"&&member.key.name==="displayName"&&member.static){return true}}return false}function getComponentFromDeclaration(node){if(node.type==="FunctionDeclaration"){return node}if(node.type==="VariableDeclarator"&&node.init){if(node.init.type==="FunctionExpression"||node.init.type==="ArrowFunctionExpression"){return node.init}}return null}function isReactComponentFunction(node){const body=node.type==="FunctionDeclaration"?node.body:node.body;return containsJSX(body)}function hasDisplayNameInScope(){return false}function getComponentNameNode(node){if(node.type==="FunctionDeclaration"){return node.id||node}return node.id||node}function containsJSX(node,visited=new Set){if(!node)return false;if(visited.has(node))return false;visited.add(node);if(node.type==="JSXElement"||node.type==="JSXFragment"){return true}const skipKeys=new Set(["parent","range","loc","start","end","tokens","comments"]);for(const key in node){if(skipKeys.has(key))continue;const child=node[key];if(child&&typeof child==="object"){if(Array.isArray(child)){for(const item of child){if(item&&typeof item==="object"&&"type"in item){if(containsJSX(item,visited)){return true}}}}else if("type"in child){if(containsJSX(child,visited)){return true}}}}return false}
@@ -1,466 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.hooksExhaustiveDeps = void 0;
4
- const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
- const eslint_devkit_2 = require("@interlace/eslint-devkit");
6
- const SKIP_KEYS = new Set(['parent', 'loc', 'range', 'tokens', 'comments', 'type']);
7
- const CHILD_KEYS_CACHE = new Map();
8
- function getChildKeys(node) {
9
- const cached = CHILD_KEYS_CACHE.get(node.type);
10
- if (cached)
11
- return cached;
12
- const keys = [];
13
- for (const key of Object.keys(node)) {
14
- if (SKIP_KEYS.has(key))
15
- continue;
16
- const v = node[key];
17
- if (v && typeof v === 'object') {
18
- keys.push(key);
19
- }
20
- }
21
- CHILD_KEYS_CACHE.set(node.type, keys);
22
- return keys;
23
- }
24
- const HOOKS_WITH_DEPS = new Set([
25
- 'useEffect',
26
- 'useLayoutEffect',
27
- 'useCallback',
28
- 'useMemo',
29
- 'useImperativeHandle',
30
- ]);
31
- exports.hooksExhaustiveDeps = (0, eslint_devkit_1.createRule)({
32
- name: 'hooks-exhaustive-deps',
33
- meta: {
34
- type: 'problem',
35
- docs: {
36
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/hooks-exhaustive-deps.md',
37
- description: 'Enforce exhaustive dependencies in React hooks to prevent stale closures',
38
- confidence: 'high',
39
- },
40
- hasSuggestions: true,
41
- messages: {
42
- missingDep: (0, eslint_devkit_2.formatLLMMessage)({
43
- icon: eslint_devkit_2.MessageIcons.WARNING,
44
- issueName: 'Missing Hook Dependency',
45
- description: 'React Hook {{hookName}} has missing dependencies: {{deps}}',
46
- severity: 'HIGH',
47
- fix: 'Add missing dependencies to the dependency array or memoize values with useMemo/useCallback',
48
- documentationLink: 'https://react.dev/reference/react/useEffect#specifying-reactive-dependencies',
49
- }),
50
- extraDep: (0, eslint_devkit_2.formatLLMMessage)({
51
- icon: eslint_devkit_2.MessageIcons.WARNING,
52
- issueName: 'Unnecessary Hook Dependency',
53
- description: 'React Hook {{hookName}} has unnecessary dependency: {{dep}}',
54
- severity: 'MEDIUM',
55
- fix: 'Remove the unnecessary dependency from the array - it never changes or is not used in the effect',
56
- documentationLink: 'https://react.dev/reference/react/useEffect#removing-unnecessary-dependencies',
57
- }),
58
- unnecessaryDep: (0, eslint_devkit_2.formatLLMMessage)({
59
- icon: eslint_devkit_2.MessageIcons.INFO,
60
- issueName: 'Unnecessary Dependency',
61
- description: 'The dependency {{dep}} is stable and never changes',
62
- severity: 'LOW',
63
- fix: 'Consider removing stable dependencies like setState functions or refs',
64
- documentationLink: 'https://react.dev/reference/react/useEffect#removing-unnecessary-dependencies',
65
- }),
66
- suggestAddDep: (0, eslint_devkit_2.formatLLMMessage)({
67
- icon: eslint_devkit_2.MessageIcons.INFO,
68
- issueName: 'Add Missing Dependency',
69
- description: 'Add {{dep}} to dependency array',
70
- severity: 'LOW',
71
- fix: 'Add the missing dependency to prevent stale closures',
72
- documentationLink: 'https://react.dev/reference/react/useEffect#specifying-reactive-dependencies',
73
- }),
74
- suggestRemoveDep: (0, eslint_devkit_2.formatLLMMessage)({
75
- icon: eslint_devkit_2.MessageIcons.INFO,
76
- issueName: 'Remove Unnecessary Dependency',
77
- description: 'Remove {{dep}} from dependency array',
78
- severity: 'LOW',
79
- fix: 'Remove the unnecessary dependency',
80
- documentationLink: 'https://react.dev/reference/react/useEffect#removing-unnecessary-dependencies',
81
- }),
82
- depsNotArrayLiteral: (0, eslint_devkit_2.formatLLMMessage)({
83
- icon: eslint_devkit_2.MessageIcons.WARNING,
84
- issueName: 'Non-literal Dependency Array',
85
- description: 'React Hook {{hookName}} was passed a dependency list that is not an array literal. This means we cannot statically verify whether you have passed the correct dependencies.',
86
- severity: 'HIGH',
87
- fix: 'Replace the variable with an inline array literal: [dep1, dep2]. If you need dynamic deps, refactor to compute them inside the hook callback instead.',
88
- documentationLink: 'https://react.dev/reference/react/useEffect#specifying-reactive-dependencies',
89
- }),
90
- },
91
- schema: [
92
- {
93
- type: 'object',
94
- properties: {
95
- additionalHooks: {
96
- type: 'string',
97
- description: 'Regex pattern for additional hooks to check (e.g., "useMyCustomHook")',
98
- },
99
- enableDangerousAutofixThisMayCauseInfiniteLoops: {
100
- type: 'boolean',
101
- default: false,
102
- description: 'Enable autofix (warning: may cause infinite loops)',
103
- },
104
- },
105
- additionalProperties: false,
106
- },
107
- ],
108
- },
109
- defaultOptions: [{}],
110
- create(context) {
111
- const options = context.options[0] || {};
112
- const additionalHooksPattern = options.additionalHooks
113
- ? new RegExp(options.additionalHooks)
114
- : null;
115
- function isHookWithDeps(node) {
116
- const callee = node.callee;
117
- if (callee.type === 'Identifier') {
118
- if (HOOKS_WITH_DEPS.has(callee.name)) {
119
- return true;
120
- }
121
- if (additionalHooksPattern?.test(callee.name)) {
122
- return true;
123
- }
124
- }
125
- if (callee.type === 'MemberExpression' &&
126
- callee.object.type === 'Identifier' &&
127
- callee.object.name === 'React' &&
128
- callee.property.type === 'Identifier' &&
129
- HOOKS_WITH_DEPS.has(callee.property.name)) {
130
- return true;
131
- }
132
- return false;
133
- }
134
- function getHookName(node) {
135
- const callee = node.callee;
136
- if (callee.type === 'Identifier') {
137
- return callee.name;
138
- }
139
- if (callee.type === 'MemberExpression' &&
140
- callee.property.type === 'Identifier') {
141
- return callee.property.name;
142
- }
143
- return 'unknown';
144
- }
145
- function extractLocallyDeclaredIdentifiers(node) {
146
- const declared = new Set();
147
- const visited = new WeakSet();
148
- function visit(n) {
149
- if (visited.has(n))
150
- return;
151
- visited.add(n);
152
- if (n.type === 'VariableDeclaration') {
153
- for (const declarator of n.declarations) {
154
- if (declarator.id.type === 'Identifier') {
155
- declared.add(declarator.id.name);
156
- }
157
- else if (declarator.id.type === 'ObjectPattern') {
158
- for (const prop of declarator.id.properties) {
159
- if (prop.type === 'Property' && prop.value.type === 'Identifier') {
160
- declared.add(prop.value.name);
161
- }
162
- }
163
- }
164
- else if (declarator.id.type === 'ArrayPattern') {
165
- for (const element of declarator.id.elements) {
166
- if (element && element.type === 'Identifier') {
167
- declared.add(element.name);
168
- }
169
- }
170
- }
171
- }
172
- }
173
- if (n.type === 'FunctionDeclaration' && n.id) {
174
- declared.add(n.id.name);
175
- }
176
- if (n.type === 'ArrowFunctionExpression' ||
177
- n.type === 'FunctionExpression' ||
178
- n.type === 'FunctionDeclaration') {
179
- const collectFromPattern = (param) => {
180
- if (param.type === 'Identifier') {
181
- declared.add(param.name);
182
- }
183
- else if (param.type === 'ObjectPattern') {
184
- for (const prop of param.properties) {
185
- if (prop.type === 'Property' && prop.value.type === 'Identifier') {
186
- declared.add(prop.value.name);
187
- }
188
- else if (prop.type === 'RestElement' && prop.argument.type === 'Identifier') {
189
- declared.add(prop.argument.name);
190
- }
191
- }
192
- }
193
- else if (param.type === 'ArrayPattern') {
194
- for (const el of param.elements) {
195
- if (el && el.type === 'Identifier')
196
- declared.add(el.name);
197
- }
198
- }
199
- else if (param.type === 'RestElement' && param.argument.type === 'Identifier') {
200
- declared.add(param.argument.name);
201
- }
202
- else if (param.type === 'AssignmentPattern' && param.left.type === 'Identifier') {
203
- declared.add(param.left.name);
204
- }
205
- };
206
- for (const param of n.params)
207
- collectFromPattern(param);
208
- }
209
- const keys = getChildKeys(n);
210
- for (let i = 0; i < keys.length; i++) {
211
- const child = n[keys[i]];
212
- if (Array.isArray(child)) {
213
- for (let j = 0; j < child.length; j++) {
214
- const c = child[j];
215
- if (c && typeof c === 'object' && 'type' in c) {
216
- visit(c);
217
- }
218
- }
219
- }
220
- else if (child && typeof child === 'object' && 'type' in child) {
221
- visit(child);
222
- }
223
- }
224
- }
225
- visit(node);
226
- return declared;
227
- }
228
- function extractUsedIdentifiers(node) {
229
- const used = new Set();
230
- const visited = new WeakSet();
231
- function visit(n, isPropertyKey = false) {
232
- if (visited.has(n)) {
233
- return;
234
- }
235
- visited.add(n);
236
- if (n.type === 'MemberExpression') {
237
- visit(n.object, false);
238
- if (n.computed && n.property) {
239
- visit(n.property, false);
240
- }
241
- return;
242
- }
243
- if (n.type === 'CallExpression') {
244
- if (n.callee) {
245
- visit(n.callee, false);
246
- }
247
- if (n.arguments) {
248
- n.arguments.forEach((arg) => visit(arg, false));
249
- }
250
- return;
251
- }
252
- if (n.type === 'Identifier' && !isPropertyKey) {
253
- used.add(n.name);
254
- }
255
- const keys = getChildKeys(n);
256
- for (let i = 0; i < keys.length; i++) {
257
- const child = n[keys[i]];
258
- if (Array.isArray(child)) {
259
- for (let j = 0; j < child.length; j++) {
260
- const c = child[j];
261
- if (c && typeof c === 'object' && 'type' in c) {
262
- visit(c, false);
263
- }
264
- }
265
- }
266
- else if (child && typeof child === 'object' && 'type' in child) {
267
- visit(child, false);
268
- }
269
- }
270
- }
271
- visit(node);
272
- return used;
273
- }
274
- function extractDependencies(depsArray) {
275
- const deps = new Set();
276
- for (const element of depsArray.elements) {
277
- if (element?.type === 'Identifier') {
278
- deps.add(element.name);
279
- }
280
- else if (element?.type === 'MemberExpression' &&
281
- element.object.type === 'Identifier') {
282
- const sourceCode = context.sourceCode;
283
- deps.add(sourceCode.getText(element));
284
- }
285
- }
286
- return deps;
287
- }
288
- function isStableReference(name) {
289
- const stablePatterns = [
290
- /^set[A-Z]/,
291
- /dispatch/i,
292
- /Ref$/,
293
- ];
294
- return stablePatterns.some((pattern) => pattern.test(name));
295
- }
296
- function filterReactiveDeps(used) {
297
- const reactive = new Set();
298
- const nonReactive = new Set([
299
- 'undefined',
300
- 'null',
301
- 'true',
302
- 'false',
303
- 'console',
304
- 'window',
305
- 'document',
306
- 'Math',
307
- 'JSON',
308
- 'Object',
309
- 'Array',
310
- 'String',
311
- 'Number',
312
- 'Boolean',
313
- 'Date',
314
- 'Promise',
315
- 'Error',
316
- 'Map',
317
- 'Set',
318
- 'WeakMap',
319
- 'WeakSet',
320
- 'Symbol',
321
- 'Reflect',
322
- 'Proxy',
323
- 'Intl',
324
- 'navigator',
325
- 'localStorage',
326
- 'sessionStorage',
327
- 'fetch',
328
- 'setTimeout',
329
- 'setInterval',
330
- 'clearTimeout',
331
- 'clearInterval',
332
- 'requestAnimationFrame',
333
- 'cancelAnimationFrame',
334
- ]);
335
- for (const name of used) {
336
- if (!nonReactive.has(name) && !isStableReference(name)) {
337
- reactive.add(name);
338
- }
339
- }
340
- return reactive;
341
- }
342
- return {
343
- CallExpression(node) {
344
- if (!isHookWithDeps(node)) {
345
- return;
346
- }
347
- const hookName = getHookName(node);
348
- const args = node.arguments;
349
- if (args.length === 0) {
350
- return;
351
- }
352
- const callback = args[0];
353
- if (callback.type !== 'ArrowFunctionExpression' &&
354
- callback.type !== 'FunctionExpression') {
355
- return;
356
- }
357
- const depsArg = args[1];
358
- if (!depsArg) {
359
- return;
360
- }
361
- const isTSAsArrayExpression = depsArg.type === 'TSAsExpression' && depsArg.expression?.type === 'ArrayExpression';
362
- const isArrayExpression = depsArg.type === 'ArrayExpression';
363
- if (!isArrayExpression && !isTSAsArrayExpression) {
364
- context.report({
365
- node: depsArg,
366
- messageId: 'depsNotArrayLiteral',
367
- data: { hookName },
368
- });
369
- return;
370
- }
371
- const depsArrayNode = isTSAsArrayExpression
372
- ? depsArg.expression
373
- : depsArg;
374
- const usedInCallback = extractUsedIdentifiers(callback.body);
375
- const locallyDeclared = extractLocallyDeclaredIdentifiers(callback.body);
376
- const externalUsed = new Set();
377
- for (const id of usedInCallback) {
378
- if (!locallyDeclared.has(id)) {
379
- externalUsed.add(id);
380
- }
381
- }
382
- const reactiveDeps = filterReactiveDeps(externalUsed);
383
- const declaredDeps = extractDependencies(depsArrayNode);
384
- const missingDeps = [];
385
- for (const dep of reactiveDeps) {
386
- if (!declaredDeps.has(dep) && /^[a-z]/.test(dep)) {
387
- missingDeps.push(dep);
388
- }
389
- }
390
- const extraDeps = [];
391
- for (const dep of declaredDeps) {
392
- if (!usedInCallback.has(dep) && !dep.includes('.')) {
393
- extraDeps.push(dep);
394
- }
395
- }
396
- if (missingDeps.length > 0) {
397
- context.report({
398
- node: depsArg,
399
- messageId: 'missingDep',
400
- data: {
401
- hookName,
402
- deps: missingDeps.join(', '),
403
- },
404
- suggest: missingDeps.map((dep) => ({
405
- messageId: 'suggestAddDep',
406
- data: { dep },
407
- fix(fixer) {
408
- const lastElement = depsArrayNode.elements[depsArrayNode.elements.length - 1];
409
- if (lastElement) {
410
- return fixer.insertTextAfter(lastElement, `, ${dep}`);
411
- }
412
- else {
413
- return fixer.insertTextAfterRange([depsArrayNode.range[0] + 1, depsArrayNode.range[0] + 1], dep);
414
- }
415
- },
416
- })),
417
- });
418
- }
419
- for (const dep of extraDeps) {
420
- context.report({
421
- node: depsArg,
422
- messageId: 'extraDep',
423
- data: {
424
- hookName,
425
- dep,
426
- },
427
- suggest: [
428
- {
429
- messageId: 'suggestRemoveDep',
430
- data: { dep },
431
- fix(fixer) {
432
- const element = depsArrayNode.elements.find((el) => el?.type === 'Identifier' && el.name === dep);
433
- if (element) {
434
- const index = depsArrayNode.elements.indexOf(element);
435
- const isLast = index === depsArrayNode.elements.length - 1;
436
- const isFirst = index === 0;
437
- if (isFirst && depsArrayNode.elements.length > 1) {
438
- const nextElement = depsArrayNode.elements[1];
439
- if (nextElement) {
440
- return fixer.removeRange([
441
- element.range[0],
442
- nextElement.range[0],
443
- ]);
444
- }
445
- }
446
- else if (isLast && index > 0) {
447
- const prevElement = depsArrayNode.elements[index - 1];
448
- if (prevElement) {
449
- return fixer.removeRange([
450
- prevElement.range[1],
451
- element.range[1],
452
- ]);
453
- }
454
- }
455
- return fixer.remove(element);
456
- }
457
- return null;
458
- },
459
- },
460
- ],
461
- });
462
- }
463
- },
464
- };
465
- },
466
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.hooksExhaustiveDeps=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");const SKIP_KEYS=new Set(["parent","loc","range","tokens","comments","type"]);const CHILD_KEYS_CACHE=new Map;function getChildKeys(node){const cached=CHILD_KEYS_CACHE.get(node.type);if(cached)return cached;const keys=[];for(const key of Object.keys(node)){if(SKIP_KEYS.has(key))continue;const v=node[key];if(v&&typeof v==="object"){keys.push(key)}}CHILD_KEYS_CACHE.set(node.type,keys);return keys}const HOOKS_WITH_DEPS=new Set(["useEffect","useLayoutEffect","useCallback","useMemo","useImperativeHandle"]);exports.hooksExhaustiveDeps=(0,eslint_devkit_1.createRule)({name:"hooks-exhaustive-deps",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/hooks-exhaustive-deps.md",description:"Enforce exhaustive dependencies in React hooks to prevent stale closures",confidence:"high"},hasSuggestions:true,messages:{missingDep:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.WARNING,issueName:"Missing Hook Dependency",description:"React Hook {{hookName}} has missing dependencies: {{deps}}",severity:"HIGH",fix:"Add missing dependencies to the dependency array or memoize values with useMemo/useCallback",documentationLink:"https://react.dev/reference/react/useEffect#specifying-reactive-dependencies"}),extraDep:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.WARNING,issueName:"Unnecessary Hook Dependency",description:"React Hook {{hookName}} has unnecessary dependency: {{dep}}",severity:"MEDIUM",fix:"Remove the unnecessary dependency from the array - it never changes or is not used in the effect",documentationLink:"https://react.dev/reference/react/useEffect#removing-unnecessary-dependencies"}),unnecessaryDep:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.INFO,issueName:"Unnecessary Dependency",description:"The dependency {{dep}} is stable and never changes",severity:"LOW",fix:"Consider removing stable dependencies like setState functions or refs",documentationLink:"https://react.dev/reference/react/useEffect#removing-unnecessary-dependencies"}),suggestAddDep:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.INFO,issueName:"Add Missing Dependency",description:"Add {{dep}} to dependency array",severity:"LOW",fix:"Add the missing dependency to prevent stale closures",documentationLink:"https://react.dev/reference/react/useEffect#specifying-reactive-dependencies"}),suggestRemoveDep:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.INFO,issueName:"Remove Unnecessary Dependency",description:"Remove {{dep}} from dependency array",severity:"LOW",fix:"Remove the unnecessary dependency",documentationLink:"https://react.dev/reference/react/useEffect#removing-unnecessary-dependencies"}),depsNotArrayLiteral:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.WARNING,issueName:"Non-literal Dependency Array",description:"React Hook {{hookName}} was passed a dependency list that is not an array literal. This means we cannot statically verify whether you have passed the correct dependencies.",severity:"HIGH",fix:"Replace the variable with an inline array literal: [dep1, dep2]. If you need dynamic deps, refactor to compute them inside the hook callback instead.",documentationLink:"https://react.dev/reference/react/useEffect#specifying-reactive-dependencies"})},schema:[{type:"object",properties:{additionalHooks:{type:"string",description:'Regex pattern for additional hooks to check (e.g., "useMyCustomHook")'},enableDangerousAutofixThisMayCauseInfiniteLoops:{type:"boolean",default:false,description:"Enable autofix (warning: may cause infinite loops)"}},additionalProperties:false}]},defaultOptions:[{}],create(context){const options=context.options[0]||{};const additionalHooksPattern=options.additionalHooks?new RegExp(options.additionalHooks):null;function isHookWithDeps(node){const callee=node.callee;if(callee.type==="Identifier"){if(HOOKS_WITH_DEPS.has(callee.name)){return true}if(additionalHooksPattern?.test(callee.name)){return true}}if(callee.type==="MemberExpression"&&callee.object.type==="Identifier"&&callee.object.name==="React"&&callee.property.type==="Identifier"&&HOOKS_WITH_DEPS.has(callee.property.name)){return true}return false}function getHookName(node){const callee=node.callee;if(callee.type==="Identifier"){return callee.name}if(callee.type==="MemberExpression"&&callee.property.type==="Identifier"){return callee.property.name}return"unknown"}function extractLocallyDeclaredIdentifiers(node){const declared=new Set;const visited=new WeakSet;function visit(n){if(visited.has(n))return;visited.add(n);if(n.type==="VariableDeclaration"){for(const declarator of n.declarations){if(declarator.id.type==="Identifier"){declared.add(declarator.id.name)}else if(declarator.id.type==="ObjectPattern"){for(const prop of declarator.id.properties){if(prop.type==="Property"&&prop.value.type==="Identifier"){declared.add(prop.value.name)}}}else if(declarator.id.type==="ArrayPattern"){for(const element of declarator.id.elements){if(element&&element.type==="Identifier"){declared.add(element.name)}}}}}if(n.type==="FunctionDeclaration"&&n.id){declared.add(n.id.name)}if(n.type==="ArrowFunctionExpression"||n.type==="FunctionExpression"||n.type==="FunctionDeclaration"){const collectFromPattern=param=>{if(param.type==="Identifier"){declared.add(param.name)}else if(param.type==="ObjectPattern"){for(const prop of param.properties){if(prop.type==="Property"&&prop.value.type==="Identifier"){declared.add(prop.value.name)}else if(prop.type==="RestElement"&&prop.argument.type==="Identifier"){declared.add(prop.argument.name)}}}else if(param.type==="ArrayPattern"){for(const el of param.elements){if(el&&el.type==="Identifier")declared.add(el.name)}}else if(param.type==="RestElement"&&param.argument.type==="Identifier"){declared.add(param.argument.name)}else if(param.type==="AssignmentPattern"&&param.left.type==="Identifier"){declared.add(param.left.name)}};for(const param of n.params)collectFromPattern(param)}const keys=getChildKeys(n);for(let i=0;i<keys.length;i++){const child=n[keys[i]];if(Array.isArray(child)){for(let j=0;j<child.length;j++){const c=child[j];if(c&&typeof c==="object"&&"type"in c){visit(c)}}}else if(child&&typeof child==="object"&&"type"in child){visit(child)}}}visit(node);return declared}function extractUsedIdentifiers(node){const used=new Set;const visited=new WeakSet;function visit(n,isPropertyKey=false){if(visited.has(n)){return}visited.add(n);if(n.type==="MemberExpression"){visit(n.object,false);if(n.computed&&n.property){visit(n.property,false)}return}if(n.type==="CallExpression"){if(n.callee){visit(n.callee,false)}if(n.arguments){n.arguments.forEach(arg=>visit(arg,false))}return}if(n.type==="Identifier"&&!isPropertyKey){used.add(n.name)}const keys=getChildKeys(n);for(let i=0;i<keys.length;i++){const child=n[keys[i]];if(Array.isArray(child)){for(let j=0;j<child.length;j++){const c=child[j];if(c&&typeof c==="object"&&"type"in c){visit(c,false)}}}else if(child&&typeof child==="object"&&"type"in child){visit(child,false)}}}visit(node);return used}function extractDependencies(depsArray){const deps=new Set;for(const element of depsArray.elements){if(element?.type==="Identifier"){deps.add(element.name)}else if(element?.type==="MemberExpression"&&element.object.type==="Identifier"){const sourceCode=context.sourceCode;deps.add(sourceCode.getText(element))}}return deps}function isStableReference(name){const stablePatterns=[/^set[A-Z]/,/dispatch/i,/Ref$/];return stablePatterns.some(pattern=>pattern.test(name))}function filterReactiveDeps(used){const reactive=new Set;const nonReactive=new Set(["undefined","null","true","false","console","window","document","Math","JSON","Object","Array","String","Number","Boolean","Date","Promise","Error","Map","Set","WeakMap","WeakSet","Symbol","Reflect","Proxy","Intl","navigator","localStorage","sessionStorage","fetch","setTimeout","setInterval","clearTimeout","clearInterval","requestAnimationFrame","cancelAnimationFrame"]);for(const name of used){if(!nonReactive.has(name)&&!isStableReference(name)){reactive.add(name)}}return reactive}return{CallExpression(node){if(!isHookWithDeps(node)){return}const hookName=getHookName(node);const args=node.arguments;if(args.length===0){return}const callback=args[0];if(callback.type!=="ArrowFunctionExpression"&&callback.type!=="FunctionExpression"){return}const depsArg=args[1];if(!depsArg){return}const isTSAsArrayExpression=depsArg.type==="TSAsExpression"&&depsArg.expression?.type==="ArrayExpression";const isArrayExpression=depsArg.type==="ArrayExpression";if(!isArrayExpression&&!isTSAsArrayExpression){context.report({node:depsArg,messageId:"depsNotArrayLiteral",data:{hookName}});return}const depsArrayNode=isTSAsArrayExpression?depsArg.expression:depsArg;const usedInCallback=extractUsedIdentifiers(callback.body);const locallyDeclared=extractLocallyDeclaredIdentifiers(callback.body);const externalUsed=new Set;for(const id of usedInCallback){if(!locallyDeclared.has(id)){externalUsed.add(id)}}const reactiveDeps=filterReactiveDeps(externalUsed);const declaredDeps=extractDependencies(depsArrayNode);const missingDeps=[];for(const dep of reactiveDeps){if(!declaredDeps.has(dep)&&/^[a-z]/.test(dep)){missingDeps.push(dep)}}const extraDeps=[];for(const dep of declaredDeps){if(!usedInCallback.has(dep)&&!dep.includes(".")){extraDeps.push(dep)}}if(missingDeps.length>0){context.report({node:depsArg,messageId:"missingDep",data:{hookName,deps:missingDeps.join(", ")},suggest:missingDeps.map(dep=>({messageId:"suggestAddDep",data:{dep},fix(fixer){const lastElement=depsArrayNode.elements[depsArrayNode.elements.length-1];if(lastElement){return fixer.insertTextAfter(lastElement,`, ${dep}`)}else{return fixer.insertTextAfterRange([depsArrayNode.range[0]+1,depsArrayNode.range[0]+1],dep)}}}))})}for(const dep of extraDeps){context.report({node:depsArg,messageId:"extraDep",data:{hookName,dep},suggest:[{messageId:"suggestRemoveDep",data:{dep},fix(fixer){const element=depsArrayNode.elements.find(el=>el?.type==="Identifier"&&el.name===dep);if(element){const index=depsArrayNode.elements.indexOf(element);const isLast=index===depsArrayNode.elements.length-1;const isFirst=index===0;if(isFirst&&depsArrayNode.elements.length>1){const nextElement=depsArrayNode.elements[1];if(nextElement){return fixer.removeRange([element.range[0],nextElement.range[0]])}}else if(isLast&&index>0){const prevElement=depsArrayNode.elements[index-1];if(prevElement){return fixer.removeRange([prevElement.range[1],element.range[1]])}}return fixer.remove(element)}return null}}]})}}}}});