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,436 @@
|
|
|
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.requireOptimization = void 0;
|
|
9
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
+
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
11
|
+
exports.requireOptimization = (0, eslint_devkit_1.createRule)({
|
|
12
|
+
name: 'require-optimization',
|
|
13
|
+
meta: {
|
|
14
|
+
type: 'suggestion',
|
|
15
|
+
docs: {
|
|
16
|
+
description: 'Require performance optimizations for React components based on usage patterns',
|
|
17
|
+
},
|
|
18
|
+
hasSuggestions: true,
|
|
19
|
+
messages: {
|
|
20
|
+
considerMemo: (0, eslint_devkit_2.formatLLMMessage)({
|
|
21
|
+
icon: eslint_devkit_2.MessageIcons.PERFORMANCE,
|
|
22
|
+
issueName: 'Component Re-rendering',
|
|
23
|
+
description: 'Component may benefit from React.memo optimization',
|
|
24
|
+
severity: 'MEDIUM',
|
|
25
|
+
fix: 'Wrap with React.memo() to prevent unnecessary re-renders',
|
|
26
|
+
documentationLink: 'https://react.dev/reference/react/memo',
|
|
27
|
+
}),
|
|
28
|
+
considerUseMemo: (0, eslint_devkit_2.formatLLMMessage)({
|
|
29
|
+
icon: eslint_devkit_2.MessageIcons.PERFORMANCE,
|
|
30
|
+
issueName: 'Expensive Computation',
|
|
31
|
+
description: 'Expensive computation should use useMemo',
|
|
32
|
+
severity: 'MEDIUM',
|
|
33
|
+
fix: 'Wrap computation with useMemo() to cache results',
|
|
34
|
+
documentationLink: 'https://react.dev/reference/react/useMemo',
|
|
35
|
+
}),
|
|
36
|
+
considerUseCallback: (0, eslint_devkit_2.formatLLMMessage)({
|
|
37
|
+
icon: eslint_devkit_2.MessageIcons.PERFORMANCE,
|
|
38
|
+
issueName: 'Function Re-creation',
|
|
39
|
+
description: 'Event handlers should use useCallback',
|
|
40
|
+
severity: 'MEDIUM',
|
|
41
|
+
fix: 'Wrap event handler with useCallback() to maintain reference equality',
|
|
42
|
+
documentationLink: 'https://react.dev/reference/react/useCallback',
|
|
43
|
+
}),
|
|
44
|
+
considerLazy: (0, eslint_devkit_2.formatLLMMessage)({
|
|
45
|
+
icon: eslint_devkit_2.MessageIcons.PERFORMANCE,
|
|
46
|
+
issueName: 'Bundle Size',
|
|
47
|
+
description: 'Large component should use React.lazy for code splitting',
|
|
48
|
+
severity: 'LOW',
|
|
49
|
+
fix: 'Use React.lazy() and Suspense for code splitting',
|
|
50
|
+
documentationLink: 'https://react.dev/reference/react/lazy',
|
|
51
|
+
}),
|
|
52
|
+
considerPureComponent: (0, eslint_devkit_2.formatLLMMessage)({
|
|
53
|
+
icon: eslint_devkit_2.MessageIcons.PERFORMANCE,
|
|
54
|
+
issueName: 'Class Component',
|
|
55
|
+
description: 'Class component may benefit from PureComponent',
|
|
56
|
+
severity: 'MEDIUM',
|
|
57
|
+
fix: 'Extend PureComponent instead of Component for shallow comparison',
|
|
58
|
+
documentationLink: 'https://react.dev/reference/react/PureComponent',
|
|
59
|
+
}),
|
|
60
|
+
performanceOptimization: (0, eslint_devkit_2.formatLLMMessage)({
|
|
61
|
+
icon: eslint_devkit_2.MessageIcons.PERFORMANCE,
|
|
62
|
+
issueName: 'Performance Opportunity',
|
|
63
|
+
description: 'Performance optimization opportunity detected',
|
|
64
|
+
severity: 'MEDIUM',
|
|
65
|
+
fix: 'Apply appropriate React optimization based on usage patterns',
|
|
66
|
+
documentationLink: 'https://react.dev/learn/render-and-commit',
|
|
67
|
+
}),
|
|
68
|
+
},
|
|
69
|
+
schema: [
|
|
70
|
+
{
|
|
71
|
+
type: 'object',
|
|
72
|
+
properties: {
|
|
73
|
+
suggestMemo: {
|
|
74
|
+
type: 'boolean',
|
|
75
|
+
default: true,
|
|
76
|
+
},
|
|
77
|
+
suggestUseMemo: {
|
|
78
|
+
type: 'boolean',
|
|
79
|
+
default: true,
|
|
80
|
+
},
|
|
81
|
+
suggestUseCallback: {
|
|
82
|
+
type: 'boolean',
|
|
83
|
+
default: true,
|
|
84
|
+
},
|
|
85
|
+
minPropsForMemo: {
|
|
86
|
+
type: 'number',
|
|
87
|
+
minimum: 1,
|
|
88
|
+
default: 3,
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
additionalProperties: false,
|
|
92
|
+
},
|
|
93
|
+
],
|
|
94
|
+
},
|
|
95
|
+
defaultOptions: [{ suggestMemo: true, suggestUseMemo: true, suggestUseCallback: true, minPropsForMemo: 3 }],
|
|
96
|
+
create(context) {
|
|
97
|
+
const [options] = context.options;
|
|
98
|
+
const { suggestMemo = true, suggestUseMemo = true, suggestUseCallback = true, minPropsForMemo = 3, } = options || {};
|
|
99
|
+
// Track component information
|
|
100
|
+
const components = new Map();
|
|
101
|
+
function analyzeFunctionComponent(node) {
|
|
102
|
+
const componentName = node.type === 'FunctionDeclaration' ? node.id?.name : 'AnonymousComponent';
|
|
103
|
+
if (!componentName)
|
|
104
|
+
return;
|
|
105
|
+
const linesOfCode = node.loc ? node.loc.end.line - node.loc.start.line + 1 : 0;
|
|
106
|
+
// Count props from function parameters
|
|
107
|
+
let propsCount = 0;
|
|
108
|
+
if (node.params.length > 0) {
|
|
109
|
+
const firstParam = node.params[0];
|
|
110
|
+
if (firstParam.type === 'ObjectPattern') {
|
|
111
|
+
// Destructured props: ({ prop1, prop2 }) => ...
|
|
112
|
+
propsCount = firstParam.properties.length;
|
|
113
|
+
}
|
|
114
|
+
else if (firstParam.type === 'Identifier' && firstParam.name === 'props') {
|
|
115
|
+
// Traditional props: (props) => ...
|
|
116
|
+
propsCount = 1; // We can't easily count individual props in this case
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
// Analyze function body
|
|
120
|
+
let hasExpensiveComputations = false;
|
|
121
|
+
let hasEventHandlers = false;
|
|
122
|
+
let hasInlineFunctions = false;
|
|
123
|
+
let renderStatements = 0;
|
|
124
|
+
function analyzeNode(currentNode, depth = 0, visited = new Set()) {
|
|
125
|
+
// Prevent infinite recursion
|
|
126
|
+
if (depth > 10 || visited.has(currentNode)) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
visited.add(currentNode);
|
|
130
|
+
// Detect expensive computations (array operations, complex expressions)
|
|
131
|
+
if (currentNode.type === 'CallExpression' &&
|
|
132
|
+
currentNode.callee.type === 'MemberExpression' &&
|
|
133
|
+
currentNode.callee.property.type === 'Identifier' &&
|
|
134
|
+
['map', 'filter', 'reduce', 'find'].includes(currentNode.callee.property.name)) {
|
|
135
|
+
hasExpensiveComputations = true;
|
|
136
|
+
}
|
|
137
|
+
// Detect event handlers (onClick, onChange, etc.)
|
|
138
|
+
if (currentNode.type === 'JSXAttribute' &&
|
|
139
|
+
currentNode.name.type === 'JSXIdentifier' &&
|
|
140
|
+
currentNode.name.name.startsWith('on') &&
|
|
141
|
+
currentNode.value?.type === 'JSXExpressionContainer') {
|
|
142
|
+
hasEventHandlers = true;
|
|
143
|
+
// Check if event handler is an inline function
|
|
144
|
+
const value = currentNode.value.expression;
|
|
145
|
+
if (value.type === 'ArrowFunctionExpression' ||
|
|
146
|
+
value.type === 'FunctionExpression') {
|
|
147
|
+
hasInlineFunctions = true;
|
|
148
|
+
}
|
|
149
|
+
else if (value.type === 'CallExpression' &&
|
|
150
|
+
value.callee.type === 'ArrowFunctionExpression') {
|
|
151
|
+
// Handle cases like: onClick={() => handleClick(item)}
|
|
152
|
+
hasInlineFunctions = true;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
// Count render statements
|
|
156
|
+
if (currentNode.type === 'ReturnStatement' ||
|
|
157
|
+
currentNode.type === 'JSXElement' ||
|
|
158
|
+
currentNode.type === 'JSXFragment') {
|
|
159
|
+
renderStatements++;
|
|
160
|
+
}
|
|
161
|
+
// Recursive analysis with depth limit - only traverse direct children, not all properties
|
|
162
|
+
if (depth < 10) {
|
|
163
|
+
// Only traverse specific child node types to avoid infinite recursion
|
|
164
|
+
if (currentNode.type === 'BlockStatement' && currentNode.body) {
|
|
165
|
+
currentNode.body.forEach((stmt) => {
|
|
166
|
+
analyzeNode(stmt, depth + 1, visited);
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
else if (currentNode.type === 'ReturnStatement' && currentNode.argument) {
|
|
170
|
+
analyzeNode(currentNode.argument, depth + 1, visited);
|
|
171
|
+
}
|
|
172
|
+
else if (currentNode.type === 'JSXElement') {
|
|
173
|
+
// Analyze JSX attributes
|
|
174
|
+
if (currentNode.openingElement && currentNode.openingElement.attributes) {
|
|
175
|
+
currentNode.openingElement.attributes.forEach((attr) => {
|
|
176
|
+
if (attr.type === 'JSXAttribute') {
|
|
177
|
+
analyzeNode(attr, depth + 1, visited);
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
// Analyze JSX children
|
|
182
|
+
if (currentNode.children) {
|
|
183
|
+
currentNode.children.forEach((child) => {
|
|
184
|
+
analyzeNode(child, depth + 1, visited);
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
else if (currentNode.type === 'CallExpression' && currentNode.arguments) {
|
|
189
|
+
currentNode.arguments.forEach((arg) => {
|
|
190
|
+
if (arg.type !== 'SpreadElement') {
|
|
191
|
+
analyzeNode(arg, depth + 1, visited);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
else if (currentNode.type === 'VariableDeclaration' && currentNode.declarations) {
|
|
196
|
+
currentNode.declarations.forEach((decl) => {
|
|
197
|
+
if (decl.init) {
|
|
198
|
+
analyzeNode(decl.init, depth + 1, visited);
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
// Get the function body to analyze
|
|
205
|
+
const body = node.body;
|
|
206
|
+
if (body) {
|
|
207
|
+
analyzeNode(body);
|
|
208
|
+
}
|
|
209
|
+
components.set(componentName, {
|
|
210
|
+
name: componentName,
|
|
211
|
+
type: node.type === 'ArrowFunctionExpression' ? 'arrow' : 'function',
|
|
212
|
+
node,
|
|
213
|
+
propsCount,
|
|
214
|
+
hasExpensiveComputations,
|
|
215
|
+
hasEventHandlers,
|
|
216
|
+
hasInlineFunctions,
|
|
217
|
+
renderStatements,
|
|
218
|
+
linesOfCode,
|
|
219
|
+
});
|
|
220
|
+
// Analyze and suggest optimizations
|
|
221
|
+
const componentData = components.get(componentName);
|
|
222
|
+
if (componentData) {
|
|
223
|
+
analyzeComponentOptimizations(componentName, componentData);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
function analyzeClassComponent(node) {
|
|
227
|
+
const componentName = node.id?.name;
|
|
228
|
+
if (!componentName)
|
|
229
|
+
return;
|
|
230
|
+
// Check if it extends Component or PureComponent
|
|
231
|
+
let extendsComponent = false;
|
|
232
|
+
let extendsPureComponent = false;
|
|
233
|
+
if (node.superClass) {
|
|
234
|
+
if (node.superClass.type === 'Identifier') {
|
|
235
|
+
extendsComponent = node.superClass.name === 'Component';
|
|
236
|
+
extendsPureComponent = node.superClass.name === 'PureComponent';
|
|
237
|
+
}
|
|
238
|
+
else if (node.superClass.type === 'MemberExpression' &&
|
|
239
|
+
node.superClass.object.type === 'Identifier' &&
|
|
240
|
+
node.superClass.object.name === 'React') {
|
|
241
|
+
extendsComponent = node.superClass.property.type === 'Identifier' &&
|
|
242
|
+
node.superClass.property.name === 'Component';
|
|
243
|
+
extendsPureComponent = node.superClass.property.type === 'Identifier' &&
|
|
244
|
+
node.superClass.property.name === 'PureComponent';
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if (!extendsComponent && !extendsPureComponent) {
|
|
248
|
+
return; // Not a React component
|
|
249
|
+
}
|
|
250
|
+
// Analyze render method and other methods
|
|
251
|
+
let propsCount = 0;
|
|
252
|
+
let hasExpensiveComputations = false;
|
|
253
|
+
let hasEventHandlers = false;
|
|
254
|
+
let renderStatements = 0;
|
|
255
|
+
let linesOfCode = 0;
|
|
256
|
+
for (const member of node.body.body) {
|
|
257
|
+
if (member.type === 'MethodDefinition' &&
|
|
258
|
+
member.key.type === 'Identifier' &&
|
|
259
|
+
member.key.name === 'render' &&
|
|
260
|
+
member.value.body) {
|
|
261
|
+
const body = member.value.body;
|
|
262
|
+
linesOfCode = body.loc ? body.loc.end.line - body.loc.start.line + 1 : 0;
|
|
263
|
+
function analyzeRenderNode(currentNode, depth = 0, visited = new Set()) {
|
|
264
|
+
// Prevent infinite recursion
|
|
265
|
+
if (depth > 10 || visited.has(currentNode)) {
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
visited.add(currentNode);
|
|
269
|
+
// Count props by looking for destructuring or this.props usage
|
|
270
|
+
if (currentNode.type === 'VariableDeclaration') {
|
|
271
|
+
currentNode.declarations.forEach((decl) => {
|
|
272
|
+
if (decl.id.type === 'ObjectPattern' && decl.init) {
|
|
273
|
+
// Check if destructuring from this.props
|
|
274
|
+
if (decl.init.type === 'MemberExpression' &&
|
|
275
|
+
decl.init.object.type === 'ThisExpression' &&
|
|
276
|
+
decl.init.property.type === 'Identifier' &&
|
|
277
|
+
decl.init.property.name === 'props') {
|
|
278
|
+
propsCount = decl.id.properties.length;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
// Also count direct this.props usage as 1 prop (conservative estimate)
|
|
284
|
+
if (currentNode.type === 'MemberExpression' &&
|
|
285
|
+
currentNode.object.type === 'ThisExpression' &&
|
|
286
|
+
currentNode.property.type === 'Identifier' &&
|
|
287
|
+
currentNode.property.name === 'props') {
|
|
288
|
+
if (propsCount === 0)
|
|
289
|
+
propsCount = 1; // At least 1 prop if this.props is used
|
|
290
|
+
}
|
|
291
|
+
if (currentNode.type === 'CallExpression' &&
|
|
292
|
+
currentNode.callee.type === 'MemberExpression' &&
|
|
293
|
+
currentNode.callee.property.type === 'Identifier' &&
|
|
294
|
+
['map', 'filter', 'reduce', 'sort'].includes(currentNode.callee.property.name)) {
|
|
295
|
+
hasExpensiveComputations = true;
|
|
296
|
+
}
|
|
297
|
+
if (currentNode.type === 'JSXAttribute' &&
|
|
298
|
+
currentNode.name.type === 'JSXIdentifier' &&
|
|
299
|
+
currentNode.name.name.startsWith('on')) {
|
|
300
|
+
hasEventHandlers = true;
|
|
301
|
+
}
|
|
302
|
+
if (currentNode.type === 'ReturnStatement' ||
|
|
303
|
+
currentNode.type === 'JSXElement') {
|
|
304
|
+
renderStatements++;
|
|
305
|
+
}
|
|
306
|
+
// Recursive analysis with depth limit - only traverse direct children, not all properties
|
|
307
|
+
if (depth < 10) {
|
|
308
|
+
// Only traverse specific child node types to avoid infinite recursion
|
|
309
|
+
if (currentNode.type === 'BlockStatement' && currentNode.body) {
|
|
310
|
+
currentNode.body.forEach((stmt) => {
|
|
311
|
+
analyzeRenderNode(stmt, depth + 1, visited);
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
else if (currentNode.type === 'ReturnStatement' && currentNode.argument) {
|
|
315
|
+
analyzeRenderNode(currentNode.argument, depth + 1, visited);
|
|
316
|
+
}
|
|
317
|
+
else if (currentNode.type === 'JSXElement') {
|
|
318
|
+
if (currentNode.children) {
|
|
319
|
+
currentNode.children.forEach((child) => {
|
|
320
|
+
analyzeRenderNode(child, depth + 1, visited);
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
else if (currentNode.type === 'VariableDeclaration') {
|
|
325
|
+
currentNode.declarations.forEach((decl) => {
|
|
326
|
+
if (decl.init) {
|
|
327
|
+
analyzeRenderNode(decl.init, depth + 1, visited);
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
analyzeRenderNode(member.value.body);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
components.set(componentName, {
|
|
337
|
+
name: componentName,
|
|
338
|
+
type: 'class',
|
|
339
|
+
node,
|
|
340
|
+
propsCount,
|
|
341
|
+
hasExpensiveComputations,
|
|
342
|
+
hasEventHandlers,
|
|
343
|
+
hasInlineFunctions: false, // Class components don't have this issue
|
|
344
|
+
renderStatements,
|
|
345
|
+
linesOfCode,
|
|
346
|
+
});
|
|
347
|
+
// Suggest PureComponent if not already using it
|
|
348
|
+
if (extendsComponent && !extendsPureComponent) {
|
|
349
|
+
const minPropsForPureComponent = 3; // Lower threshold for class components
|
|
350
|
+
if (propsCount >= minPropsForPureComponent || hasExpensiveComputations) {
|
|
351
|
+
context.report({
|
|
352
|
+
node: node.superClass,
|
|
353
|
+
messageId: 'considerPureComponent',
|
|
354
|
+
data: {
|
|
355
|
+
componentName,
|
|
356
|
+
reason: propsCount >= minPropsForPureComponent
|
|
357
|
+
? `has ${propsCount} props and may benefit from shallow comparison`
|
|
358
|
+
: 'contains expensive computations',
|
|
359
|
+
},
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
// Don't analyze function component optimizations for class components
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
function analyzeComponentOptimizations(componentName, component) {
|
|
367
|
+
const { type, node, propsCount, hasExpensiveComputations, hasInlineFunctions, linesOfCode } = component;
|
|
368
|
+
// Suggest React.memo for function components
|
|
369
|
+
if (suggestMemo && type !== 'class' && propsCount >= minPropsForMemo) {
|
|
370
|
+
context.report({
|
|
371
|
+
node,
|
|
372
|
+
messageId: 'considerMemo',
|
|
373
|
+
data: {
|
|
374
|
+
componentName,
|
|
375
|
+
propsCount,
|
|
376
|
+
reason: `receives ${propsCount} props and may re-render unnecessarily`,
|
|
377
|
+
},
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
// Suggest useMemo for expensive computations
|
|
381
|
+
if (suggestUseMemo && hasExpensiveComputations && type !== 'class') {
|
|
382
|
+
context.report({
|
|
383
|
+
node,
|
|
384
|
+
messageId: 'considerUseMemo',
|
|
385
|
+
data: {
|
|
386
|
+
componentName,
|
|
387
|
+
reason: 'contains array operations that could be expensive',
|
|
388
|
+
},
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
// Suggest useCallback for event handlers with inline functions
|
|
392
|
+
if (suggestUseCallback && hasInlineFunctions && type !== 'class') {
|
|
393
|
+
context.report({
|
|
394
|
+
node,
|
|
395
|
+
messageId: 'considerUseCallback',
|
|
396
|
+
data: {
|
|
397
|
+
componentName,
|
|
398
|
+
reason: 'has inline event handlers that create new functions on each render',
|
|
399
|
+
},
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
// Suggest React.lazy for large components
|
|
403
|
+
if (linesOfCode > 50) { // Arbitrary threshold
|
|
404
|
+
context.report({
|
|
405
|
+
node,
|
|
406
|
+
messageId: 'considerLazy',
|
|
407
|
+
data: {
|
|
408
|
+
componentName,
|
|
409
|
+
linesOfCode,
|
|
410
|
+
reason: `component has ${linesOfCode} lines and may benefit from code splitting`,
|
|
411
|
+
},
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
return {
|
|
416
|
+
FunctionDeclaration(node) {
|
|
417
|
+
// Check if it's a React component (simple heuristic)
|
|
418
|
+
if (node.id?.name && /^[A-Z]/.test(node.id.name)) {
|
|
419
|
+
analyzeFunctionComponent(node);
|
|
420
|
+
}
|
|
421
|
+
},
|
|
422
|
+
ArrowFunctionExpression(node) {
|
|
423
|
+
// Check if it's assigned to a capitalized variable (React component)
|
|
424
|
+
const parent = node.parent;
|
|
425
|
+
if (parent?.type === 'VariableDeclarator' &&
|
|
426
|
+
parent.id?.type === 'Identifier' &&
|
|
427
|
+
/^[A-Z]/.test(parent.id.name)) {
|
|
428
|
+
analyzeFunctionComponent(node);
|
|
429
|
+
}
|
|
430
|
+
},
|
|
431
|
+
ClassDeclaration(node) {
|
|
432
|
+
analyzeClassComponent(node);
|
|
433
|
+
},
|
|
434
|
+
};
|
|
435
|
+
},
|
|
436
|
+
});
|
|
@@ -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: require-render-return
|
|
8
|
+
* Require render methods to return
|
|
9
|
+
*/
|
|
10
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
11
|
+
export declare const requireRenderReturn: TSESLint.RuleModule<"requireRenderReturn", [], unknown, TSESLint.RuleListener> & {
|
|
12
|
+
name: string;
|
|
13
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
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.requireRenderReturn = void 0;
|
|
9
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
+
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
11
|
+
exports.requireRenderReturn = (0, eslint_devkit_1.createRule)({
|
|
12
|
+
name: 'require-render-return',
|
|
13
|
+
meta: {
|
|
14
|
+
type: 'problem',
|
|
15
|
+
docs: {
|
|
16
|
+
description: 'Require render methods to return',
|
|
17
|
+
},
|
|
18
|
+
schema: [],
|
|
19
|
+
messages: {
|
|
20
|
+
requireRenderReturn: (0, eslint_devkit_2.formatLLMMessage)({
|
|
21
|
+
icon: eslint_devkit_2.MessageIcons.WARNING,
|
|
22
|
+
issueName: 'Missing Render Return',
|
|
23
|
+
description: 'Render method must return a value',
|
|
24
|
+
severity: 'CRITICAL',
|
|
25
|
+
fix: 'Add return statement or return JSX/null',
|
|
26
|
+
documentationLink: 'https://react.dev/reference/react/Component#render',
|
|
27
|
+
}),
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
defaultOptions: [],
|
|
31
|
+
create(context) {
|
|
32
|
+
return {
|
|
33
|
+
MethodDefinition(node) {
|
|
34
|
+
if (node.key.type === 'Identifier' &&
|
|
35
|
+
node.key.name === 'render' &&
|
|
36
|
+
node.value.type === 'FunctionExpression') {
|
|
37
|
+
const body = node.value.body;
|
|
38
|
+
// Check if render method has a return statement
|
|
39
|
+
if (!hasReturnStatement(body)) {
|
|
40
|
+
context.report({
|
|
41
|
+
node: node.key,
|
|
42
|
+
messageId: 'requireRenderReturn',
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
function hasReturnStatement(node) {
|
|
49
|
+
// Handle BlockStatement
|
|
50
|
+
if (node.type === 'BlockStatement') {
|
|
51
|
+
for (const statement of node.body) {
|
|
52
|
+
if (checkStatement(statement)) {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
// Handle single statement
|
|
59
|
+
return checkStatement(node);
|
|
60
|
+
}
|
|
61
|
+
function checkStatement(statement) {
|
|
62
|
+
if (statement.type === 'ReturnStatement') {
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
// Check nested blocks (if statements, etc.)
|
|
66
|
+
if (statement.type === 'IfStatement') {
|
|
67
|
+
if (hasReturnStatement(statement.consequent) ||
|
|
68
|
+
(statement.alternate && hasReturnStatement(statement.alternate))) {
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (statement.type === 'BlockStatement') {
|
|
73
|
+
return hasReturnStatement(statement);
|
|
74
|
+
}
|
|
75
|
+
if (statement.type === 'SwitchStatement') {
|
|
76
|
+
for (const switchCase of statement.cases) {
|
|
77
|
+
for (const caseStatement of switchCase.consequent) {
|
|
78
|
+
if (caseStatement.type === 'ReturnStatement') {
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
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: required-attributes
|
|
8
|
+
* Enforce required attributes on React components/elements with customizable ignore lists
|
|
9
|
+
*/
|
|
10
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
11
|
+
type MessageIds = 'missingAttribute' | 'addAttribute';
|
|
12
|
+
interface AttributeRule {
|
|
13
|
+
attribute: string;
|
|
14
|
+
ignoreTags?: string[];
|
|
15
|
+
message?: string;
|
|
16
|
+
suggestedValue?: string;
|
|
17
|
+
}
|
|
18
|
+
export interface Options {
|
|
19
|
+
/** Array of required attribute rules */
|
|
20
|
+
attributes?: AttributeRule[];
|
|
21
|
+
/** Component names to ignore for this rule */
|
|
22
|
+
ignoreComponents?: string[];
|
|
23
|
+
}
|
|
24
|
+
type RuleOptions = [Options?];
|
|
25
|
+
export declare const requiredAttributes: TSESLint.RuleModule<MessageIds, RuleOptions, unknown, TSESLint.RuleListener> & {
|
|
26
|
+
name: string;
|
|
27
|
+
};
|
|
28
|
+
export {};
|