eslint-plugin-react-features 1.1.3 → 1.1.4
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.
- package/CHANGELOG.md +20 -0
- package/README.md +72 -11
- package/package.json +15 -3
- package/src/index.d.ts +375 -6
- package/src/index.js +15 -1
- package/src/oxlint.d.ts +20 -0
- package/src/oxlint.js +21 -0
- package/src/rules/migration/react-class-to-hooks.js +6 -1
- package/src/rules/performance/no-unnecessary-rerenders.js +7 -3
- package/src/rules/performance/react-no-inline-functions.js +3 -0
- package/src/rules/performance/react-render-optimization.js +2 -1
- package/src/rules/react/checked-requires-onchange-or-readonly.js +1 -0
- package/src/rules/react/default-props-match-prop-types.js +1 -0
- package/src/rules/react/display-name.js +1 -0
- package/src/rules/react/hooks-exhaustive-deps.d.ts +1 -1
- package/src/rules/react/hooks-exhaustive-deps.js +136 -47
- package/src/rules/react/jsx-handler-names.js +1 -0
- package/src/rules/react/jsx-key.js +3 -2
- package/src/rules/react/jsx-max-depth.js +1 -0
- package/src/rules/react/jsx-no-bind.js +1 -0
- package/src/rules/react/jsx-no-duplicate-props.js +1 -0
- package/src/rules/react/jsx-no-literals.js +1 -0
- package/src/rules/react/jsx-no-script-url.js +6 -2
- package/src/rules/react/jsx-no-target-blank.js +7 -1
- package/src/rules/react/no-access-state-in-setstate.js +1 -0
- package/src/rules/react/no-adjacent-inline-elements.js +1 -0
- package/src/rules/react/no-arrow-function-lifecycle.js +1 -0
- package/src/rules/react/no-children-prop.js +1 -0
- package/src/rules/react/no-danger-with-children.js +2 -0
- package/src/rules/react/no-danger.js +3 -0
- package/src/rules/react/no-deprecated.js +1 -0
- package/src/rules/react/no-did-mount-set-state.js +1 -0
- package/src/rules/react/no-did-update-set-state.js +1 -0
- package/src/rules/react/no-direct-mutation-state.js +24 -66
- package/src/rules/react/no-find-dom-node.js +1 -0
- package/src/rules/react/no-invalid-html-attribute.js +1 -0
- package/src/rules/react/no-is-mounted.js +1 -0
- package/src/rules/react/no-multi-comp.js +4 -0
- package/src/rules/react/no-namespace.js +1 -0
- package/src/rules/react/no-object-type-as-default-prop.js +1 -0
- package/src/rules/react/no-redundant-should-component-update.js +1 -0
- package/src/rules/react/no-render-return-value.js +1 -0
- package/src/rules/react/no-set-state.js +1 -0
- package/src/rules/react/no-string-refs.js +1 -0
- package/src/rules/react/no-this-in-sfc.js +1 -0
- package/src/rules/react/no-typos.js +1 -0
- package/src/rules/react/no-unescaped-entities.js +1 -0
- package/src/rules/react/no-unknown-property.js +1 -0
- package/src/rules/react/no-unsafe.js +1 -0
- package/src/rules/react/prefer-es6-class.js +1 -0
- package/src/rules/react/prefer-stateless-function.js +8 -3
- package/src/rules/react/prop-types.js +2 -0
- package/src/rules/react/react-in-jsx-scope.js +1 -0
- package/src/rules/react/require-default-props.js +4 -0
- package/src/rules/react/require-optimization.js +1 -0
- package/src/rules/react/require-render-return.js +1 -0
- package/src/rules/react/required-attributes.js +6 -0
- package/src/rules/react/sort-comp.js +17 -13
- package/src/rules/react/state-in-constructor.js +3 -0
- package/src/rules/react/static-property-placement.js +7 -0
- package/src/rules/react/void-dom-elements-no-children.js +2 -0
|
@@ -13,6 +13,7 @@ exports.displayName = (0, eslint_devkit_1.createRule)({
|
|
|
13
13
|
meta: {
|
|
14
14
|
type: 'problem',
|
|
15
15
|
docs: {
|
|
16
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/display-name.md',
|
|
16
17
|
description: 'Enforce component display names',
|
|
17
18
|
},
|
|
18
19
|
schema: [],
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* useEffect, useCallback, useMemo, and useLayoutEffect.
|
|
12
12
|
*/
|
|
13
13
|
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
14
|
-
type MessageIds = 'missingDep' | 'extraDep' | 'unnecessaryDep' | 'suggestAddDep' | 'suggestRemoveDep';
|
|
14
|
+
type MessageIds = 'missingDep' | 'extraDep' | 'unnecessaryDep' | 'suggestAddDep' | 'suggestRemoveDep' | 'depsNotArrayLiteral';
|
|
15
15
|
export interface Options {
|
|
16
16
|
/** Additional hooks to check (e.g., custom hooks using useEffect internally) */
|
|
17
17
|
additionalHooks?: string;
|
|
@@ -8,20 +8,54 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
8
8
|
exports.hooksExhaustiveDeps = void 0;
|
|
9
9
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
10
|
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
11
|
+
/**
|
|
12
|
+
* Per-(node.type) cache of which property keys point at child AST nodes.
|
|
13
|
+
*
|
|
14
|
+
* The previous traversal allocated `Object.keys(node)` for every visited
|
|
15
|
+
* node and filtered out metadata (`loc`, `range`, `parent`, …). On next.js's
|
|
16
|
+
* 14K-file lint pass the same node types recur millions of times — the
|
|
17
|
+
* Object.keys allocation alone showed up as ~4% of the rule's runtime in
|
|
18
|
+
* the v2 profile. Caching by node.type makes the second-and-later visit
|
|
19
|
+
* per type a single map lookup.
|
|
20
|
+
*
|
|
21
|
+
* Module-level so the cache amortizes across files in one ESLint run.
|
|
22
|
+
*/
|
|
23
|
+
const SKIP_KEYS = new Set(['parent', 'loc', 'range', 'tokens', 'comments', 'type']);
|
|
24
|
+
const CHILD_KEYS_CACHE = new Map();
|
|
25
|
+
function getChildKeys(node) {
|
|
26
|
+
const cached = CHILD_KEYS_CACHE.get(node.type);
|
|
27
|
+
if (cached)
|
|
28
|
+
return cached;
|
|
29
|
+
const keys = [];
|
|
30
|
+
for (const key of Object.keys(node)) {
|
|
31
|
+
if (SKIP_KEYS.has(key))
|
|
32
|
+
continue;
|
|
33
|
+
const v = node[key];
|
|
34
|
+
if (v && typeof v === 'object') {
|
|
35
|
+
// Either an AST node or an array of nodes — record either way; the
|
|
36
|
+
// traversal logic at the call site handles both shapes.
|
|
37
|
+
keys.push(key);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
CHILD_KEYS_CACHE.set(node.type, keys);
|
|
41
|
+
return keys;
|
|
42
|
+
}
|
|
11
43
|
// Hook names that require dependency array checking
|
|
12
|
-
const HOOKS_WITH_DEPS = [
|
|
44
|
+
const HOOKS_WITH_DEPS = new Set([
|
|
13
45
|
'useEffect',
|
|
14
46
|
'useLayoutEffect',
|
|
15
47
|
'useCallback',
|
|
16
48
|
'useMemo',
|
|
17
49
|
'useImperativeHandle',
|
|
18
|
-
];
|
|
50
|
+
]);
|
|
19
51
|
exports.hooksExhaustiveDeps = (0, eslint_devkit_1.createRule)({
|
|
20
52
|
name: 'hooks-exhaustive-deps',
|
|
21
53
|
meta: {
|
|
22
54
|
type: 'problem',
|
|
23
55
|
docs: {
|
|
56
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/hooks-exhaustive-deps.md',
|
|
24
57
|
description: 'Enforce exhaustive dependencies in React hooks to prevent stale closures',
|
|
58
|
+
confidence: 'high',
|
|
25
59
|
},
|
|
26
60
|
hasSuggestions: true,
|
|
27
61
|
messages: {
|
|
@@ -65,6 +99,14 @@ exports.hooksExhaustiveDeps = (0, eslint_devkit_1.createRule)({
|
|
|
65
99
|
fix: 'Remove the unnecessary dependency',
|
|
66
100
|
documentationLink: 'https://react.dev/reference/react/useEffect#removing-unnecessary-dependencies',
|
|
67
101
|
}),
|
|
102
|
+
depsNotArrayLiteral: (0, eslint_devkit_2.formatLLMMessage)({
|
|
103
|
+
icon: eslint_devkit_2.MessageIcons.WARNING,
|
|
104
|
+
issueName: 'Non-literal Dependency Array',
|
|
105
|
+
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.',
|
|
106
|
+
severity: 'HIGH',
|
|
107
|
+
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.',
|
|
108
|
+
documentationLink: 'https://react.dev/reference/react/useEffect#specifying-reactive-dependencies',
|
|
109
|
+
}),
|
|
68
110
|
},
|
|
69
111
|
schema: [
|
|
70
112
|
{
|
|
@@ -97,7 +139,7 @@ exports.hooksExhaustiveDeps = (0, eslint_devkit_1.createRule)({
|
|
|
97
139
|
const callee = node.callee;
|
|
98
140
|
// Direct hook call: useEffect(...)
|
|
99
141
|
if (callee.type === 'Identifier') {
|
|
100
|
-
if (HOOKS_WITH_DEPS.
|
|
142
|
+
if (HOOKS_WITH_DEPS.has(callee.name)) {
|
|
101
143
|
return true;
|
|
102
144
|
}
|
|
103
145
|
if (additionalHooksPattern?.test(callee.name)) {
|
|
@@ -109,7 +151,7 @@ exports.hooksExhaustiveDeps = (0, eslint_devkit_1.createRule)({
|
|
|
109
151
|
callee.object.type === 'Identifier' &&
|
|
110
152
|
callee.object.name === 'React' &&
|
|
111
153
|
callee.property.type === 'Identifier' &&
|
|
112
|
-
HOOKS_WITH_DEPS.
|
|
154
|
+
HOOKS_WITH_DEPS.has(callee.property.name)) {
|
|
113
155
|
return true;
|
|
114
156
|
}
|
|
115
157
|
return false;
|
|
@@ -117,6 +159,7 @@ exports.hooksExhaustiveDeps = (0, eslint_devkit_1.createRule)({
|
|
|
117
159
|
/**
|
|
118
160
|
* Get the hook name from a call expression
|
|
119
161
|
*/
|
|
162
|
+
// oxlint-disable-next-line consistent-function-scoping
|
|
120
163
|
function getHookName(node) {
|
|
121
164
|
const callee = node.callee;
|
|
122
165
|
if (callee.type === 'Identifier') {
|
|
@@ -134,7 +177,6 @@ exports.hooksExhaustiveDeps = (0, eslint_devkit_1.createRule)({
|
|
|
134
177
|
function extractLocallyDeclaredIdentifiers(node) {
|
|
135
178
|
const declared = new Set();
|
|
136
179
|
const visited = new WeakSet();
|
|
137
|
-
const skipKeys = new Set(['parent', 'loc', 'range', 'tokens', 'comments']);
|
|
138
180
|
function visit(n) {
|
|
139
181
|
if (visited.has(n))
|
|
140
182
|
return;
|
|
@@ -167,23 +209,59 @@ exports.hooksExhaustiveDeps = (0, eslint_devkit_1.createRule)({
|
|
|
167
209
|
if (n.type === 'FunctionDeclaration' && n.id) {
|
|
168
210
|
declared.add(n.id.name);
|
|
169
211
|
}
|
|
170
|
-
//
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
212
|
+
// Function parameters of nested arrow / function expressions —
|
|
213
|
+
// these are LOCAL to the inner callback, not closures over the
|
|
214
|
+
// hook's body. Without this, callbacks like `.then((r) => ...)`
|
|
215
|
+
// had `r` flagged as a missing dependency (real FP found by the
|
|
216
|
+
// CWE-react-hooks corpus).
|
|
217
|
+
if (n.type === 'ArrowFunctionExpression' ||
|
|
218
|
+
n.type === 'FunctionExpression' ||
|
|
219
|
+
n.type === 'FunctionDeclaration') {
|
|
220
|
+
const collectFromPattern = (param) => {
|
|
221
|
+
if (param.type === 'Identifier') {
|
|
222
|
+
declared.add(param.name);
|
|
223
|
+
}
|
|
224
|
+
else if (param.type === 'ObjectPattern') {
|
|
225
|
+
for (const prop of param.properties) {
|
|
226
|
+
if (prop.type === 'Property' && prop.value.type === 'Identifier') {
|
|
227
|
+
declared.add(prop.value.name);
|
|
228
|
+
}
|
|
229
|
+
else if (prop.type === 'RestElement' && prop.argument.type === 'Identifier') {
|
|
230
|
+
declared.add(prop.argument.name);
|
|
180
231
|
}
|
|
181
|
-
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
else if (param.type === 'ArrayPattern') {
|
|
235
|
+
for (const el of param.elements) {
|
|
236
|
+
if (el && el.type === 'Identifier')
|
|
237
|
+
declared.add(el.name);
|
|
238
|
+
}
|
|
182
239
|
}
|
|
183
|
-
else if (
|
|
184
|
-
|
|
240
|
+
else if (param.type === 'RestElement' && param.argument.type === 'Identifier') {
|
|
241
|
+
declared.add(param.argument.name);
|
|
242
|
+
}
|
|
243
|
+
else if (param.type === 'AssignmentPattern' && param.left.type === 'Identifier') {
|
|
244
|
+
declared.add(param.left.name);
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
for (const param of n.params)
|
|
248
|
+
collectFromPattern(param);
|
|
249
|
+
}
|
|
250
|
+
// Traverse children using the cached child-keys table.
|
|
251
|
+
const keys = getChildKeys(n);
|
|
252
|
+
for (let i = 0; i < keys.length; i++) {
|
|
253
|
+
const child = n[keys[i]];
|
|
254
|
+
if (Array.isArray(child)) {
|
|
255
|
+
for (let j = 0; j < child.length; j++) {
|
|
256
|
+
const c = child[j];
|
|
257
|
+
if (c && typeof c === 'object' && 'type' in c) {
|
|
258
|
+
visit(c);
|
|
259
|
+
}
|
|
185
260
|
}
|
|
186
261
|
}
|
|
262
|
+
else if (child && typeof child === 'object' && 'type' in child) {
|
|
263
|
+
visit(child);
|
|
264
|
+
}
|
|
187
265
|
}
|
|
188
266
|
}
|
|
189
267
|
visit(node);
|
|
@@ -196,7 +274,6 @@ exports.hooksExhaustiveDeps = (0, eslint_devkit_1.createRule)({
|
|
|
196
274
|
const used = new Set();
|
|
197
275
|
const visited = new WeakSet();
|
|
198
276
|
// Keys to skip during traversal (to avoid infinite loops)
|
|
199
|
-
const skipKeys = new Set(['parent', 'loc', 'range', 'tokens', 'comments']);
|
|
200
277
|
function visit(n, isPropertyKey = false) {
|
|
201
278
|
// Prevent infinite loops
|
|
202
279
|
if (visited.has(n)) {
|
|
@@ -227,25 +304,21 @@ exports.hooksExhaustiveDeps = (0, eslint_devkit_1.createRule)({
|
|
|
227
304
|
if (n.type === 'Identifier' && !isPropertyKey) {
|
|
228
305
|
used.add(n.name);
|
|
229
306
|
}
|
|
230
|
-
// Traverse child nodes
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
if (c && typeof c === 'object' && 'type' in c) {
|
|
241
|
-
visit(c, false);
|
|
242
|
-
}
|
|
243
|
-
});
|
|
244
|
-
}
|
|
245
|
-
else if ('type' in child) {
|
|
246
|
-
visit(child, false);
|
|
307
|
+
// Traverse child nodes (cached child-keys table — see getChildKeys).
|
|
308
|
+
const keys = getChildKeys(n);
|
|
309
|
+
for (let i = 0; i < keys.length; i++) {
|
|
310
|
+
const child = n[keys[i]];
|
|
311
|
+
if (Array.isArray(child)) {
|
|
312
|
+
for (let j = 0; j < child.length; j++) {
|
|
313
|
+
const c = child[j];
|
|
314
|
+
if (c && typeof c === 'object' && 'type' in c) {
|
|
315
|
+
visit(c, false);
|
|
316
|
+
}
|
|
247
317
|
}
|
|
248
318
|
}
|
|
319
|
+
else if (child && typeof child === 'object' && 'type' in child) {
|
|
320
|
+
visit(child, false);
|
|
321
|
+
}
|
|
249
322
|
}
|
|
250
323
|
}
|
|
251
324
|
visit(node);
|
|
@@ -263,7 +336,7 @@ exports.hooksExhaustiveDeps = (0, eslint_devkit_1.createRule)({
|
|
|
263
336
|
else if (element?.type === 'MemberExpression' &&
|
|
264
337
|
element.object.type === 'Identifier') {
|
|
265
338
|
// For props.foo, we track the full path
|
|
266
|
-
const sourceCode = context.sourceCode
|
|
339
|
+
const sourceCode = context.sourceCode;
|
|
267
340
|
deps.add(sourceCode.getText(element));
|
|
268
341
|
}
|
|
269
342
|
}
|
|
@@ -272,6 +345,7 @@ exports.hooksExhaustiveDeps = (0, eslint_devkit_1.createRule)({
|
|
|
272
345
|
/**
|
|
273
346
|
* Check if an identifier is a stable React reference
|
|
274
347
|
*/
|
|
348
|
+
// oxlint-disable-next-line consistent-function-scoping
|
|
275
349
|
function isStableReference(name) {
|
|
276
350
|
// setState functions from useState are stable
|
|
277
351
|
// dispatch from useReducer is stable
|
|
@@ -355,9 +429,24 @@ exports.hooksExhaustiveDeps = (0, eslint_devkit_1.createRule)({
|
|
|
355
429
|
if (!depsArg) {
|
|
356
430
|
return;
|
|
357
431
|
}
|
|
358
|
-
|
|
432
|
+
// Mirrors eslint-plugin-react-hooks: handle `as const` casts on the
|
|
433
|
+
// deps array, then report when the deps argument is anything other
|
|
434
|
+
// than an inline array literal (we can't statically verify a variable
|
|
435
|
+
// reference, so the user gets no protection — silent skip would be
|
|
436
|
+
// a missed FN, see CWE-XXX in next.js compiled bundles for examples).
|
|
437
|
+
const isTSAsArrayExpression = depsArg.type === 'TSAsExpression' && depsArg.expression?.type === 'ArrayExpression';
|
|
438
|
+
const isArrayExpression = depsArg.type === 'ArrayExpression';
|
|
439
|
+
if (!isArrayExpression && !isTSAsArrayExpression) {
|
|
440
|
+
context.report({
|
|
441
|
+
node: depsArg,
|
|
442
|
+
messageId: 'depsNotArrayLiteral',
|
|
443
|
+
data: { hookName },
|
|
444
|
+
});
|
|
359
445
|
return;
|
|
360
446
|
}
|
|
447
|
+
const depsArrayNode = isTSAsArrayExpression
|
|
448
|
+
? depsArg.expression
|
|
449
|
+
: depsArg;
|
|
361
450
|
// Extract used identifiers from callback
|
|
362
451
|
const usedInCallback = extractUsedIdentifiers(callback.body);
|
|
363
452
|
// Extract locally declared identifiers (should not be dependencies)
|
|
@@ -371,7 +460,7 @@ exports.hooksExhaustiveDeps = (0, eslint_devkit_1.createRule)({
|
|
|
371
460
|
}
|
|
372
461
|
const reactiveDeps = filterReactiveDeps(externalUsed);
|
|
373
462
|
// Extract declared dependencies
|
|
374
|
-
const declaredDeps = extractDependencies(
|
|
463
|
+
const declaredDeps = extractDependencies(depsArrayNode);
|
|
375
464
|
// Find missing dependencies
|
|
376
465
|
const missingDeps = [];
|
|
377
466
|
for (const dep of reactiveDeps) {
|
|
@@ -401,13 +490,13 @@ exports.hooksExhaustiveDeps = (0, eslint_devkit_1.createRule)({
|
|
|
401
490
|
messageId: 'suggestAddDep',
|
|
402
491
|
data: { dep },
|
|
403
492
|
fix(fixer) {
|
|
404
|
-
const lastElement =
|
|
493
|
+
const lastElement = depsArrayNode.elements[depsArrayNode.elements.length - 1];
|
|
405
494
|
if (lastElement) {
|
|
406
495
|
return fixer.insertTextAfter(lastElement, `, ${dep}`);
|
|
407
496
|
}
|
|
408
497
|
else {
|
|
409
498
|
// Empty array
|
|
410
|
-
return fixer.insertTextAfterRange([
|
|
499
|
+
return fixer.insertTextAfterRange([depsArrayNode.range[0] + 1, depsArrayNode.range[0] + 1], dep);
|
|
411
500
|
}
|
|
412
501
|
},
|
|
413
502
|
})),
|
|
@@ -427,14 +516,14 @@ exports.hooksExhaustiveDeps = (0, eslint_devkit_1.createRule)({
|
|
|
427
516
|
messageId: 'suggestRemoveDep',
|
|
428
517
|
data: { dep },
|
|
429
518
|
fix(fixer) {
|
|
430
|
-
const element =
|
|
519
|
+
const element = depsArrayNode.elements.find((el) => el?.type === 'Identifier' && el.name === dep);
|
|
431
520
|
if (element) {
|
|
432
|
-
const index =
|
|
433
|
-
const isLast = index ===
|
|
521
|
+
const index = depsArrayNode.elements.indexOf(element);
|
|
522
|
+
const isLast = index === depsArrayNode.elements.length - 1;
|
|
434
523
|
const isFirst = index === 0;
|
|
435
|
-
if (isFirst &&
|
|
524
|
+
if (isFirst && depsArrayNode.elements.length > 1) {
|
|
436
525
|
// Remove first element and following comma
|
|
437
|
-
const nextElement =
|
|
526
|
+
const nextElement = depsArrayNode.elements[1];
|
|
438
527
|
if (nextElement) {
|
|
439
528
|
return fixer.removeRange([
|
|
440
529
|
element.range[0],
|
|
@@ -444,7 +533,7 @@ exports.hooksExhaustiveDeps = (0, eslint_devkit_1.createRule)({
|
|
|
444
533
|
}
|
|
445
534
|
else if (isLast && index > 0) {
|
|
446
535
|
// Remove last element and preceding comma
|
|
447
|
-
const prevElement =
|
|
536
|
+
const prevElement = depsArrayNode.elements[index - 1];
|
|
448
537
|
if (prevElement) {
|
|
449
538
|
return fixer.removeRange([
|
|
450
539
|
prevElement.range[1],
|
|
@@ -13,6 +13,7 @@ exports.jsxHandlerNames = (0, eslint_devkit_1.createRule)({
|
|
|
13
13
|
meta: {
|
|
14
14
|
type: 'problem',
|
|
15
15
|
docs: {
|
|
16
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/jsx-handler-names.md',
|
|
16
17
|
description: 'Enforce event handler naming conventions',
|
|
17
18
|
},
|
|
18
19
|
schema: [],
|
|
@@ -13,6 +13,7 @@ exports.jsxKey = (0, eslint_devkit_1.createRule)({
|
|
|
13
13
|
meta: {
|
|
14
14
|
type: 'problem',
|
|
15
15
|
docs: {
|
|
16
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/jsx-key.md',
|
|
16
17
|
description: 'Detect missing or problematic React keys that could break reconciliation',
|
|
17
18
|
},
|
|
18
19
|
hasSuggestions: true,
|
|
@@ -83,7 +84,7 @@ exports.jsxKey = (0, eslint_devkit_1.createRule)({
|
|
|
83
84
|
/**
|
|
84
85
|
* Array iterator methods that produce lists requiring keys
|
|
85
86
|
*/
|
|
86
|
-
const ITERATOR_METHODS = ['map', 'forEach', 'flatMap'];
|
|
87
|
+
const ITERATOR_METHODS = new Set(['map', 'forEach', 'flatMap']);
|
|
87
88
|
/**
|
|
88
89
|
* Check if a CallExpression is an iterator call that produces a list
|
|
89
90
|
* Handles:
|
|
@@ -100,7 +101,7 @@ exports.jsxKey = (0, eslint_devkit_1.createRule)({
|
|
|
100
101
|
// Pattern 1: array.map(), array.forEach(), array.flatMap()
|
|
101
102
|
if (callee.type === 'MemberExpression' &&
|
|
102
103
|
callee.property.type === 'Identifier' &&
|
|
103
|
-
ITERATOR_METHODS.
|
|
104
|
+
ITERATOR_METHODS.has(callee.property.name)) {
|
|
104
105
|
return true;
|
|
105
106
|
}
|
|
106
107
|
// Pattern 2: Array.from(items, mapFn) - only when 2nd argument is a mapping function
|
|
@@ -13,6 +13,7 @@ exports.jsxMaxDepth = (0, eslint_devkit_1.createRule)({
|
|
|
13
13
|
meta: {
|
|
14
14
|
type: 'problem',
|
|
15
15
|
docs: {
|
|
16
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/jsx-max-depth.md',
|
|
16
17
|
description: 'Limit JSX nesting depth',
|
|
17
18
|
},
|
|
18
19
|
messages: {
|
|
@@ -13,6 +13,7 @@ exports.jsxNoBind = (0, eslint_devkit_1.createRule)({
|
|
|
13
13
|
meta: {
|
|
14
14
|
type: 'problem',
|
|
15
15
|
docs: {
|
|
16
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/jsx-no-bind.md',
|
|
16
17
|
description: 'Prevent .bind() in JSX',
|
|
17
18
|
},
|
|
18
19
|
schema: [],
|
|
@@ -13,6 +13,7 @@ exports.jsxNoDuplicateProps = (0, eslint_devkit_2.createRule)({
|
|
|
13
13
|
meta: {
|
|
14
14
|
type: 'problem',
|
|
15
15
|
docs: {
|
|
16
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/jsx-no-duplicate-props.md',
|
|
16
17
|
description: 'Prevent duplicate props in JSX',
|
|
17
18
|
},
|
|
18
19
|
messages: {
|
|
@@ -13,6 +13,7 @@ exports.jsxNoLiterals = (0, eslint_devkit_1.createRule)({
|
|
|
13
13
|
meta: {
|
|
14
14
|
type: 'problem',
|
|
15
15
|
docs: {
|
|
16
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/jsx-no-literals.md',
|
|
16
17
|
description: 'Prevent string literals in JSX',
|
|
17
18
|
},
|
|
18
19
|
messages: {
|
|
@@ -9,13 +9,16 @@ exports.jsxNoScriptUrl = void 0;
|
|
|
9
9
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
10
|
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
11
11
|
// Attributes that commonly accept URLs
|
|
12
|
-
const URL_PROPS = ['href', 'src', 'action', 'formAction', 'data', 'poster', 'background'];
|
|
12
|
+
const URL_PROPS = new Set(['href', 'src', 'action', 'formAction', 'data', 'poster', 'background']);
|
|
13
13
|
exports.jsxNoScriptUrl = (0, eslint_devkit_2.createRule)({
|
|
14
14
|
name: 'jsx-no-script-url',
|
|
15
15
|
meta: {
|
|
16
16
|
type: 'problem',
|
|
17
17
|
docs: {
|
|
18
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/jsx-no-script-url.md',
|
|
18
19
|
description: 'Forbid javascript: URLs',
|
|
20
|
+
cwe: 'CWE-79',
|
|
21
|
+
cvss: 9.5,
|
|
19
22
|
},
|
|
20
23
|
messages: {
|
|
21
24
|
noScriptUrl: (0, eslint_devkit_1.formatLLMMessage)({
|
|
@@ -58,6 +61,7 @@ exports.jsxNoScriptUrl = (0, eslint_devkit_2.createRule)({
|
|
|
58
61
|
for (const config of componentConfigs) {
|
|
59
62
|
componentPropsMap.set(config.name, new Set(config.props));
|
|
60
63
|
}
|
|
64
|
+
// oxlint-disable-next-line consistent-function-scoping
|
|
61
65
|
function isJavaScriptUrl(value) {
|
|
62
66
|
// Match javascript: protocol (case insensitive, with optional whitespace)
|
|
63
67
|
return /^\s*javascript:/i.test(value);
|
|
@@ -67,7 +71,7 @@ exports.jsxNoScriptUrl = (0, eslint_devkit_2.createRule)({
|
|
|
67
71
|
return;
|
|
68
72
|
const propName = node.name.name;
|
|
69
73
|
// Check if this is a URL prop for standard elements
|
|
70
|
-
const isUrlProp = URL_PROPS.
|
|
74
|
+
const isUrlProp = URL_PROPS.has(propName);
|
|
71
75
|
// Check if this is a configured custom component prop
|
|
72
76
|
const customProps = componentPropsMap.get(elementName);
|
|
73
77
|
const isCustomProp = customProps?.has(propName);
|
|
@@ -13,7 +13,10 @@ exports.jsxNoTargetBlank = (0, eslint_devkit_2.createRule)({
|
|
|
13
13
|
meta: {
|
|
14
14
|
type: 'problem',
|
|
15
15
|
docs: {
|
|
16
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/jsx-no-target-blank.md',
|
|
16
17
|
description: 'Forbid target="_blank" without rel="noopener noreferrer"',
|
|
18
|
+
cwe: 'CWE-1022',
|
|
19
|
+
cvss: 7.5,
|
|
17
20
|
},
|
|
18
21
|
fixable: 'code',
|
|
19
22
|
messages: {
|
|
@@ -86,7 +89,7 @@ exports.jsxNoTargetBlank = (0, eslint_devkit_2.createRule)({
|
|
|
86
89
|
if (hrefAttr.value.type === 'Literal' && typeof hrefAttr.value.value === 'string') {
|
|
87
90
|
const href = hrefAttr.value.value;
|
|
88
91
|
// External if starts with http:// or https:// or //
|
|
89
|
-
return /^(https?:)?\/\//.test(href) ||
|
|
92
|
+
return /^(https?:)?\/\//.test(href) || href.startsWith('//');
|
|
90
93
|
}
|
|
91
94
|
// Dynamic href - assume external
|
|
92
95
|
return enforceDynamicLinks === 'always';
|
|
@@ -96,6 +99,7 @@ exports.jsxNoTargetBlank = (0, eslint_devkit_2.createRule)({
|
|
|
96
99
|
attr.name.type === 'JSXIdentifier' &&
|
|
97
100
|
attr.name.name === 'rel');
|
|
98
101
|
}
|
|
102
|
+
// oxlint-disable-next-line consistent-function-scoping
|
|
99
103
|
function hasNoopener(relAttr) {
|
|
100
104
|
if (!relAttr.value)
|
|
101
105
|
return false;
|
|
@@ -105,6 +109,7 @@ exports.jsxNoTargetBlank = (0, eslint_devkit_2.createRule)({
|
|
|
105
109
|
}
|
|
106
110
|
return false;
|
|
107
111
|
}
|
|
112
|
+
// oxlint-disable-next-line consistent-function-scoping
|
|
108
113
|
function hasNoreferrer(relAttr) {
|
|
109
114
|
if (!relAttr.value)
|
|
110
115
|
return false;
|
|
@@ -114,6 +119,7 @@ exports.jsxNoTargetBlank = (0, eslint_devkit_2.createRule)({
|
|
|
114
119
|
}
|
|
115
120
|
return false;
|
|
116
121
|
}
|
|
122
|
+
// oxlint-disable-next-line consistent-function-scoping
|
|
117
123
|
function hasSpreadAttribute(node) {
|
|
118
124
|
return node.attributes.some(attr => attr.type === 'JSXSpreadAttribute');
|
|
119
125
|
}
|
|
@@ -13,6 +13,7 @@ exports.noAccessStateInSetState = (0, eslint_devkit_1.createRule)({
|
|
|
13
13
|
meta: {
|
|
14
14
|
type: 'problem',
|
|
15
15
|
docs: {
|
|
16
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/no-access-state-in-setstate.md',
|
|
16
17
|
description: 'Disallow accessing this.state inside setState calls',
|
|
17
18
|
},
|
|
18
19
|
schema: [],
|
|
@@ -18,6 +18,7 @@ exports.noAdjacentInlineElements = (0, eslint_devkit_1.createRule)({
|
|
|
18
18
|
meta: {
|
|
19
19
|
type: 'problem',
|
|
20
20
|
docs: {
|
|
21
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/no-adjacent-inline-elements.md',
|
|
21
22
|
description: 'Prevent adjacent inline elements',
|
|
22
23
|
},
|
|
23
24
|
schema: [],
|
|
@@ -28,6 +28,7 @@ exports.noArrowFunctionLifecycle = (0, eslint_devkit_1.createRule)({
|
|
|
28
28
|
meta: {
|
|
29
29
|
type: 'problem',
|
|
30
30
|
docs: {
|
|
31
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/no-arrow-function-lifecycle.md',
|
|
31
32
|
description: 'Prevent arrow functions in lifecycle methods',
|
|
32
33
|
},
|
|
33
34
|
messages: {
|
|
@@ -13,6 +13,7 @@ exports.noChildrenProp = (0, eslint_devkit_1.createRule)({
|
|
|
13
13
|
meta: {
|
|
14
14
|
type: 'problem',
|
|
15
15
|
docs: {
|
|
16
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/no-children-prop.md',
|
|
16
17
|
description: 'Disallow passing children as props',
|
|
17
18
|
},
|
|
18
19
|
schema: [],
|
|
@@ -13,6 +13,7 @@ exports.noDangerWithChildren = (0, eslint_devkit_2.createRule)({
|
|
|
13
13
|
meta: {
|
|
14
14
|
type: 'problem',
|
|
15
15
|
docs: {
|
|
16
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/no-danger-with-children.md',
|
|
16
17
|
description: 'Prevent using both children and dangerouslySetInnerHTML',
|
|
17
18
|
},
|
|
18
19
|
messages: {
|
|
@@ -39,6 +40,7 @@ exports.noDangerWithChildren = (0, eslint_devkit_2.createRule)({
|
|
|
39
40
|
attr.name.type === 'JSXIdentifier' &&
|
|
40
41
|
attr.name.name === 'children');
|
|
41
42
|
}
|
|
43
|
+
// oxlint-disable-next-line consistent-function-scoping
|
|
42
44
|
function hasJSXChildren(node) {
|
|
43
45
|
// Check if there are any meaningful children
|
|
44
46
|
return node.children.some(child => {
|
|
@@ -13,7 +13,10 @@ exports.noDanger = (0, eslint_devkit_1.createRule)({
|
|
|
13
13
|
meta: {
|
|
14
14
|
type: 'problem',
|
|
15
15
|
docs: {
|
|
16
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/no-danger.md',
|
|
16
17
|
description: 'Disallow dangerouslySetInnerHTML usage',
|
|
18
|
+
cwe: 'CWE-79',
|
|
19
|
+
cvss: 9.5,
|
|
17
20
|
},
|
|
18
21
|
schema: [],
|
|
19
22
|
messages: {
|
|
@@ -32,6 +32,7 @@ exports.noDeprecated = (0, eslint_devkit_2.createRule)({
|
|
|
32
32
|
meta: {
|
|
33
33
|
type: 'problem',
|
|
34
34
|
docs: {
|
|
35
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/no-deprecated.md',
|
|
35
36
|
description: 'Prevent using deprecated React methods',
|
|
36
37
|
},
|
|
37
38
|
messages: {
|
|
@@ -13,6 +13,7 @@ exports.noDidMountSetState = (0, eslint_devkit_1.createRule)({
|
|
|
13
13
|
meta: {
|
|
14
14
|
type: 'problem',
|
|
15
15
|
docs: {
|
|
16
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/no-did-mount-set-state.md',
|
|
16
17
|
description: 'Prevent setState in componentDidMount',
|
|
17
18
|
},
|
|
18
19
|
schema: [],
|
|
@@ -13,6 +13,7 @@ exports.noDidUpdateSetState = (0, eslint_devkit_1.createRule)({
|
|
|
13
13
|
meta: {
|
|
14
14
|
type: 'problem',
|
|
15
15
|
docs: {
|
|
16
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/no-did-update-set-state.md',
|
|
16
17
|
description: 'Prevent setState in componentDidUpdate',
|
|
17
18
|
},
|
|
18
19
|
messages: {
|