eslint-plugin-react-features 1.1.0
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 +70 -0
- package/LICENSE +23 -0
- package/README.md +155 -0
- package/package.json +65 -0
- package/src/index.d.ts +1093 -0
- package/src/index.js +212 -0
- package/src/rules/migration/react-class-to-hooks.d.ts +22 -0
- package/src/rules/migration/react-class-to-hooks.js +178 -0
- package/src/rules/performance/no-unnecessary-rerenders.d.ts +26 -0
- package/src/rules/performance/no-unnecessary-rerenders.js +212 -0
- package/src/rules/performance/react-no-inline-functions.d.ts +24 -0
- package/src/rules/performance/react-no-inline-functions.js +211 -0
- package/src/rules/performance/react-render-optimization.d.ts +31 -0
- package/src/rules/performance/react-render-optimization.js +294 -0
- package/src/rules/react/checked-requires-onchange-or-readonly.d.ts +16 -0
- package/src/rules/react/checked-requires-onchange-or-readonly.js +74 -0
- package/src/rules/react/default-props-match-prop-types.d.ts +13 -0
- package/src/rules/react/default-props-match-prop-types.js +119 -0
- package/src/rules/react/display-name.d.ts +13 -0
- package/src/rules/react/display-name.js +164 -0
- package/src/rules/react/hooks-exhaustive-deps.d.ts +25 -0
- package/src/rules/react/hooks-exhaustive-deps.js +466 -0
- package/src/rules/react/jsx-handler-names.d.ts +13 -0
- package/src/rules/react/jsx-handler-names.js +57 -0
- package/src/rules/react/jsx-key.d.ts +20 -0
- package/src/rules/react/jsx-key.js +421 -0
- package/src/rules/react/jsx-max-depth.d.ts +19 -0
- package/src/rules/react/jsx-max-depth.js +80 -0
- package/src/rules/react/jsx-no-bind.d.ts +13 -0
- package/src/rules/react/jsx-no-bind.js +64 -0
- package/src/rules/react/jsx-no-duplicate-props.d.ts +21 -0
- package/src/rules/react/jsx-no-duplicate-props.js +70 -0
- package/src/rules/react/jsx-no-literals.d.ts +20 -0
- package/src/rules/react/jsx-no-literals.js +83 -0
- package/src/rules/react/jsx-no-script-url.d.ts +25 -0
- package/src/rules/react/jsx-no-script-url.js +131 -0
- package/src/rules/react/jsx-no-target-blank.d.ts +26 -0
- package/src/rules/react/jsx-no-target-blank.js +176 -0
- package/src/rules/react/no-access-state-in-setstate.d.ts +13 -0
- package/src/rules/react/no-access-state-in-setstate.js +92 -0
- package/src/rules/react/no-adjacent-inline-elements.d.ts +13 -0
- package/src/rules/react/no-adjacent-inline-elements.js +81 -0
- package/src/rules/react/no-arrow-function-lifecycle.d.ts +13 -0
- package/src/rules/react/no-arrow-function-lifecycle.js +63 -0
- package/src/rules/react/no-children-prop.d.ts +13 -0
- package/src/rules/react/no-children-prop.js +60 -0
- package/src/rules/react/no-danger-with-children.d.ts +16 -0
- package/src/rules/react/no-danger-with-children.js +110 -0
- package/src/rules/react/no-danger.d.ts +13 -0
- package/src/rules/react/no-danger.js +45 -0
- package/src/rules/react/no-deprecated.d.ts +16 -0
- package/src/rules/react/no-deprecated.js +140 -0
- package/src/rules/react/no-did-mount-set-state.d.ts +13 -0
- package/src/rules/react/no-did-mount-set-state.js +55 -0
- package/src/rules/react/no-did-update-set-state.d.ts +19 -0
- package/src/rules/react/no-did-update-set-state.js +80 -0
- package/src/rules/react/no-direct-mutation-state.d.ts +20 -0
- package/src/rules/react/no-direct-mutation-state.js +277 -0
- package/src/rules/react/no-find-dom-node.d.ts +15 -0
- package/src/rules/react/no-find-dom-node.js +56 -0
- package/src/rules/react/no-invalid-html-attribute.d.ts +13 -0
- package/src/rules/react/no-invalid-html-attribute.js +84 -0
- package/src/rules/react/no-is-mounted.d.ts +13 -0
- package/src/rules/react/no-is-mounted.js +52 -0
- package/src/rules/react/no-multi-comp.d.ts +18 -0
- package/src/rules/react/no-multi-comp.js +160 -0
- package/src/rules/react/no-namespace.d.ts +13 -0
- package/src/rules/react/no-namespace.js +41 -0
- package/src/rules/react/no-object-type-as-default-prop.d.ts +13 -0
- package/src/rules/react/no-object-type-as-default-prop.js +61 -0
- package/src/rules/react/no-redundant-should-component-update.d.ts +13 -0
- package/src/rules/react/no-redundant-should-component-update.js +55 -0
- package/src/rules/react/no-render-return-value.d.ts +13 -0
- package/src/rules/react/no-render-return-value.js +74 -0
- package/src/rules/react/no-set-state.d.ts +13 -0
- package/src/rules/react/no-set-state.js +50 -0
- package/src/rules/react/no-string-refs.d.ts +13 -0
- package/src/rules/react/no-string-refs.js +68 -0
- package/src/rules/react/no-this-in-sfc.d.ts +13 -0
- package/src/rules/react/no-this-in-sfc.js +65 -0
- package/src/rules/react/no-typos.d.ts +13 -0
- package/src/rules/react/no-typos.js +97 -0
- package/src/rules/react/no-unescaped-entities.d.ts +13 -0
- package/src/rules/react/no-unescaped-entities.js +50 -0
- package/src/rules/react/no-unknown-property.d.ts +13 -0
- package/src/rules/react/no-unknown-property.js +130 -0
- package/src/rules/react/no-unsafe.d.ts +20 -0
- package/src/rules/react/no-unsafe.js +112 -0
- package/src/rules/react/prefer-es6-class.d.ts +13 -0
- package/src/rules/react/prefer-es6-class.js +56 -0
- package/src/rules/react/prefer-stateless-function.d.ts +16 -0
- package/src/rules/react/prefer-stateless-function.js +134 -0
- package/src/rules/react/prop-types.d.ts +18 -0
- package/src/rules/react/prop-types.js +153 -0
- package/src/rules/react/react-in-jsx-scope.d.ts +13 -0
- package/src/rules/react/react-in-jsx-scope.js +94 -0
- package/src/rules/react/require-default-props.d.ts +16 -0
- package/src/rules/react/require-default-props.js +146 -0
- package/src/rules/react/require-optimization.d.ts +26 -0
- package/src/rules/react/require-optimization.js +436 -0
- package/src/rules/react/require-render-return.d.ts +13 -0
- package/src/rules/react/require-render-return.js +87 -0
- package/src/rules/react/required-attributes.d.ts +28 -0
- package/src/rules/react/required-attributes.js +212 -0
- package/src/rules/react/sort-comp.d.ts +17 -0
- package/src/rules/react/sort-comp.js +160 -0
- package/src/rules/react/state-in-constructor.d.ts +13 -0
- package/src/rules/react/state-in-constructor.js +103 -0
- package/src/rules/react/static-property-placement.d.ts +20 -0
- package/src/rules/react/static-property-placement.js +166 -0
- package/src/rules/react/void-dom-elements-no-children.d.ts +15 -0
- package/src/rules/react/void-dom-elements-no-children.js +94 -0
- package/src/types/index.d.ts +54 -0
- package/src/types/index.js +7 -0
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) 2025 Ofri Peretz
|
|
4
|
+
* Licensed under the MIT License. Use of this source code is governed by the
|
|
5
|
+
* MIT license that can be found in the LICENSE file.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.jsxKey = void 0;
|
|
9
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
+
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
11
|
+
exports.jsxKey = (0, eslint_devkit_1.createRule)({
|
|
12
|
+
name: 'jsx-key',
|
|
13
|
+
meta: {
|
|
14
|
+
type: 'problem',
|
|
15
|
+
docs: {
|
|
16
|
+
description: 'Detect missing or problematic React keys that could break reconciliation',
|
|
17
|
+
},
|
|
18
|
+
hasSuggestions: true,
|
|
19
|
+
messages: {
|
|
20
|
+
missingKey: (0, eslint_devkit_2.formatLLMMessage)({
|
|
21
|
+
icon: eslint_devkit_2.MessageIcons.WARNING,
|
|
22
|
+
issueName: 'Missing React Key',
|
|
23
|
+
description: 'Missing key prop for element in iterator',
|
|
24
|
+
severity: 'HIGH',
|
|
25
|
+
fix: 'Add unique, stable key prop based on item data',
|
|
26
|
+
documentationLink: 'https://react.dev/learn/render-and-commit#lists-and-keys',
|
|
27
|
+
}),
|
|
28
|
+
invalidKey: (0, eslint_devkit_2.formatLLMMessage)({
|
|
29
|
+
icon: eslint_devkit_2.MessageIcons.WARNING,
|
|
30
|
+
issueName: 'Invalid React Key',
|
|
31
|
+
description: 'Key prop should be unique and stable',
|
|
32
|
+
severity: 'MEDIUM',
|
|
33
|
+
fix: 'Use item.id, item.key, or index as last resort',
|
|
34
|
+
documentationLink: 'https://react.dev/learn/render-and-commit#lists-and-keys',
|
|
35
|
+
}),
|
|
36
|
+
duplicateKey: (0, eslint_devkit_2.formatLLMMessage)({
|
|
37
|
+
icon: eslint_devkit_2.MessageIcons.WARNING,
|
|
38
|
+
issueName: 'Duplicate React Key',
|
|
39
|
+
description: 'Duplicate key values can break reconciliation',
|
|
40
|
+
severity: 'HIGH',
|
|
41
|
+
fix: 'Ensure all keys in the array are unique',
|
|
42
|
+
documentationLink: 'https://react.dev/learn/render-and-commit#lists-and-keys',
|
|
43
|
+
}),
|
|
44
|
+
unstableKey: (0, eslint_devkit_2.formatLLMMessage)({
|
|
45
|
+
icon: eslint_devkit_2.MessageIcons.WARNING,
|
|
46
|
+
issueName: 'Unstable React Key',
|
|
47
|
+
description: 'Using index as key can cause reconciliation issues',
|
|
48
|
+
severity: 'MEDIUM',
|
|
49
|
+
fix: 'Use stable identifier from data instead of array index',
|
|
50
|
+
documentationLink: 'https://react.dev/learn/render-and-commit#lists-and-keys',
|
|
51
|
+
}),
|
|
52
|
+
suggestKey: (0, eslint_devkit_2.formatLLMMessage)({
|
|
53
|
+
icon: eslint_devkit_2.MessageIcons.INFO,
|
|
54
|
+
issueName: 'Add Key Prop',
|
|
55
|
+
description: 'Add unique key prop to element',
|
|
56
|
+
severity: 'LOW',
|
|
57
|
+
fix: 'key={item.id}',
|
|
58
|
+
documentationLink: 'https://react.dev/learn/render-and-commit#lists-and-keys',
|
|
59
|
+
}),
|
|
60
|
+
},
|
|
61
|
+
schema: [
|
|
62
|
+
{
|
|
63
|
+
type: 'object',
|
|
64
|
+
properties: {
|
|
65
|
+
warnUnstableKeys: {
|
|
66
|
+
type: 'boolean',
|
|
67
|
+
default: true,
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
additionalProperties: false,
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
},
|
|
74
|
+
defaultOptions: [{ warnUnstableKeys: true }],
|
|
75
|
+
create(context) {
|
|
76
|
+
const [options] = context.options;
|
|
77
|
+
const { warnUnstableKeys = true } = options || {};
|
|
78
|
+
// Track keys in current iteration context
|
|
79
|
+
const keyTracker = new Map();
|
|
80
|
+
function resetKeyTracker() {
|
|
81
|
+
keyTracker.clear();
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Array iterator methods that produce lists requiring keys
|
|
85
|
+
*/
|
|
86
|
+
const ITERATOR_METHODS = ['map', 'forEach', 'flatMap'];
|
|
87
|
+
/**
|
|
88
|
+
* Check if a CallExpression is an iterator call that produces a list
|
|
89
|
+
* Handles:
|
|
90
|
+
* - array.map(), array.forEach(), array.flatMap()
|
|
91
|
+
* - Array.from(items, mapFn)
|
|
92
|
+
* - React.Children.map(), Children.map()
|
|
93
|
+
* - [...items].map() (spread then map)
|
|
94
|
+
*/
|
|
95
|
+
function isIteratorCall(node) {
|
|
96
|
+
if (node.type !== 'CallExpression') {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
const { callee } = node;
|
|
100
|
+
// Pattern 1: array.map(), array.forEach(), array.flatMap()
|
|
101
|
+
if (callee.type === 'MemberExpression' &&
|
|
102
|
+
callee.property.type === 'Identifier' &&
|
|
103
|
+
ITERATOR_METHODS.includes(callee.property.name)) {
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
// Pattern 2: Array.from(items, mapFn) - only when 2nd argument is a mapping function
|
|
107
|
+
if (callee.type === 'MemberExpression' &&
|
|
108
|
+
callee.object.type === 'Identifier' &&
|
|
109
|
+
callee.object.name === 'Array' &&
|
|
110
|
+
callee.property.type === 'Identifier' &&
|
|
111
|
+
callee.property.name === 'from' &&
|
|
112
|
+
node.arguments.length >= 2) {
|
|
113
|
+
const mapFn = node.arguments[1];
|
|
114
|
+
// Check if second argument is a function (arrow or regular)
|
|
115
|
+
if (mapFn.type === 'ArrowFunctionExpression' ||
|
|
116
|
+
mapFn.type === 'FunctionExpression') {
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
// Pattern 3: React.Children.map(children, fn) or Children.map(children, fn)
|
|
121
|
+
if (callee.type === 'MemberExpression' &&
|
|
122
|
+
callee.property.type === 'Identifier' &&
|
|
123
|
+
callee.property.name === 'map') {
|
|
124
|
+
const obj = callee.object;
|
|
125
|
+
// Children.map()
|
|
126
|
+
if (obj.type === 'Identifier' && obj.name === 'Children') {
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
// React.Children.map()
|
|
130
|
+
if (obj.type === 'MemberExpression' &&
|
|
131
|
+
obj.object.type === 'Identifier' &&
|
|
132
|
+
obj.object.name === 'React' &&
|
|
133
|
+
obj.property.type === 'Identifier' &&
|
|
134
|
+
obj.property.name === 'Children') {
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Check if node is inside Array.from's mapping function (2nd argument)
|
|
142
|
+
*/
|
|
143
|
+
function isInsideArrayFromMapFn(node) {
|
|
144
|
+
let current = node;
|
|
145
|
+
while (current?.parent) {
|
|
146
|
+
const parent = current.parent;
|
|
147
|
+
// Found a function - check if it's Array.from's 2nd argument
|
|
148
|
+
if (parent.type === 'ArrowFunctionExpression' ||
|
|
149
|
+
parent.type === 'FunctionExpression') {
|
|
150
|
+
const grandParent = parent.parent;
|
|
151
|
+
if (grandParent?.type === 'CallExpression' &&
|
|
152
|
+
grandParent.callee.type === 'MemberExpression' &&
|
|
153
|
+
grandParent.callee.object.type === 'Identifier' &&
|
|
154
|
+
grandParent.callee.object.name === 'Array' &&
|
|
155
|
+
grandParent.callee.property.type === 'Identifier' &&
|
|
156
|
+
grandParent.callee.property.name === 'from' &&
|
|
157
|
+
grandParent.arguments[1] === parent) {
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
current = parent;
|
|
162
|
+
}
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Check if node is inside Children.map or React.Children.map
|
|
167
|
+
*/
|
|
168
|
+
function isInsideChildrenMap(node) {
|
|
169
|
+
let current = node;
|
|
170
|
+
while (current?.parent) {
|
|
171
|
+
const parent = current.parent;
|
|
172
|
+
if (parent.type === 'ArrowFunctionExpression' ||
|
|
173
|
+
parent.type === 'FunctionExpression') {
|
|
174
|
+
const grandParent = parent.parent;
|
|
175
|
+
if (grandParent?.type === 'CallExpression') {
|
|
176
|
+
const callee = grandParent.callee;
|
|
177
|
+
if (callee.type === 'MemberExpression' &&
|
|
178
|
+
callee.property.type === 'Identifier' &&
|
|
179
|
+
callee.property.name === 'map') {
|
|
180
|
+
const obj = callee.object;
|
|
181
|
+
// Children.map()
|
|
182
|
+
if (obj.type === 'Identifier' && obj.name === 'Children') {
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
// React.Children.map()
|
|
186
|
+
if (obj.type === 'MemberExpression' &&
|
|
187
|
+
obj.object.type === 'Identifier' &&
|
|
188
|
+
obj.object.name === 'React' &&
|
|
189
|
+
obj.property.type === 'Identifier' &&
|
|
190
|
+
obj.property.name === 'Children') {
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
current = parent;
|
|
197
|
+
}
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Check if a JSXElement is the DIRECT return value of an iterator callback.
|
|
202
|
+
* Uses node.parent traversal for reliability.
|
|
203
|
+
*
|
|
204
|
+
* Handles:
|
|
205
|
+
* - array.map(item => <div>)
|
|
206
|
+
* - array.map(function(item) { return <div>; })
|
|
207
|
+
* - Array.from(items, item => <div>)
|
|
208
|
+
* - React.Children.map(children, child => <div>)
|
|
209
|
+
* - [...items].map(item => <div>)
|
|
210
|
+
*/
|
|
211
|
+
function isDirectIteratorReturn(node) {
|
|
212
|
+
// Quick check for Array.from and Children.map patterns
|
|
213
|
+
if (isInsideArrayFromMapFn(node)) {
|
|
214
|
+
return isDirectReturnFromFunction(node);
|
|
215
|
+
}
|
|
216
|
+
if (isInsideChildrenMap(node)) {
|
|
217
|
+
return isDirectReturnFromFunction(node);
|
|
218
|
+
}
|
|
219
|
+
let current = node;
|
|
220
|
+
while (current.parent) {
|
|
221
|
+
const parent = current.parent;
|
|
222
|
+
// If we hit another JSXElement, this is a nested element - skip
|
|
223
|
+
if (parent.type === 'JSXElement' || parent.type === 'JSXFragment') {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
// Check: arrow function expression body -> item => <div>
|
|
227
|
+
if (parent.type === 'ArrowFunctionExpression' &&
|
|
228
|
+
parent.body === current) {
|
|
229
|
+
// Check if the arrow function is argument to map/forEach/flatMap
|
|
230
|
+
if (parent.parent && isIteratorCall(parent.parent)) {
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
// Check: return statement -> return <div>
|
|
235
|
+
if (parent.type === 'ReturnStatement' &&
|
|
236
|
+
parent.argument === current) {
|
|
237
|
+
// Walk up to find the function, then check if it's a map callback
|
|
238
|
+
let funcParent = parent.parent;
|
|
239
|
+
while (funcParent) {
|
|
240
|
+
if (funcParent.type === 'ArrowFunctionExpression' ||
|
|
241
|
+
funcParent.type === 'FunctionExpression') {
|
|
242
|
+
if (funcParent.parent && isIteratorCall(funcParent.parent)) {
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
if (funcParent.type === 'BlockStatement') {
|
|
248
|
+
funcParent = funcParent.parent;
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
break;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
// Check: JSX expression container inside another map
|
|
255
|
+
if (parent.type === 'JSXExpressionContainer') {
|
|
256
|
+
// This could be {items.map(...)} inside JSX - check if current is the map result
|
|
257
|
+
if (current.type === 'JSXElement' && parent.parent?.type === 'JSXElement') {
|
|
258
|
+
// We're inside a JSX expression, check if we came from a map callback
|
|
259
|
+
return false;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
// Continue walking up for: conditional expressions, parentheses, etc.
|
|
263
|
+
if (parent.type === 'ConditionalExpression' ||
|
|
264
|
+
parent.type === 'LogicalExpression' ||
|
|
265
|
+
parent.type === 'SequenceExpression') {
|
|
266
|
+
current = parent;
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
// If we hit CallExpression directly, check if it's the map callback
|
|
270
|
+
if (parent.type === 'CallExpression') {
|
|
271
|
+
// We might be the map callback's return value
|
|
272
|
+
break;
|
|
273
|
+
}
|
|
274
|
+
current = parent;
|
|
275
|
+
}
|
|
276
|
+
return false;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Helper: Check if JSX is directly returned from a function (arrow body or return statement)
|
|
280
|
+
*/
|
|
281
|
+
function isDirectReturnFromFunction(node) {
|
|
282
|
+
let current = node;
|
|
283
|
+
while (current.parent) {
|
|
284
|
+
const parent = current.parent;
|
|
285
|
+
// Nested in another JSXElement - not a direct return
|
|
286
|
+
if (parent.type === 'JSXElement' || parent.type === 'JSXFragment') {
|
|
287
|
+
return false;
|
|
288
|
+
}
|
|
289
|
+
// Arrow function with expression body: item => <div>
|
|
290
|
+
if (parent.type === 'ArrowFunctionExpression' &&
|
|
291
|
+
parent.body === current) {
|
|
292
|
+
return true;
|
|
293
|
+
}
|
|
294
|
+
// Return statement: return <div>
|
|
295
|
+
if (parent.type === 'ReturnStatement' &&
|
|
296
|
+
parent.argument === current) {
|
|
297
|
+
return true;
|
|
298
|
+
}
|
|
299
|
+
// Continue for conditionals/logical expressions
|
|
300
|
+
if (parent.type === 'ConditionalExpression' ||
|
|
301
|
+
parent.type === 'LogicalExpression') {
|
|
302
|
+
current = parent;
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
current = parent;
|
|
306
|
+
}
|
|
307
|
+
return false;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Extract the callback parameter name from an iterator call.
|
|
311
|
+
* Returns the first parameter name (e.g., 'user' from users.map(user => ...))
|
|
312
|
+
*
|
|
313
|
+
* Handles:
|
|
314
|
+
* - array.map(item => ...) -> 'item'
|
|
315
|
+
* - Array.from(items, (item, index) => ...) -> 'item'
|
|
316
|
+
* - Children.map(children, child => ...) -> 'child'
|
|
317
|
+
*/
|
|
318
|
+
function getIteratorCallbackParamName(node) {
|
|
319
|
+
let current = node;
|
|
320
|
+
while (current.parent) {
|
|
321
|
+
const parent = current.parent;
|
|
322
|
+
const grandParent = parent.parent;
|
|
323
|
+
// Check arrow function expression - isIteratorCall already handles Array.from
|
|
324
|
+
if (parent.type === 'ArrowFunctionExpression' &&
|
|
325
|
+
parent.params.length > 0 &&
|
|
326
|
+
parent.params[0].type === 'Identifier' &&
|
|
327
|
+
grandParent &&
|
|
328
|
+
isIteratorCall(grandParent)) {
|
|
329
|
+
return parent.params[0].name;
|
|
330
|
+
}
|
|
331
|
+
// Check function expression - isIteratorCall already handles Array.from
|
|
332
|
+
if (parent.type === 'FunctionExpression' &&
|
|
333
|
+
parent.params.length > 0 &&
|
|
334
|
+
parent.params[0].type === 'Identifier' &&
|
|
335
|
+
grandParent &&
|
|
336
|
+
isIteratorCall(grandParent)) {
|
|
337
|
+
return parent.params[0].name;
|
|
338
|
+
}
|
|
339
|
+
// Check block statement -> return -> function
|
|
340
|
+
if (parent.type === 'ReturnStatement') {
|
|
341
|
+
let funcParent = parent.parent;
|
|
342
|
+
while (funcParent) {
|
|
343
|
+
if ((funcParent.type === 'ArrowFunctionExpression' ||
|
|
344
|
+
funcParent.type === 'FunctionExpression') &&
|
|
345
|
+
funcParent.params.length > 0 &&
|
|
346
|
+
funcParent.params[0].type === 'Identifier') {
|
|
347
|
+
if (funcParent.parent && isIteratorCall(funcParent.parent)) {
|
|
348
|
+
return funcParent.params[0].name;
|
|
349
|
+
}
|
|
350
|
+
break;
|
|
351
|
+
}
|
|
352
|
+
if (funcParent.type === 'BlockStatement') {
|
|
353
|
+
funcParent = funcParent.parent;
|
|
354
|
+
continue;
|
|
355
|
+
}
|
|
356
|
+
break;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
current = parent;
|
|
360
|
+
}
|
|
361
|
+
// Default fallback
|
|
362
|
+
return 'item';
|
|
363
|
+
}
|
|
364
|
+
function checkJSXElementInIteration(node) {
|
|
365
|
+
const isDirectReturn = isDirectIteratorReturn(node);
|
|
366
|
+
// Only check elements that are direct returns from iterators
|
|
367
|
+
if (!isDirectReturn) {
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
// Check for key prop
|
|
371
|
+
const keyProp = node.openingElement.attributes.find((attr) => attr.type === 'JSXAttribute' &&
|
|
372
|
+
attr.name.type === 'JSXIdentifier' &&
|
|
373
|
+
attr.name.name === 'key');
|
|
374
|
+
if (!keyProp) {
|
|
375
|
+
// Get the actual callback parameter name for the fix
|
|
376
|
+
const paramName = getIteratorCallbackParamName(node);
|
|
377
|
+
// Missing key - this is the primary issue
|
|
378
|
+
context.report({
|
|
379
|
+
node: node.openingElement,
|
|
380
|
+
messageId: 'missingKey',
|
|
381
|
+
suggest: [
|
|
382
|
+
{
|
|
383
|
+
messageId: 'suggestKey',
|
|
384
|
+
fix(fixer) {
|
|
385
|
+
return fixer.insertTextAfter(node.openingElement.name, ` key={${paramName}.id}`);
|
|
386
|
+
},
|
|
387
|
+
},
|
|
388
|
+
],
|
|
389
|
+
});
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
// Key exists, now analyze its quality
|
|
393
|
+
if (keyProp.type === 'JSXAttribute' && keyProp.value) {
|
|
394
|
+
const keyValue = keyProp.value;
|
|
395
|
+
// Check for common index variable names used as keys (unstable)
|
|
396
|
+
if (warnUnstableKeys &&
|
|
397
|
+
keyValue.type === 'JSXExpressionContainer' &&
|
|
398
|
+
keyValue.expression.type === 'Identifier' &&
|
|
399
|
+
['i', 'index', 'idx', 'key'].includes(keyValue.expression.name)) {
|
|
400
|
+
context.report({
|
|
401
|
+
node: keyProp,
|
|
402
|
+
messageId: 'unstableKey',
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
return {
|
|
408
|
+
JSXElement(node) {
|
|
409
|
+
checkJSXElementInIteration(node);
|
|
410
|
+
},
|
|
411
|
+
// Reset key tracker for each new array/map context
|
|
412
|
+
CallExpression(node) {
|
|
413
|
+
// Check if this is any iterator pattern that produces lists
|
|
414
|
+
if (isIteratorCall(node)) {
|
|
415
|
+
// Entering a new iteration context
|
|
416
|
+
resetKeyTracker();
|
|
417
|
+
}
|
|
418
|
+
},
|
|
419
|
+
};
|
|
420
|
+
},
|
|
421
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Ofri Peretz
|
|
3
|
+
* Licensed under the MIT License. Use of this source code is governed by the
|
|
4
|
+
* MIT license that can be found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* ESLint Rule: jsx-max-depth
|
|
8
|
+
* Limit JSX nesting depth
|
|
9
|
+
*/
|
|
10
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
11
|
+
export interface Options {
|
|
12
|
+
/** Maximum allowed depth of JSX nesting */
|
|
13
|
+
max?: number;
|
|
14
|
+
}
|
|
15
|
+
type RuleOptions = [Options?];
|
|
16
|
+
export declare const jsxMaxDepth: TSESLint.RuleModule<"jsxMaxDepth", RuleOptions, unknown, TSESLint.RuleListener> & {
|
|
17
|
+
name: string;
|
|
18
|
+
};
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) 2025 Ofri Peretz
|
|
4
|
+
* Licensed under the MIT License. Use of this source code is governed by the
|
|
5
|
+
* MIT license that can be found in the LICENSE file.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.jsxMaxDepth = void 0;
|
|
9
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
+
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
11
|
+
exports.jsxMaxDepth = (0, eslint_devkit_1.createRule)({
|
|
12
|
+
name: 'jsx-max-depth',
|
|
13
|
+
meta: {
|
|
14
|
+
type: 'problem',
|
|
15
|
+
docs: {
|
|
16
|
+
description: 'Limit JSX nesting depth',
|
|
17
|
+
},
|
|
18
|
+
messages: {
|
|
19
|
+
jsxMaxDepth: (0, eslint_devkit_2.formatLLMMessage)({
|
|
20
|
+
icon: eslint_devkit_2.MessageIcons.WARNING,
|
|
21
|
+
issueName: 'Excessive JSX Depth',
|
|
22
|
+
description: 'JSX nesting depth exceeds maximum',
|
|
23
|
+
severity: 'MEDIUM',
|
|
24
|
+
fix: 'Extract nested JSX into separate component or reduce nesting',
|
|
25
|
+
documentationLink: 'https://react.dev/learn/thinking-in-react#step-4-identify-where-your-state-should-live',
|
|
26
|
+
}),
|
|
27
|
+
},
|
|
28
|
+
schema: [
|
|
29
|
+
{
|
|
30
|
+
type: 'object',
|
|
31
|
+
properties: {
|
|
32
|
+
max: {
|
|
33
|
+
type: 'number',
|
|
34
|
+
default: 5,
|
|
35
|
+
minimum: 1,
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
additionalProperties: false,
|
|
39
|
+
},
|
|
40
|
+
],
|
|
41
|
+
},
|
|
42
|
+
defaultOptions: [{ max: 5 }],
|
|
43
|
+
create(context) {
|
|
44
|
+
const [options] = context.options;
|
|
45
|
+
const maxDepth = options?.max ?? 5;
|
|
46
|
+
return {
|
|
47
|
+
JSXElement(node) {
|
|
48
|
+
const depth = calculateDepth(node);
|
|
49
|
+
if (depth > maxDepth) {
|
|
50
|
+
context.report({
|
|
51
|
+
node: node.openingElement.name,
|
|
52
|
+
messageId: 'jsxMaxDepth',
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
/**
|
|
60
|
+
* Calculate the maximum depth of JSX nesting
|
|
61
|
+
*/
|
|
62
|
+
function calculateDepth(node, currentDepth = 0) {
|
|
63
|
+
let maxDepth = currentDepth;
|
|
64
|
+
// Check children
|
|
65
|
+
for (const child of node.children) {
|
|
66
|
+
if (child.type === 'JSXElement') {
|
|
67
|
+
const childDepth = calculateDepth(child, currentDepth + 1);
|
|
68
|
+
maxDepth = Math.max(maxDepth, childDepth);
|
|
69
|
+
}
|
|
70
|
+
else if (child.type === 'JSXExpressionContainer') {
|
|
71
|
+
// Check for JSX inside expressions
|
|
72
|
+
const expression = child.expression;
|
|
73
|
+
if (expression.type === 'JSXElement') {
|
|
74
|
+
const childDepth = calculateDepth(expression, currentDepth + 1);
|
|
75
|
+
maxDepth = Math.max(maxDepth, childDepth);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return maxDepth;
|
|
80
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Ofri Peretz
|
|
3
|
+
* Licensed under the MIT License. Use of this source code is governed by the
|
|
4
|
+
* MIT license that can be found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* ESLint Rule: jsx-no-bind
|
|
8
|
+
* Prevent .bind() in JSX (performance issue)
|
|
9
|
+
*/
|
|
10
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
11
|
+
export declare const jsxNoBind: TSESLint.RuleModule<"jsxNoBind", [], unknown, TSESLint.RuleListener> & {
|
|
12
|
+
name: string;
|
|
13
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) 2025 Ofri Peretz
|
|
4
|
+
* Licensed under the MIT License. Use of this source code is governed by the
|
|
5
|
+
* MIT license that can be found in the LICENSE file.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.jsxNoBind = void 0;
|
|
9
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
+
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
11
|
+
exports.jsxNoBind = (0, eslint_devkit_1.createRule)({
|
|
12
|
+
name: 'jsx-no-bind',
|
|
13
|
+
meta: {
|
|
14
|
+
type: 'problem',
|
|
15
|
+
docs: {
|
|
16
|
+
description: 'Prevent .bind() in JSX',
|
|
17
|
+
},
|
|
18
|
+
schema: [],
|
|
19
|
+
messages: {
|
|
20
|
+
jsxNoBind: (0, eslint_devkit_2.formatLLMMessage)({
|
|
21
|
+
icon: eslint_devkit_2.MessageIcons.PERFORMANCE,
|
|
22
|
+
issueName: 'Bind in JSX',
|
|
23
|
+
description: 'Using .bind() in JSX creates new function on every render',
|
|
24
|
+
severity: 'HIGH',
|
|
25
|
+
fix: 'Use arrow function or bind in constructor/class field',
|
|
26
|
+
documentationLink: 'https://react.dev/learn/render-and-commit#should-you-add-eslint-rules',
|
|
27
|
+
}),
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
defaultOptions: [],
|
|
31
|
+
create(context) {
|
|
32
|
+
return {
|
|
33
|
+
JSXAttribute(node) {
|
|
34
|
+
if (node.value && node.value.type === 'JSXExpressionContainer') {
|
|
35
|
+
const expression = node.value.expression;
|
|
36
|
+
// Skip empty expressions like {}
|
|
37
|
+
if (expression.type === 'JSXEmptyExpression') {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
// Check for .bind() calls
|
|
41
|
+
if (isBindCall(expression)) {
|
|
42
|
+
context.report({
|
|
43
|
+
node: expression,
|
|
44
|
+
messageId: 'jsxNoBind',
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
/**
|
|
53
|
+
* Check if expression is a .bind() call
|
|
54
|
+
*/
|
|
55
|
+
function isBindCall(node) {
|
|
56
|
+
if (node.type === 'CallExpression') {
|
|
57
|
+
if (node.callee.type === 'MemberExpression' &&
|
|
58
|
+
node.callee.property.type === 'Identifier' &&
|
|
59
|
+
node.callee.property.name === 'bind') {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Ofri Peretz
|
|
3
|
+
* Licensed under the MIT License. Use of this source code is governed by the
|
|
4
|
+
* MIT license that can be found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* ESLint Rule: jsx-no-duplicate-props
|
|
8
|
+
* Prevent duplicate props in JSX elements
|
|
9
|
+
*
|
|
10
|
+
* Bug Prevention: Duplicate props cause silent overwrites
|
|
11
|
+
* @see https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-no-duplicate-props.md
|
|
12
|
+
*/
|
|
13
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
14
|
+
type Options = {
|
|
15
|
+
ignoreCase?: boolean;
|
|
16
|
+
};
|
|
17
|
+
type RuleOptions = [Options?];
|
|
18
|
+
export declare const jsxNoDuplicateProps: TSESLint.RuleModule<"duplicateProp", RuleOptions, unknown, TSESLint.RuleListener> & {
|
|
19
|
+
name: string;
|
|
20
|
+
};
|
|
21
|
+
export {};
|