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,24 @@
|
|
|
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: react-no-inline-functions
|
|
8
|
+
* Detects inline functions in React renders with performance impact context
|
|
9
|
+
*/
|
|
10
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
11
|
+
type MessageIds = 'inlineFunction' | 'useCallback' | 'extractFunction' | 'strategyUseCallback' | 'strategyUseMemo' | 'strategyExtract';
|
|
12
|
+
export interface Options {
|
|
13
|
+
/** Allow inline functions in event handlers. Default: false */
|
|
14
|
+
allowInEventHandlers?: boolean;
|
|
15
|
+
/** Minimum array size to trigger warning. Default: 5 */
|
|
16
|
+
minArraySize?: number;
|
|
17
|
+
/** Strategy for fixing inline functions: 'usecallback', 'usememo', 'extract', or 'auto' */
|
|
18
|
+
strategy?: 'usecallback' | 'usememo' | 'extract' | 'auto';
|
|
19
|
+
}
|
|
20
|
+
type RuleOptions = [Options?];
|
|
21
|
+
export declare const reactNoInlineFunctions: TSESLint.RuleModule<MessageIds, RuleOptions, unknown, TSESLint.RuleListener> & {
|
|
22
|
+
name: string;
|
|
23
|
+
};
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,211 @@
|
|
|
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.reactNoInlineFunctions = void 0;
|
|
9
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
+
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
11
|
+
exports.reactNoInlineFunctions = (0, eslint_devkit_2.createRule)({
|
|
12
|
+
name: 'react-no-inline-functions',
|
|
13
|
+
meta: {
|
|
14
|
+
type: 'suggestion',
|
|
15
|
+
docs: {
|
|
16
|
+
description: 'Prevent inline functions in React renders with performance metrics',
|
|
17
|
+
},
|
|
18
|
+
fixable: 'code',
|
|
19
|
+
hasSuggestions: true,
|
|
20
|
+
messages: {
|
|
21
|
+
// 🎯 Token optimization: 42% reduction (48→28 tokens) - inline functions cause unnecessary re-renders
|
|
22
|
+
inlineFunction: (0, eslint_devkit_1.formatLLMMessage)({
|
|
23
|
+
icon: eslint_devkit_1.MessageIcons.PERFORMANCE,
|
|
24
|
+
issueName: 'Inline function',
|
|
25
|
+
description: 'Inline function detected',
|
|
26
|
+
severity: 'MEDIUM',
|
|
27
|
+
fix: 'Use useCallback hook or extract to component method',
|
|
28
|
+
documentationLink: 'https://react.dev/reference/react/useCallback',
|
|
29
|
+
}),
|
|
30
|
+
useCallback: (0, eslint_devkit_1.formatLLMMessage)({
|
|
31
|
+
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
32
|
+
issueName: 'Use useCallback',
|
|
33
|
+
description: 'Wrap with useCallback',
|
|
34
|
+
severity: 'LOW',
|
|
35
|
+
fix: 'const handler = useCallback(() => {}, [deps])',
|
|
36
|
+
documentationLink: 'https://react.dev/reference/react/useCallback',
|
|
37
|
+
}),
|
|
38
|
+
extractFunction: (0, eslint_devkit_1.formatLLMMessage)({
|
|
39
|
+
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
40
|
+
issueName: 'Extract Function',
|
|
41
|
+
description: 'Extract to component method',
|
|
42
|
+
severity: 'LOW',
|
|
43
|
+
fix: 'Define function outside JSX or in useCallback',
|
|
44
|
+
documentationLink: 'https://react.dev/learn/your-first-component',
|
|
45
|
+
}),
|
|
46
|
+
strategyUseCallback: (0, eslint_devkit_1.formatLLMMessage)({
|
|
47
|
+
icon: eslint_devkit_1.MessageIcons.STRATEGY,
|
|
48
|
+
issueName: 'useCallback Strategy',
|
|
49
|
+
description: 'Use useCallback to memoize',
|
|
50
|
+
severity: 'LOW',
|
|
51
|
+
fix: 'useCallback(() => {}, [dependencies])',
|
|
52
|
+
documentationLink: 'https://react.dev/reference/react/useCallback',
|
|
53
|
+
}),
|
|
54
|
+
strategyUseMemo: (0, eslint_devkit_1.formatLLMMessage)({
|
|
55
|
+
icon: eslint_devkit_1.MessageIcons.STRATEGY,
|
|
56
|
+
issueName: 'useMemo Strategy',
|
|
57
|
+
description: 'Use useMemo for computed values',
|
|
58
|
+
severity: 'LOW',
|
|
59
|
+
fix: 'useMemo(() => computedValue, [deps])',
|
|
60
|
+
documentationLink: 'https://react.dev/reference/react/useMemo',
|
|
61
|
+
}),
|
|
62
|
+
strategyExtract: (0, eslint_devkit_1.formatLLMMessage)({
|
|
63
|
+
icon: eslint_devkit_1.MessageIcons.STRATEGY,
|
|
64
|
+
issueName: 'Extract Strategy',
|
|
65
|
+
description: 'Extract function outside render',
|
|
66
|
+
severity: 'LOW',
|
|
67
|
+
fix: 'Define function before return statement',
|
|
68
|
+
documentationLink: 'https://react.dev/learn/your-first-component',
|
|
69
|
+
}),
|
|
70
|
+
},
|
|
71
|
+
schema: [
|
|
72
|
+
{
|
|
73
|
+
type: 'object',
|
|
74
|
+
properties: {
|
|
75
|
+
allowInEventHandlers: {
|
|
76
|
+
type: 'boolean',
|
|
77
|
+
default: false,
|
|
78
|
+
},
|
|
79
|
+
minArraySize: {
|
|
80
|
+
type: 'number',
|
|
81
|
+
default: 5,
|
|
82
|
+
description: 'Minimum array size to report inline functions in .map()',
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
additionalProperties: false,
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
},
|
|
89
|
+
defaultOptions: [
|
|
90
|
+
{
|
|
91
|
+
allowInEventHandlers: false,
|
|
92
|
+
minArraySize: 5,
|
|
93
|
+
},
|
|
94
|
+
],
|
|
95
|
+
create(context) {
|
|
96
|
+
const options = context.options[0] || {};
|
|
97
|
+
const { allowInEventHandlers = false, minArraySize = 10 } = options || {};
|
|
98
|
+
/**
|
|
99
|
+
* Check if node is inside JSX
|
|
100
|
+
*/
|
|
101
|
+
const isInJSX = (node) => {
|
|
102
|
+
let parent = node.parent;
|
|
103
|
+
while (parent) {
|
|
104
|
+
if (parent.type === 'JSXExpressionContainer' || parent.type === 'JSXAttribute') {
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
parent = parent.parent;
|
|
108
|
+
}
|
|
109
|
+
return false;
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Check if inline function is in array method
|
|
113
|
+
*/
|
|
114
|
+
const isInArrayMethod = (node) => {
|
|
115
|
+
const parent = node.parent;
|
|
116
|
+
if (parent?.type === 'CallExpression') {
|
|
117
|
+
const callee = parent.callee;
|
|
118
|
+
if (callee.type === 'MemberExpression' && callee.property.type === 'Identifier') {
|
|
119
|
+
const method = callee.property.name;
|
|
120
|
+
if (['map', 'forEach', 'filter', 'reduce', 'sort'].includes(method)) {
|
|
121
|
+
return { inArray: true, method, estimatedSize: minArraySize };
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return { inArray: false };
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* Calculate performance impact
|
|
129
|
+
*/
|
|
130
|
+
const calculateImpact = (arrayInfo) => {
|
|
131
|
+
if (arrayInfo.inArray && arrayInfo.estimatedSize && arrayInfo.estimatedSize > 50) {
|
|
132
|
+
return {
|
|
133
|
+
severity: 'high',
|
|
134
|
+
estimatedSlowdown: '30-50ms per render',
|
|
135
|
+
affectedMetric: 'Interaction to Next Paint (INP)',
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
severity: 'medium',
|
|
140
|
+
estimatedSlowdown: '15-30ms per render',
|
|
141
|
+
affectedMetric: 'Interaction to Next Paint (INP)',
|
|
142
|
+
};
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* Check if node is inside JSXExpressionContainer (directly or nested)
|
|
146
|
+
*/
|
|
147
|
+
const isInJSXExpressionContainer = (node) => {
|
|
148
|
+
let current = node;
|
|
149
|
+
while (current) {
|
|
150
|
+
if (current.type === 'JSXExpressionContainer') {
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
current = current.parent;
|
|
154
|
+
}
|
|
155
|
+
return false;
|
|
156
|
+
};
|
|
157
|
+
return {
|
|
158
|
+
// Match direct children of JSXExpressionContainer
|
|
159
|
+
'JSXExpressionContainer > ArrowFunctionExpression, JSXExpressionContainer > FunctionExpression'(node) {
|
|
160
|
+
if (!isInJSX(node))
|
|
161
|
+
return;
|
|
162
|
+
// Check if it's in an event handler prop
|
|
163
|
+
const parent = node.parent;
|
|
164
|
+
if (allowInEventHandlers && parent?.parent?.type === 'JSXAttribute') {
|
|
165
|
+
const attr = parent.parent;
|
|
166
|
+
if (attr.name.type === 'JSXIdentifier' && attr.name.name.startsWith('on')) {
|
|
167
|
+
return; // Allow if configured
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
const arrayInfo = isInArrayMethod(node);
|
|
171
|
+
const impact = calculateImpact(arrayInfo);
|
|
172
|
+
context.report({
|
|
173
|
+
node,
|
|
174
|
+
messageId: 'inlineFunction',
|
|
175
|
+
data: {
|
|
176
|
+
impact: impact.severity,
|
|
177
|
+
location: arrayInfo.inArray ? `${arrayInfo.method}() call` : 'JSX prop',
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
},
|
|
181
|
+
// Also match arrow functions inside CallExpressions within JSXExpressionContainer
|
|
182
|
+
// This catches cases like {items.map(() => ...)}
|
|
183
|
+
'CallExpression > ArrowFunctionExpression, CallExpression > FunctionExpression'(node) {
|
|
184
|
+
// Only check if we're inside a JSXExpressionContainer
|
|
185
|
+
if (!isInJSXExpressionContainer(node))
|
|
186
|
+
return;
|
|
187
|
+
// Check if it's in an event handler prop
|
|
188
|
+
let current = node;
|
|
189
|
+
while (current) {
|
|
190
|
+
if (current.type === 'JSXAttribute') {
|
|
191
|
+
const attr = current;
|
|
192
|
+
if (allowInEventHandlers && attr.name.type === 'JSXIdentifier' && attr.name.name.startsWith('on')) {
|
|
193
|
+
return; // Allow if configured
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
current = current.parent;
|
|
197
|
+
}
|
|
198
|
+
const arrayInfo = isInArrayMethod(node);
|
|
199
|
+
const impact = calculateImpact(arrayInfo);
|
|
200
|
+
context.report({
|
|
201
|
+
node,
|
|
202
|
+
messageId: 'inlineFunction',
|
|
203
|
+
data: {
|
|
204
|
+
impact: impact.severity,
|
|
205
|
+
location: arrayInfo.inArray ? `${arrayInfo.method}() call` : 'JSX prop',
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
},
|
|
209
|
+
};
|
|
210
|
+
},
|
|
211
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
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: react-render-optimization
|
|
8
|
+
* Detects unnecessary re-renders and expensive computations in React
|
|
9
|
+
* Priority 7: Performance & Optimization
|
|
10
|
+
*
|
|
11
|
+
* @see https://react.dev/learn/render-and-commit
|
|
12
|
+
*/
|
|
13
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
14
|
+
type MessageIds = 'unnecessaryRerender' | 'missingMemoization' | 'expensiveComputation' | 'useMemo' | 'useCallback' | 'useReactMemo';
|
|
15
|
+
export interface Options {
|
|
16
|
+
/** Detect inline object/array props. Default: true */
|
|
17
|
+
detectInlineProps?: boolean;
|
|
18
|
+
/** Detect inline function props. Default: true */
|
|
19
|
+
detectInlineFunctions?: boolean;
|
|
20
|
+
/** Detect expensive computations without useMemo. Default: true */
|
|
21
|
+
detectExpensiveComputations?: boolean;
|
|
22
|
+
/** Minimum computation complexity to flag. Default: 3 */
|
|
23
|
+
minComputationComplexity?: number;
|
|
24
|
+
/** Ignore in test files. Default: true */
|
|
25
|
+
ignoreInTests?: boolean;
|
|
26
|
+
}
|
|
27
|
+
type RuleOptions = [Options?];
|
|
28
|
+
export declare const reactRenderOptimization: TSESLint.RuleModule<MessageIds, RuleOptions, unknown, TSESLint.RuleListener> & {
|
|
29
|
+
name: string;
|
|
30
|
+
};
|
|
31
|
+
export {};
|
|
@@ -0,0 +1,294 @@
|
|
|
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.reactRenderOptimization = void 0;
|
|
9
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
+
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
11
|
+
/**
|
|
12
|
+
* Check if a node is an inline object literal
|
|
13
|
+
*/
|
|
14
|
+
function isInlineObject(node) {
|
|
15
|
+
return node.type === 'ObjectExpression';
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Check if a node is an inline array literal
|
|
19
|
+
*/
|
|
20
|
+
function isInlineArray(node) {
|
|
21
|
+
return node.type === 'ArrayExpression';
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Check if a node is an inline arrow function
|
|
25
|
+
*/
|
|
26
|
+
function isInlineArrowFunction(node) {
|
|
27
|
+
return node.type === 'ArrowFunctionExpression';
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Check if a node is an inline function expression
|
|
31
|
+
*/
|
|
32
|
+
function isInlineFunctionExpression(node) {
|
|
33
|
+
return node.type === 'FunctionExpression';
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Estimate computation complexity
|
|
37
|
+
*/
|
|
38
|
+
function estimateComplexity(node) {
|
|
39
|
+
let complexity = 0;
|
|
40
|
+
// Count loops
|
|
41
|
+
if (node.type === 'ForStatement' || node.type === 'ForOfStatement' ||
|
|
42
|
+
node.type === 'ForInStatement' || node.type === 'WhileStatement') {
|
|
43
|
+
complexity += 3;
|
|
44
|
+
}
|
|
45
|
+
// Count conditionals
|
|
46
|
+
if (node.type === 'IfStatement' || node.type === 'ConditionalExpression') {
|
|
47
|
+
complexity += 1;
|
|
48
|
+
}
|
|
49
|
+
// Count function calls (heuristic)
|
|
50
|
+
if (node.type === 'CallExpression') {
|
|
51
|
+
complexity += 1;
|
|
52
|
+
}
|
|
53
|
+
return complexity;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Check if computation is expensive
|
|
57
|
+
*/
|
|
58
|
+
function isExpensiveComputation(node, minComplexity) {
|
|
59
|
+
// Check if it's a complex expression
|
|
60
|
+
if (node.type === 'BinaryExpression' || node.type === 'LogicalExpression') {
|
|
61
|
+
return false; // Simple operations
|
|
62
|
+
}
|
|
63
|
+
// Check for loops or nested structures
|
|
64
|
+
let current = node;
|
|
65
|
+
let depth = 0;
|
|
66
|
+
const maxDepth = 5;
|
|
67
|
+
while (current && depth < maxDepth) {
|
|
68
|
+
const complexity = estimateComplexity(current);
|
|
69
|
+
if (complexity >= minComplexity) {
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
// Check children
|
|
73
|
+
if ('body' in current && current.body) {
|
|
74
|
+
if (Array.isArray(current.body)) {
|
|
75
|
+
for (const child of current.body) {
|
|
76
|
+
if (estimateComplexity(child) >= minComplexity) {
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
current = current.parent ?? null;
|
|
83
|
+
depth++;
|
|
84
|
+
}
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
exports.reactRenderOptimization = (0, eslint_devkit_2.createRule)({
|
|
88
|
+
name: 'react-render-optimization',
|
|
89
|
+
meta: {
|
|
90
|
+
type: 'suggestion',
|
|
91
|
+
docs: {
|
|
92
|
+
description: 'Detects unnecessary re-renders and expensive computations in React',
|
|
93
|
+
},
|
|
94
|
+
messages: {
|
|
95
|
+
unnecessaryRerender: (0, eslint_devkit_1.formatLLMMessage)({
|
|
96
|
+
icon: eslint_devkit_1.MessageIcons.PERFORMANCE,
|
|
97
|
+
issueName: 'Unnecessary re-render',
|
|
98
|
+
description: 'Inline {{type}} prop causes re-render on every render',
|
|
99
|
+
severity: 'MEDIUM',
|
|
100
|
+
fix: 'Extract to constant or use useMemo/useCallback',
|
|
101
|
+
documentationLink: 'https://react.dev/learn/render-and-commit',
|
|
102
|
+
}),
|
|
103
|
+
missingMemoization: (0, eslint_devkit_1.formatLLMMessage)({
|
|
104
|
+
icon: eslint_devkit_1.MessageIcons.PERFORMANCE,
|
|
105
|
+
issueName: 'Missing memoization',
|
|
106
|
+
description: 'Expensive computation should be memoized',
|
|
107
|
+
severity: 'MEDIUM',
|
|
108
|
+
fix: 'Wrap computation in useMemo hook',
|
|
109
|
+
documentationLink: 'https://react.dev/reference/react/useMemo',
|
|
110
|
+
}),
|
|
111
|
+
expensiveComputation: (0, eslint_devkit_1.formatLLMMessage)({
|
|
112
|
+
icon: eslint_devkit_1.MessageIcons.PERFORMANCE,
|
|
113
|
+
issueName: 'Expensive computation',
|
|
114
|
+
description: 'Complex computation detected (complexity: {{complexity}})',
|
|
115
|
+
severity: 'MEDIUM',
|
|
116
|
+
fix: 'Use useMemo to memoize result',
|
|
117
|
+
documentationLink: 'https://react.dev/reference/react/useMemo',
|
|
118
|
+
}),
|
|
119
|
+
useMemo: (0, eslint_devkit_1.formatLLMMessage)({
|
|
120
|
+
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
121
|
+
issueName: 'Use useMemo',
|
|
122
|
+
description: 'Memoize with useMemo',
|
|
123
|
+
severity: 'LOW',
|
|
124
|
+
fix: 'const memoized = useMemo(() => value, [deps])',
|
|
125
|
+
documentationLink: 'https://react.dev/reference/react/useMemo',
|
|
126
|
+
}),
|
|
127
|
+
useCallback: (0, eslint_devkit_1.formatLLMMessage)({
|
|
128
|
+
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
129
|
+
issueName: 'Use useCallback',
|
|
130
|
+
description: 'Memoize callback function',
|
|
131
|
+
severity: 'LOW',
|
|
132
|
+
fix: 'const handler = useCallback(() => {}, [deps])',
|
|
133
|
+
documentationLink: 'https://react.dev/reference/react/useCallback',
|
|
134
|
+
}),
|
|
135
|
+
useReactMemo: (0, eslint_devkit_1.formatLLMMessage)({
|
|
136
|
+
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
137
|
+
issueName: 'Use React.memo',
|
|
138
|
+
description: 'Wrap component with React.memo',
|
|
139
|
+
severity: 'LOW',
|
|
140
|
+
fix: 'export default React.memo(Component)',
|
|
141
|
+
documentationLink: 'https://react.dev/reference/react/memo',
|
|
142
|
+
}),
|
|
143
|
+
},
|
|
144
|
+
schema: [
|
|
145
|
+
{
|
|
146
|
+
type: 'object',
|
|
147
|
+
properties: {
|
|
148
|
+
detectInlineProps: {
|
|
149
|
+
type: 'boolean',
|
|
150
|
+
default: true,
|
|
151
|
+
description: 'Detect inline object/array props',
|
|
152
|
+
},
|
|
153
|
+
detectInlineFunctions: {
|
|
154
|
+
type: 'boolean',
|
|
155
|
+
default: true,
|
|
156
|
+
description: 'Detect inline function props',
|
|
157
|
+
},
|
|
158
|
+
detectExpensiveComputations: {
|
|
159
|
+
type: 'boolean',
|
|
160
|
+
default: true,
|
|
161
|
+
description: 'Detect expensive computations without useMemo',
|
|
162
|
+
},
|
|
163
|
+
minComputationComplexity: {
|
|
164
|
+
type: 'number',
|
|
165
|
+
default: 3,
|
|
166
|
+
minimum: 1,
|
|
167
|
+
description: 'Minimum computation complexity to flag',
|
|
168
|
+
},
|
|
169
|
+
ignoreInTests: {
|
|
170
|
+
type: 'boolean',
|
|
171
|
+
default: true,
|
|
172
|
+
description: 'Ignore in test files',
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
additionalProperties: false,
|
|
176
|
+
},
|
|
177
|
+
],
|
|
178
|
+
},
|
|
179
|
+
defaultOptions: [
|
|
180
|
+
{
|
|
181
|
+
detectInlineProps: true,
|
|
182
|
+
detectInlineFunctions: true,
|
|
183
|
+
detectExpensiveComputations: true,
|
|
184
|
+
minComputationComplexity: 3,
|
|
185
|
+
ignoreInTests: true,
|
|
186
|
+
},
|
|
187
|
+
],
|
|
188
|
+
create(context, [options = {}]) {
|
|
189
|
+
const { detectInlineProps = true, detectInlineFunctions = true, detectExpensiveComputations = true, minComputationComplexity = 3, ignoreInTests = true, } = options || {};
|
|
190
|
+
const filename = context.getFilename();
|
|
191
|
+
const isTestFile = ignoreInTests && /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
|
|
192
|
+
if (isTestFile) {
|
|
193
|
+
return {};
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Check JSX attributes for inline props
|
|
197
|
+
*/
|
|
198
|
+
function checkJSXAttribute(node) {
|
|
199
|
+
try {
|
|
200
|
+
if (!node.value) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
// Extract expression from JSXExpressionContainer
|
|
204
|
+
let value = null;
|
|
205
|
+
if (node.value.type === 'JSXExpressionContainer') {
|
|
206
|
+
value = node.value.expression;
|
|
207
|
+
}
|
|
208
|
+
else if (node.value.type === 'Literal') {
|
|
209
|
+
// String literals are fine
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (!value) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
// Check for inline objects
|
|
216
|
+
if (detectInlineProps && isInlineObject(value)) {
|
|
217
|
+
context.report({
|
|
218
|
+
node,
|
|
219
|
+
messageId: 'unnecessaryRerender',
|
|
220
|
+
data: {
|
|
221
|
+
type: 'object',
|
|
222
|
+
},
|
|
223
|
+
suggest: [
|
|
224
|
+
{
|
|
225
|
+
messageId: 'useMemo',
|
|
226
|
+
fix: () => null,
|
|
227
|
+
},
|
|
228
|
+
],
|
|
229
|
+
});
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
// Check for inline arrays
|
|
233
|
+
if (detectInlineProps && isInlineArray(value)) {
|
|
234
|
+
context.report({
|
|
235
|
+
node,
|
|
236
|
+
messageId: 'unnecessaryRerender',
|
|
237
|
+
data: {
|
|
238
|
+
type: 'array',
|
|
239
|
+
},
|
|
240
|
+
suggest: [
|
|
241
|
+
{
|
|
242
|
+
messageId: 'useMemo',
|
|
243
|
+
fix: () => null,
|
|
244
|
+
},
|
|
245
|
+
],
|
|
246
|
+
});
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
// Check for inline functions
|
|
250
|
+
if (detectInlineFunctions &&
|
|
251
|
+
(isInlineArrowFunction(value) || isInlineFunctionExpression(value))) {
|
|
252
|
+
context.report({
|
|
253
|
+
node,
|
|
254
|
+
messageId: 'unnecessaryRerender',
|
|
255
|
+
data: {
|
|
256
|
+
type: 'function',
|
|
257
|
+
},
|
|
258
|
+
suggest: [
|
|
259
|
+
{
|
|
260
|
+
messageId: 'useCallback',
|
|
261
|
+
fix: () => null,
|
|
262
|
+
},
|
|
263
|
+
],
|
|
264
|
+
});
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
// Check for expensive computations
|
|
268
|
+
if (detectExpensiveComputations && isExpensiveComputation(value, minComputationComplexity)) {
|
|
269
|
+
const complexity = estimateComplexity(value);
|
|
270
|
+
context.report({
|
|
271
|
+
node,
|
|
272
|
+
messageId: 'expensiveComputation',
|
|
273
|
+
data: {
|
|
274
|
+
complexity: String(complexity),
|
|
275
|
+
},
|
|
276
|
+
suggest: [
|
|
277
|
+
{
|
|
278
|
+
messageId: 'useMemo',
|
|
279
|
+
fix: () => null,
|
|
280
|
+
},
|
|
281
|
+
],
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
catch {
|
|
286
|
+
// Silently skip if there's an error processing this attribute
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return {
|
|
291
|
+
JSXAttribute: checkJSXAttribute,
|
|
292
|
+
};
|
|
293
|
+
},
|
|
294
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
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: checked-requires-onchange-or-readonly
|
|
8
|
+
* Ensure controlled inputs have onChange or readOnly
|
|
9
|
+
*/
|
|
10
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
11
|
+
export interface Options {
|
|
12
|
+
}
|
|
13
|
+
export type RuleOptions = [Options?];
|
|
14
|
+
export declare const checkedRequiresOnchangeOrReadonly: TSESLint.RuleModule<"checkedRequiresOnChangeOrReadOnly", RuleOptions, unknown, TSESLint.RuleListener> & {
|
|
15
|
+
name: string;
|
|
16
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
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.checkedRequiresOnchangeOrReadonly = void 0;
|
|
9
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
+
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
11
|
+
exports.checkedRequiresOnchangeOrReadonly = (0, eslint_devkit_1.createRule)({
|
|
12
|
+
name: 'checked-requires-onchange-or-readonly',
|
|
13
|
+
meta: {
|
|
14
|
+
type: 'problem',
|
|
15
|
+
docs: {
|
|
16
|
+
description: 'Ensure controlled inputs have onChange or readOnly',
|
|
17
|
+
},
|
|
18
|
+
schema: [],
|
|
19
|
+
messages: {
|
|
20
|
+
checkedRequiresOnChangeOrReadOnly: (0, eslint_devkit_2.formatLLMMessage)({
|
|
21
|
+
icon: eslint_devkit_2.MessageIcons.WARNING,
|
|
22
|
+
issueName: 'Controlled Input Missing Handler',
|
|
23
|
+
description: 'Checked input missing onChange or readOnly',
|
|
24
|
+
severity: 'MEDIUM',
|
|
25
|
+
fix: 'Add onChange handler or readOnly attribute',
|
|
26
|
+
documentationLink: 'https://react.dev/reference/react-dom/components/input#controlling-an-input-with-a-state-variable',
|
|
27
|
+
}),
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
defaultOptions: [],
|
|
31
|
+
create(context) {
|
|
32
|
+
return {
|
|
33
|
+
JSXElement(node) {
|
|
34
|
+
if (node.openingElement.name.type !== 'JSXIdentifier') {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const tagName = node.openingElement.name.name;
|
|
38
|
+
// Only check input and textarea elements
|
|
39
|
+
if (!['input', 'textarea', 'select'].includes(tagName)) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const attributes = node.openingElement.attributes;
|
|
43
|
+
let hasChecked = false;
|
|
44
|
+
let hasValue = false;
|
|
45
|
+
let hasOnChange = false;
|
|
46
|
+
let hasReadOnly = false;
|
|
47
|
+
for (const attr of attributes) {
|
|
48
|
+
if (attr.type !== 'JSXAttribute')
|
|
49
|
+
continue;
|
|
50
|
+
const attrName = attr.name.type === 'JSXIdentifier' ? attr.name.name : '';
|
|
51
|
+
if (attrName === 'checked') {
|
|
52
|
+
hasChecked = true;
|
|
53
|
+
}
|
|
54
|
+
else if (attrName === 'value') {
|
|
55
|
+
hasValue = true;
|
|
56
|
+
}
|
|
57
|
+
else if (attrName === 'onChange' || attrName === 'onInput') {
|
|
58
|
+
hasOnChange = true;
|
|
59
|
+
}
|
|
60
|
+
else if (attrName === 'readOnly') {
|
|
61
|
+
hasReadOnly = true;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
// Report if checked/value is present but no onChange/readOnly
|
|
65
|
+
if ((hasChecked || hasValue) && !hasOnChange && !hasReadOnly) {
|
|
66
|
+
context.report({
|
|
67
|
+
node: node.openingElement.name,
|
|
68
|
+
messageId: 'checkedRequiresOnChangeOrReadOnly',
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
},
|
|
74
|
+
});
|
|
@@ -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: default-props-match-prop-types
|
|
8
|
+
* Validate default props match prop types
|
|
9
|
+
*/
|
|
10
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
11
|
+
export declare const defaultPropsMatchPropTypes: TSESLint.RuleModule<"defaultPropsMatchPropTypes", [], unknown, TSESLint.RuleListener> & {
|
|
12
|
+
name: string;
|
|
13
|
+
};
|