eslint-plugin-react-features 1.2.5 → 1.2.6
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/package.json +5 -17
- package/src/index.d.ts +15 -2074
- package/src/index.js +1 -36
- package/src/oxlint.d.ts +17 -20
- package/src/oxlint.js +0 -18
- package/src/rules/component-api/no-arbitrary-token-class.js +0 -11
- package/src/rules/component-api/no-default-test-id.js +0 -7
- package/src/rules/component-api/no-inline-style.js +0 -6
- package/src/rules/component-api/no-is-prefix-prop.js +0 -7
- package/src/rules/component-api/no-kind-prop-discriminator.js +0 -5
- package/src/rules/component-api/no-raw-color-literal.js +0 -8
- package/src/rules/component-api/no-wrapper-sub-component.js +0 -8
- package/src/rules/component-api/require-data-slot.js +0 -5
- package/src/rules/migration/react-class-to-hooks.js +1 -26
- package/src/rules/performance/no-unnecessary-rerenders.js +1 -32
- package/src/rules/performance/react-no-inline-functions.js +2 -29
- package/src/rules/performance/react-render-optimization.js +1 -40
- package/src/rules/react/checked-requires-onchange-or-readonly.js +0 -7
- package/src/rules/react/default-props-match-prop-types.js +2 -23
- package/src/rules/react/display-name.js +1 -33
- package/src/rules/react/hooks-exhaustive-deps.js +1 -90
- package/src/rules/react/jsx-handler-names.js +0 -8
- package/src/rules/react/jsx-key.js +0 -84
- package/src/rules/react/jsx-max-depth.js +0 -10
- package/src/rules/react/jsx-no-bind.js +0 -10
- package/src/rules/react/jsx-no-duplicate-props.js +0 -5
- package/src/rules/react/jsx-no-literals.js +0 -5
- package/src/rules/react/jsx-no-script-url.js +0 -16
- package/src/rules/react/jsx-no-target-blank.js +1 -20
- package/src/rules/react/no-access-state-in-setstate.js +0 -13
- package/src/rules/react/no-adjacent-inline-elements.js +0 -10
- package/src/rules/react/no-arrow-function-lifecycle.js +0 -7
- package/src/rules/react/no-children-prop.js +0 -7
- package/src/rules/react/no-danger-with-children.js +0 -13
- package/src/rules/react/no-danger.js +0 -5
- package/src/rules/react/no-deprecated.js +0 -11
- package/src/rules/react/no-did-mount-set-state.js +0 -6
- package/src/rules/react/no-did-update-set-state.js +0 -10
- package/src/rules/react/no-direct-mutation-state.js +3 -29
- package/src/rules/react/no-find-dom-node.js +0 -7
- package/src/rules/react/no-invalid-html-attribute.js +36 -41
- package/src/rules/react/no-is-mounted.js +0 -7
- package/src/rules/react/no-multi-comp.js +0 -14
- package/src/rules/react/no-namespace.js +0 -5
- package/src/rules/react/no-object-type-as-default-prop.js +0 -9
- package/src/rules/react/no-redundant-should-component-update.js +0 -6
- package/src/rules/react/no-render-return-value.js +1 -9
- package/src/rules/react/no-set-state.js +0 -6
- package/src/rules/react/no-string-refs.js +0 -8
- package/src/rules/react/no-this-in-sfc.js +0 -11
- package/src/rules/react/no-typos.js +1 -9
- package/src/rules/react/no-unescaped-entities.js +0 -7
- package/src/rules/react/no-unknown-property.js +0 -13
- package/src/rules/react/no-unsafe.js +0 -7
- package/src/rules/react/prefer-es6-class.js +0 -7
- package/src/rules/react/prefer-stateless-function.js +0 -13
- package/src/rules/react/prop-types.js +0 -12
- package/src/rules/react/react-in-jsx-scope.js +0 -9
- package/src/rules/react/require-default-props.js +0 -19
- package/src/rules/react/require-optimization.js +6 -44
- package/src/rules/react/require-render-return.js +0 -9
- package/src/rules/react/required-attributes.js +0 -34
- package/src/rules/react/sort-comp.js +1 -19
- package/src/rules/react/state-in-constructor.js +0 -11
- package/src/rules/react/static-property-placement.js +1 -21
- package/src/rules/react/void-dom-elements-no-children.js +0 -7
- package/src/types/index.js +0 -5
- package/CHANGELOG.md +0 -255
- package/src/rules/component-api/no-arbitrary-token-class.d.ts +0 -17
- package/src/rules/component-api/no-default-test-id.d.ts +0 -15
- package/src/rules/component-api/no-inline-style.d.ts +0 -15
- package/src/rules/component-api/no-is-prefix-prop.d.ts +0 -17
- package/src/rules/component-api/no-kind-prop-discriminator.d.ts +0 -25
- package/src/rules/component-api/no-raw-color-literal.d.ts +0 -16
- package/src/rules/component-api/no-wrapper-sub-component.d.ts +0 -23
- package/src/rules/component-api/require-data-slot.d.ts +0 -15
- package/src/rules/migration/react-class-to-hooks.d.ts +0 -22
- package/src/rules/performance/no-unnecessary-rerenders.d.ts +0 -26
- package/src/rules/performance/react-no-inline-functions.d.ts +0 -24
- package/src/rules/performance/react-render-optimization.d.ts +0 -31
- package/src/rules/react/checked-requires-onchange-or-readonly.d.ts +0 -16
- package/src/rules/react/default-props-match-prop-types.d.ts +0 -13
- package/src/rules/react/display-name.d.ts +0 -13
- package/src/rules/react/hooks-exhaustive-deps.d.ts +0 -25
- package/src/rules/react/jsx-handler-names.d.ts +0 -13
- package/src/rules/react/jsx-key.d.ts +0 -20
- package/src/rules/react/jsx-max-depth.d.ts +0 -19
- package/src/rules/react/jsx-no-bind.d.ts +0 -13
- package/src/rules/react/jsx-no-duplicate-props.d.ts +0 -21
- package/src/rules/react/jsx-no-literals.d.ts +0 -20
- package/src/rules/react/jsx-no-script-url.d.ts +0 -25
- package/src/rules/react/jsx-no-target-blank.d.ts +0 -26
- package/src/rules/react/no-access-state-in-setstate.d.ts +0 -13
- package/src/rules/react/no-adjacent-inline-elements.d.ts +0 -13
- package/src/rules/react/no-arrow-function-lifecycle.d.ts +0 -13
- package/src/rules/react/no-children-prop.d.ts +0 -13
- package/src/rules/react/no-danger-with-children.d.ts +0 -16
- package/src/rules/react/no-danger.d.ts +0 -13
- package/src/rules/react/no-deprecated.d.ts +0 -16
- package/src/rules/react/no-did-mount-set-state.d.ts +0 -13
- package/src/rules/react/no-did-update-set-state.d.ts +0 -19
- package/src/rules/react/no-direct-mutation-state.d.ts +0 -20
- package/src/rules/react/no-find-dom-node.d.ts +0 -15
- package/src/rules/react/no-invalid-html-attribute.d.ts +0 -13
- package/src/rules/react/no-is-mounted.d.ts +0 -13
- package/src/rules/react/no-multi-comp.d.ts +0 -18
- package/src/rules/react/no-namespace.d.ts +0 -13
- package/src/rules/react/no-object-type-as-default-prop.d.ts +0 -13
- package/src/rules/react/no-redundant-should-component-update.d.ts +0 -13
- package/src/rules/react/no-render-return-value.d.ts +0 -13
- package/src/rules/react/no-set-state.d.ts +0 -13
- package/src/rules/react/no-string-refs.d.ts +0 -13
- package/src/rules/react/no-this-in-sfc.d.ts +0 -13
- package/src/rules/react/no-typos.d.ts +0 -13
- package/src/rules/react/no-unescaped-entities.d.ts +0 -13
- package/src/rules/react/no-unknown-property.d.ts +0 -13
- package/src/rules/react/no-unsafe.d.ts +0 -20
- package/src/rules/react/prefer-es6-class.d.ts +0 -13
- package/src/rules/react/prefer-stateless-function.d.ts +0 -16
- package/src/rules/react/prop-types.d.ts +0 -18
- package/src/rules/react/react-in-jsx-scope.d.ts +0 -13
- package/src/rules/react/require-default-props.d.ts +0 -16
- package/src/rules/react/require-optimization.d.ts +0 -26
- package/src/rules/react/require-render-return.d.ts +0 -13
- package/src/rules/react/required-attributes.d.ts +0 -28
- package/src/rules/react/sort-comp.d.ts +0 -17
- package/src/rules/react/state-in-constructor.d.ts +0 -13
- package/src/rules/react/static-property-placement.d.ts +0 -20
- package/src/rules/react/void-dom-elements-no-children.d.ts +0 -15
|
@@ -1,9 +1,4 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.reactInJsxScope = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
@@ -34,7 +29,6 @@ exports.reactInJsxScope = (0, eslint_devkit_1.createRule)({
|
|
|
34
29
|
let hasJsxUsage = false;
|
|
35
30
|
return {
|
|
36
31
|
ImportDeclaration(node) {
|
|
37
|
-
// Check for React import
|
|
38
32
|
if (node.source.value === 'react') {
|
|
39
33
|
for (const specifier of node.specifiers) {
|
|
40
34
|
if (specifier.type === 'ImportDefaultSpecifier' &&
|
|
@@ -50,17 +44,14 @@ exports.reactInJsxScope = (0, eslint_devkit_1.createRule)({
|
|
|
50
44
|
}
|
|
51
45
|
}
|
|
52
46
|
},
|
|
53
|
-
// Check for JSX usage
|
|
54
47
|
JSXElement() {
|
|
55
48
|
hasJsxUsage = true;
|
|
56
49
|
},
|
|
57
50
|
JSXFragment() {
|
|
58
51
|
hasJsxUsage = true;
|
|
59
52
|
},
|
|
60
|
-
// Check at the end of the program
|
|
61
53
|
'Program:exit'(node) {
|
|
62
54
|
if (hasJsxUsage && !hasReactImport) {
|
|
63
|
-
// Find the first JSX element to report on
|
|
64
55
|
for (const statement of node.body) {
|
|
65
56
|
if (statement.type === 'ExpressionStatement' && statement.expression.type === 'JSXElement') {
|
|
66
57
|
context.report({
|
|
@@ -1,9 +1,4 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.requireDefaultProps = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
@@ -46,13 +41,11 @@ exports.requireDefaultProps = (0, eslint_devkit_1.createRule)({
|
|
|
46
41
|
const defaultProps = new Map();
|
|
47
42
|
const reactComponents = new Set();
|
|
48
43
|
return {
|
|
49
|
-
// Track React component classes
|
|
50
44
|
ClassDeclaration(node) {
|
|
51
45
|
if (isReactComponent(node) && node.id?.name) {
|
|
52
46
|
reactComponents.add(node.id.name);
|
|
53
47
|
}
|
|
54
48
|
},
|
|
55
|
-
// Collect propTypes (PropertyDefinition is used by TypeScript parser)
|
|
56
49
|
'PropertyDefinition[key.name="propTypes"]'(node) {
|
|
57
50
|
if (node.value && node.value.type === 'ObjectExpression') {
|
|
58
51
|
const componentName = getComponentName(node);
|
|
@@ -67,7 +60,6 @@ exports.requireDefaultProps = (0, eslint_devkit_1.createRule)({
|
|
|
67
60
|
}
|
|
68
61
|
}
|
|
69
62
|
},
|
|
70
|
-
// Collect defaultProps (PropertyDefinition is used by TypeScript parser)
|
|
71
63
|
'PropertyDefinition[key.name="defaultProps"]'(node) {
|
|
72
64
|
if (node.value && node.value.type === 'ObjectExpression') {
|
|
73
65
|
const componentName = getComponentName(node);
|
|
@@ -82,10 +74,8 @@ exports.requireDefaultProps = (0, eslint_devkit_1.createRule)({
|
|
|
82
74
|
}
|
|
83
75
|
}
|
|
84
76
|
},
|
|
85
|
-
// Check at the end
|
|
86
77
|
'Program:exit'() {
|
|
87
78
|
for (const [componentName, propTypesMap] of propTypes) {
|
|
88
|
-
// Only check React components
|
|
89
79
|
if (!reactComponents.has(componentName)) {
|
|
90
80
|
continue;
|
|
91
81
|
}
|
|
@@ -94,16 +84,11 @@ exports.requireDefaultProps = (0, eslint_devkit_1.createRule)({
|
|
|
94
84
|
const hasDefault = defaultsMap?.has(propName);
|
|
95
85
|
const isRequired = isRequiredProp(propType);
|
|
96
86
|
if (!isRequired && !hasDefault) {
|
|
97
|
-
// Find the component node to report on
|
|
98
|
-
// This is a simplified version - in practice, we'd need to track component nodes
|
|
99
87
|
context.report({
|
|
100
88
|
node: propType.key,
|
|
101
89
|
messageId: 'requireDefaultProps',
|
|
102
90
|
});
|
|
103
91
|
}
|
|
104
|
-
// `hasDefault` is intentionally not re-checked here: `get()`
|
|
105
|
-
// returning a value is the same condition, and this keeps the
|
|
106
|
-
// no-default arm of the lookup reachable.
|
|
107
92
|
if (forbidDefaultForRequired && isRequired && defaultsMap) {
|
|
108
93
|
const defaultProp = defaultsMap.get(propName);
|
|
109
94
|
if (defaultProp) {
|
|
@@ -117,14 +102,12 @@ exports.requireDefaultProps = (0, eslint_devkit_1.createRule)({
|
|
|
117
102
|
}
|
|
118
103
|
},
|
|
119
104
|
};
|
|
120
|
-
// oxlint-disable-next-line consistent-function-scoping
|
|
121
105
|
function getComponentName(node) {
|
|
122
106
|
if (node.parent.type === 'ClassBody' && node.parent.parent.type === 'ClassDeclaration') {
|
|
123
107
|
return node.parent.parent.id?.name || null;
|
|
124
108
|
}
|
|
125
109
|
return null;
|
|
126
110
|
}
|
|
127
|
-
// oxlint-disable-next-line consistent-function-scoping
|
|
128
111
|
function isReactComponent(node) {
|
|
129
112
|
if (!node.superClass)
|
|
130
113
|
return false;
|
|
@@ -139,9 +122,7 @@ exports.requireDefaultProps = (0, eslint_devkit_1.createRule)({
|
|
|
139
122
|
}
|
|
140
123
|
return false;
|
|
141
124
|
}
|
|
142
|
-
// oxlint-disable-next-line consistent-function-scoping
|
|
143
125
|
function isRequiredProp(propType) {
|
|
144
|
-
// Check if prop type ends with .isRequired
|
|
145
126
|
if (propType.value.type === 'MemberExpression' &&
|
|
146
127
|
propType.value.property.type === 'Identifier' &&
|
|
147
128
|
propType.value.property.name === 'isRequired') {
|
|
@@ -1,9 +1,4 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.requireOptimization = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
@@ -97,51 +92,42 @@ exports.requireOptimization = (0, eslint_devkit_1.createRule)({
|
|
|
97
92
|
create(context) {
|
|
98
93
|
const [options] = context.options;
|
|
99
94
|
const { suggestMemo = true, suggestUseMemo = true, suggestUseCallback = true, minPropsForMemo = 3, } = options || {};
|
|
100
|
-
// Track component information
|
|
101
95
|
const components = new Map();
|
|
102
96
|
function analyzeFunctionComponent(node) {
|
|
103
97
|
const componentName = node.type === 'FunctionDeclaration' ? node.id?.name : 'AnonymousComponent';
|
|
104
98
|
if (!componentName)
|
|
105
99
|
return;
|
|
106
100
|
const linesOfCode = node.loc ? node.loc.end.line - node.loc.start.line + 1 : 0;
|
|
107
|
-
// Count props from function parameters
|
|
108
101
|
let propsCount = 0;
|
|
109
102
|
if (node.params.length > 0) {
|
|
110
103
|
const firstParam = node.params[0];
|
|
111
104
|
if (firstParam.type === 'ObjectPattern') {
|
|
112
|
-
// Destructured props: ({ prop1, prop2 }) => ...
|
|
113
105
|
propsCount = firstParam.properties.length;
|
|
114
106
|
}
|
|
115
107
|
else if (firstParam.type === 'Identifier' && firstParam.name === 'props') {
|
|
116
|
-
|
|
117
|
-
propsCount = 1; // We can't easily count individual props in this case
|
|
108
|
+
propsCount = 1;
|
|
118
109
|
}
|
|
119
110
|
}
|
|
120
|
-
// Analyze function body
|
|
121
111
|
let hasExpensiveComputations = false;
|
|
122
112
|
let hasEventHandlers = false;
|
|
123
113
|
let hasInlineFunctions = false;
|
|
124
114
|
let renderStatements = 0;
|
|
125
115
|
function analyzeNode(currentNode, depth = 0, visited = new Set()) {
|
|
126
|
-
// Prevent infinite recursion
|
|
127
116
|
if (depth > 10 || visited.has(currentNode)) {
|
|
128
117
|
return;
|
|
129
118
|
}
|
|
130
119
|
visited.add(currentNode);
|
|
131
|
-
// Detect expensive computations (array operations, complex expressions)
|
|
132
120
|
if (currentNode.type === 'CallExpression' &&
|
|
133
121
|
currentNode.callee.type === 'MemberExpression' &&
|
|
134
122
|
currentNode.callee.property.type === 'Identifier' &&
|
|
135
123
|
['map', 'filter', 'reduce', 'find'].includes(currentNode.callee.property.name)) {
|
|
136
124
|
hasExpensiveComputations = true;
|
|
137
125
|
}
|
|
138
|
-
// Detect event handlers (onClick, onChange, etc.)
|
|
139
126
|
if (currentNode.type === 'JSXAttribute' &&
|
|
140
127
|
currentNode.name.type === 'JSXIdentifier' &&
|
|
141
128
|
currentNode.name.name.startsWith('on') &&
|
|
142
129
|
currentNode.value?.type === 'JSXExpressionContainer') {
|
|
143
130
|
hasEventHandlers = true;
|
|
144
|
-
// Check if event handler is an inline function
|
|
145
131
|
const value = currentNode.value.expression;
|
|
146
132
|
if (value.type === 'ArrowFunctionExpression' ||
|
|
147
133
|
value.type === 'FunctionExpression') {
|
|
@@ -149,19 +135,15 @@ exports.requireOptimization = (0, eslint_devkit_1.createRule)({
|
|
|
149
135
|
}
|
|
150
136
|
else if (value.type === 'CallExpression' &&
|
|
151
137
|
value.callee.type === 'ArrowFunctionExpression') {
|
|
152
|
-
// Handle cases like: onClick={() => handleClick(item)}
|
|
153
138
|
hasInlineFunctions = true;
|
|
154
139
|
}
|
|
155
140
|
}
|
|
156
|
-
// Count render statements
|
|
157
141
|
if (currentNode.type === 'ReturnStatement' ||
|
|
158
142
|
currentNode.type === 'JSXElement' ||
|
|
159
143
|
currentNode.type === 'JSXFragment') {
|
|
160
144
|
renderStatements++;
|
|
161
145
|
}
|
|
162
|
-
// Recursive analysis with depth limit - only traverse direct children, not all properties
|
|
163
146
|
if (depth < 10) {
|
|
164
|
-
// Only traverse specific child node types to avoid infinite recursion
|
|
165
147
|
if (currentNode.type === 'BlockStatement' && currentNode.body) {
|
|
166
148
|
currentNode.body.forEach((stmt) => {
|
|
167
149
|
analyzeNode(stmt, depth + 1, visited);
|
|
@@ -171,7 +153,6 @@ exports.requireOptimization = (0, eslint_devkit_1.createRule)({
|
|
|
171
153
|
analyzeNode(currentNode.argument, depth + 1, visited);
|
|
172
154
|
}
|
|
173
155
|
else if (currentNode.type === 'JSXElement') {
|
|
174
|
-
// Analyze JSX attributes
|
|
175
156
|
if (currentNode.openingElement && currentNode.openingElement.attributes) {
|
|
176
157
|
currentNode.openingElement.attributes.forEach((attr) => {
|
|
177
158
|
if (attr.type === 'JSXAttribute') {
|
|
@@ -179,7 +160,6 @@ exports.requireOptimization = (0, eslint_devkit_1.createRule)({
|
|
|
179
160
|
}
|
|
180
161
|
});
|
|
181
162
|
}
|
|
182
|
-
// Analyze JSX children
|
|
183
163
|
if (currentNode.children) {
|
|
184
164
|
currentNode.children.forEach((child) => {
|
|
185
165
|
analyzeNode(child, depth + 1, visited);
|
|
@@ -202,7 +182,6 @@ exports.requireOptimization = (0, eslint_devkit_1.createRule)({
|
|
|
202
182
|
}
|
|
203
183
|
}
|
|
204
184
|
}
|
|
205
|
-
// Get the function body to analyze
|
|
206
185
|
const body = node.body;
|
|
207
186
|
if (body) {
|
|
208
187
|
analyzeNode(body);
|
|
@@ -219,14 +198,12 @@ exports.requireOptimization = (0, eslint_devkit_1.createRule)({
|
|
|
219
198
|
linesOfCode,
|
|
220
199
|
};
|
|
221
200
|
components.set(componentName, componentData);
|
|
222
|
-
// Analyze and suggest optimizations
|
|
223
201
|
analyzeComponentOptimizations(componentName, componentData);
|
|
224
202
|
}
|
|
225
203
|
function analyzeClassComponent(node) {
|
|
226
204
|
const componentName = node.id?.name;
|
|
227
205
|
if (!componentName)
|
|
228
206
|
return;
|
|
229
|
-
// Check if it extends Component or PureComponent
|
|
230
207
|
let extendsComponent = false;
|
|
231
208
|
let extendsPureComponent = false;
|
|
232
209
|
if (node.superClass) {
|
|
@@ -244,9 +221,8 @@ exports.requireOptimization = (0, eslint_devkit_1.createRule)({
|
|
|
244
221
|
}
|
|
245
222
|
}
|
|
246
223
|
if (!extendsComponent && !extendsPureComponent) {
|
|
247
|
-
return;
|
|
224
|
+
return;
|
|
248
225
|
}
|
|
249
|
-
// Analyze render method and other methods
|
|
250
226
|
let propsCount = 0;
|
|
251
227
|
let hasExpensiveComputations = false;
|
|
252
228
|
let hasEventHandlers = false;
|
|
@@ -260,16 +236,13 @@ exports.requireOptimization = (0, eslint_devkit_1.createRule)({
|
|
|
260
236
|
const body = member.value.body;
|
|
261
237
|
linesOfCode = body.loc ? body.loc.end.line - body.loc.start.line + 1 : 0;
|
|
262
238
|
function analyzeRenderNode(currentNode, depth = 0, visited = new Set()) {
|
|
263
|
-
// Prevent infinite recursion
|
|
264
239
|
if (depth > 10 || visited.has(currentNode)) {
|
|
265
240
|
return;
|
|
266
241
|
}
|
|
267
242
|
visited.add(currentNode);
|
|
268
|
-
// Count props by looking for destructuring or this.props usage
|
|
269
243
|
if (currentNode.type === 'VariableDeclaration') {
|
|
270
244
|
currentNode.declarations.forEach((decl) => {
|
|
271
245
|
if (decl.id.type === 'ObjectPattern' && decl.init) {
|
|
272
|
-
// Check if destructuring from this.props
|
|
273
246
|
if (decl.init.type === 'MemberExpression' &&
|
|
274
247
|
decl.init.object.type === 'ThisExpression' &&
|
|
275
248
|
decl.init.property.type === 'Identifier' &&
|
|
@@ -279,13 +252,12 @@ exports.requireOptimization = (0, eslint_devkit_1.createRule)({
|
|
|
279
252
|
}
|
|
280
253
|
});
|
|
281
254
|
}
|
|
282
|
-
// Also count direct this.props usage as 1 prop (conservative estimate)
|
|
283
255
|
if (currentNode.type === 'MemberExpression' &&
|
|
284
256
|
currentNode.object.type === 'ThisExpression' &&
|
|
285
257
|
currentNode.property.type === 'Identifier' &&
|
|
286
258
|
currentNode.property.name === 'props') {
|
|
287
259
|
if (propsCount === 0)
|
|
288
|
-
propsCount = 1;
|
|
260
|
+
propsCount = 1;
|
|
289
261
|
}
|
|
290
262
|
if (currentNode.type === 'CallExpression' &&
|
|
291
263
|
currentNode.callee.type === 'MemberExpression' &&
|
|
@@ -302,9 +274,7 @@ exports.requireOptimization = (0, eslint_devkit_1.createRule)({
|
|
|
302
274
|
currentNode.type === 'JSXElement') {
|
|
303
275
|
renderStatements++;
|
|
304
276
|
}
|
|
305
|
-
// Recursive analysis with depth limit - only traverse direct children, not all properties
|
|
306
277
|
if (depth < 10) {
|
|
307
|
-
// Only traverse specific child node types to avoid infinite recursion
|
|
308
278
|
if (currentNode.type === 'BlockStatement' && currentNode.body) {
|
|
309
279
|
currentNode.body.forEach((stmt) => {
|
|
310
280
|
analyzeRenderNode(stmt, depth + 1, visited);
|
|
@@ -339,13 +309,12 @@ exports.requireOptimization = (0, eslint_devkit_1.createRule)({
|
|
|
339
309
|
propsCount,
|
|
340
310
|
hasExpensiveComputations,
|
|
341
311
|
hasEventHandlers,
|
|
342
|
-
hasInlineFunctions: false,
|
|
312
|
+
hasInlineFunctions: false,
|
|
343
313
|
renderStatements,
|
|
344
314
|
linesOfCode,
|
|
345
315
|
});
|
|
346
|
-
// Suggest PureComponent if not already using it
|
|
347
316
|
if (extendsComponent && !extendsPureComponent) {
|
|
348
|
-
const minPropsForPureComponent = 3;
|
|
317
|
+
const minPropsForPureComponent = 3;
|
|
349
318
|
if (propsCount >= minPropsForPureComponent || hasExpensiveComputations) {
|
|
350
319
|
context.report({
|
|
351
320
|
node: node.superClass,
|
|
@@ -359,12 +328,10 @@ exports.requireOptimization = (0, eslint_devkit_1.createRule)({
|
|
|
359
328
|
});
|
|
360
329
|
}
|
|
361
330
|
}
|
|
362
|
-
// Don't analyze function component optimizations for class components
|
|
363
331
|
return;
|
|
364
332
|
}
|
|
365
333
|
function analyzeComponentOptimizations(componentName, component) {
|
|
366
334
|
const { type, node, propsCount, hasExpensiveComputations, hasInlineFunctions, linesOfCode } = component;
|
|
367
|
-
// Suggest React.memo for function components
|
|
368
335
|
if (suggestMemo && type !== 'class' && propsCount >= minPropsForMemo) {
|
|
369
336
|
context.report({
|
|
370
337
|
node,
|
|
@@ -376,7 +343,6 @@ exports.requireOptimization = (0, eslint_devkit_1.createRule)({
|
|
|
376
343
|
},
|
|
377
344
|
});
|
|
378
345
|
}
|
|
379
|
-
// Suggest useMemo for expensive computations
|
|
380
346
|
if (suggestUseMemo && hasExpensiveComputations && type !== 'class') {
|
|
381
347
|
context.report({
|
|
382
348
|
node,
|
|
@@ -387,7 +353,6 @@ exports.requireOptimization = (0, eslint_devkit_1.createRule)({
|
|
|
387
353
|
},
|
|
388
354
|
});
|
|
389
355
|
}
|
|
390
|
-
// Suggest useCallback for event handlers with inline functions
|
|
391
356
|
if (suggestUseCallback && hasInlineFunctions && type !== 'class') {
|
|
392
357
|
context.report({
|
|
393
358
|
node,
|
|
@@ -398,8 +363,7 @@ exports.requireOptimization = (0, eslint_devkit_1.createRule)({
|
|
|
398
363
|
},
|
|
399
364
|
});
|
|
400
365
|
}
|
|
401
|
-
|
|
402
|
-
if (linesOfCode > 50) { // Arbitrary threshold
|
|
366
|
+
if (linesOfCode > 50) {
|
|
403
367
|
context.report({
|
|
404
368
|
node,
|
|
405
369
|
messageId: 'considerLazy',
|
|
@@ -413,13 +377,11 @@ exports.requireOptimization = (0, eslint_devkit_1.createRule)({
|
|
|
413
377
|
}
|
|
414
378
|
return {
|
|
415
379
|
FunctionDeclaration(node) {
|
|
416
|
-
// Check if it's a React component (simple heuristic)
|
|
417
380
|
if (node.id?.name && /^[A-Z]/.test(node.id.name)) {
|
|
418
381
|
analyzeFunctionComponent(node);
|
|
419
382
|
}
|
|
420
383
|
},
|
|
421
384
|
ArrowFunctionExpression(node) {
|
|
422
|
-
// Check if it's assigned to a capitalized variable (React component)
|
|
423
385
|
const parent = node.parent;
|
|
424
386
|
if (parent?.type === 'VariableDeclarator' &&
|
|
425
387
|
parent.id?.type === 'Identifier' &&
|
|
@@ -1,9 +1,4 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.requireRenderReturn = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
@@ -36,7 +31,6 @@ exports.requireRenderReturn = (0, eslint_devkit_1.createRule)({
|
|
|
36
31
|
node.key.name === 'render' &&
|
|
37
32
|
node.value.type === 'FunctionExpression') {
|
|
38
33
|
const body = node.value.body;
|
|
39
|
-
// Check if render method has a return statement
|
|
40
34
|
if (!hasReturnStatement(body)) {
|
|
41
35
|
context.report({
|
|
42
36
|
node: node.key,
|
|
@@ -47,7 +41,6 @@ exports.requireRenderReturn = (0, eslint_devkit_1.createRule)({
|
|
|
47
41
|
},
|
|
48
42
|
};
|
|
49
43
|
function hasReturnStatement(node) {
|
|
50
|
-
// Handle BlockStatement
|
|
51
44
|
if (node.type === 'BlockStatement') {
|
|
52
45
|
for (const statement of node.body) {
|
|
53
46
|
if (checkStatement(statement)) {
|
|
@@ -56,14 +49,12 @@ exports.requireRenderReturn = (0, eslint_devkit_1.createRule)({
|
|
|
56
49
|
}
|
|
57
50
|
return false;
|
|
58
51
|
}
|
|
59
|
-
// Handle single statement
|
|
60
52
|
return checkStatement(node);
|
|
61
53
|
}
|
|
62
54
|
function checkStatement(statement) {
|
|
63
55
|
if (statement.type === 'ReturnStatement') {
|
|
64
56
|
return true;
|
|
65
57
|
}
|
|
66
|
-
// Check nested blocks (if statements, etc.)
|
|
67
58
|
if (statement.type === 'IfStatement') {
|
|
68
59
|
if (hasReturnStatement(statement.consequent) ||
|
|
69
60
|
(statement.alternate && hasReturnStatement(statement.alternate))) {
|
|
@@ -1,9 +1,4 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.requiredAttributes = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
@@ -21,7 +16,6 @@ exports.requiredAttributes = (0, eslint_devkit_2.createRule)({
|
|
|
21
16
|
fixable: 'code',
|
|
22
17
|
hasSuggestions: true,
|
|
23
18
|
messages: {
|
|
24
|
-
// 🎯 Token optimization: 41% reduction (54→32 tokens) - required attributes for form/a11y
|
|
25
19
|
missingAttribute: (0, eslint_devkit_1.formatLLMMessage)({
|
|
26
20
|
icon: '📝',
|
|
27
21
|
issueName: 'Missing required attribute',
|
|
@@ -80,17 +74,12 @@ exports.requiredAttributes = (0, eslint_devkit_2.createRule)({
|
|
|
80
74
|
],
|
|
81
75
|
create(context) {
|
|
82
76
|
const options = context.options[0] || {};
|
|
83
|
-
// `options` is always an object here (`|| {}` above), so no second fallback.
|
|
84
77
|
const { attributes = [], ignoreComponents = [] } = options;
|
|
85
|
-
/**
|
|
86
|
-
* Get element name from JSX opening element
|
|
87
|
-
*/
|
|
88
78
|
const getElementName = (node) => {
|
|
89
79
|
if (node.name.type === 'JSXIdentifier') {
|
|
90
80
|
return node.name.name;
|
|
91
81
|
}
|
|
92
82
|
if (node.name.type === 'JSXMemberExpression') {
|
|
93
|
-
// Handle cases like <Box.Item>
|
|
94
83
|
let current = node.name;
|
|
95
84
|
const parts = [];
|
|
96
85
|
while (current.type === 'JSXMemberExpression') {
|
|
@@ -106,9 +95,6 @@ exports.requiredAttributes = (0, eslint_devkit_2.createRule)({
|
|
|
106
95
|
}
|
|
107
96
|
return null;
|
|
108
97
|
};
|
|
109
|
-
/**
|
|
110
|
-
* Check if element has attribute
|
|
111
|
-
*/
|
|
112
98
|
const hasAttribute = (node, attrName) => {
|
|
113
99
|
return node.attributes.some((attr) => {
|
|
114
100
|
if (attr.type !== 'JSXAttribute')
|
|
@@ -118,10 +104,6 @@ exports.requiredAttributes = (0, eslint_devkit_2.createRule)({
|
|
|
118
104
|
return attr.name.name === attrName;
|
|
119
105
|
});
|
|
120
106
|
};
|
|
121
|
-
/**
|
|
122
|
-
* Get attribute purpose based on name
|
|
123
|
-
*/
|
|
124
|
-
// oxlint-disable-next-line consistent-function-scoping
|
|
125
107
|
const getAttributePurpose = (attribute) => {
|
|
126
108
|
const purposes = {
|
|
127
109
|
'data-testid': 'Testing & QA automation',
|
|
@@ -136,20 +118,11 @@ exports.requiredAttributes = (0, eslint_devkit_2.createRule)({
|
|
|
136
118
|
};
|
|
137
119
|
return purposes[attribute] || 'Required for consistency';
|
|
138
120
|
};
|
|
139
|
-
/**
|
|
140
|
-
* Get default suggested value for attribute
|
|
141
|
-
*/
|
|
142
|
-
/**
|
|
143
|
-
* Check if an element is a form element that should have type/name attributes
|
|
144
|
-
*/
|
|
145
|
-
// oxlint-disable-next-line consistent-function-scoping
|
|
146
121
|
const isFormElement = (elementName) => {
|
|
147
122
|
return ['input', 'button', 'select', 'textarea'].includes(elementName.toLowerCase());
|
|
148
123
|
};
|
|
149
|
-
// oxlint-disable-next-line consistent-function-scoping
|
|
150
124
|
const getDefaultSuggestedValue = (attribute, elementName) => {
|
|
151
125
|
if (attribute === 'data-testid') {
|
|
152
|
-
// Generate kebab-case testid from element name
|
|
153
126
|
const kebab = elementName.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
|
|
154
127
|
return kebab;
|
|
155
128
|
}
|
|
@@ -166,26 +139,20 @@ exports.requiredAttributes = (0, eslint_devkit_2.createRule)({
|
|
|
166
139
|
const elementName = getElementName(node);
|
|
167
140
|
if (!elementName)
|
|
168
141
|
return;
|
|
169
|
-
// Check if element is in global ignore list
|
|
170
142
|
if (ignoreComponents.includes(elementName)) {
|
|
171
143
|
return;
|
|
172
144
|
}
|
|
173
|
-
// Check each required attribute
|
|
174
145
|
for (const attrRule of attributes) {
|
|
175
146
|
const { attribute, ignoreTags = [], message, suggestedValue } = attrRule;
|
|
176
|
-
// Check if this element should be ignored for this attribute
|
|
177
147
|
if (ignoreTags.includes(elementName)) {
|
|
178
148
|
continue;
|
|
179
149
|
}
|
|
180
|
-
// SKIP: type/name attributes only apply to form elements
|
|
181
150
|
if ((attribute === 'type' || attribute === 'name') && !isFormElement(elementName)) {
|
|
182
151
|
continue;
|
|
183
152
|
}
|
|
184
|
-
// Check if attribute exists
|
|
185
153
|
if (hasAttribute(node, attribute)) {
|
|
186
154
|
continue;
|
|
187
155
|
}
|
|
188
|
-
// Attribute is missing - report violation
|
|
189
156
|
const purpose = message || getAttributePurpose(attribute);
|
|
190
157
|
const defaultValue = suggestedValue || getDefaultSuggestedValue(attribute, elementName);
|
|
191
158
|
context.report({
|
|
@@ -205,7 +172,6 @@ exports.requiredAttributes = (0, eslint_devkit_2.createRule)({
|
|
|
205
172
|
suggestedValue: defaultValue,
|
|
206
173
|
},
|
|
207
174
|
fix: (fixer) => {
|
|
208
|
-
// Insert attribute after element name
|
|
209
175
|
const nameNode = node.name;
|
|
210
176
|
return fixer.insertTextAfter(nameNode, ` ${attribute}="${defaultValue}"`);
|
|
211
177
|
},
|
|
@@ -1,9 +1,4 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.sortComp = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
@@ -74,7 +69,6 @@ exports.sortComp = (0, eslint_devkit_1.createRule)({
|
|
|
74
69
|
}
|
|
75
70
|
},
|
|
76
71
|
};
|
|
77
|
-
// oxlint-disable-next-line consistent-function-scoping
|
|
78
72
|
function isReactComponent(node) {
|
|
79
73
|
if (!node.superClass)
|
|
80
74
|
return false;
|
|
@@ -92,7 +86,6 @@ exports.sortComp = (0, eslint_devkit_1.createRule)({
|
|
|
92
86
|
function checkMethodOrder(node, sortOrder) {
|
|
93
87
|
const members = node.body.body;
|
|
94
88
|
const methodOrder = [];
|
|
95
|
-
// Collect all methods/properties with their order index
|
|
96
89
|
for (let i = 0; i < members.length; i++) {
|
|
97
90
|
const member = members[i];
|
|
98
91
|
const methodName = getMemberName(member);
|
|
@@ -101,63 +94,52 @@ exports.sortComp = (0, eslint_devkit_1.createRule)({
|
|
|
101
94
|
methodOrder.push({ name: methodName, index: orderIndex, node: member });
|
|
102
95
|
}
|
|
103
96
|
}
|
|
104
|
-
// Check if methods are in correct order
|
|
105
97
|
for (let i = 1; i < methodOrder.length; i++) {
|
|
106
98
|
if (methodOrder[i].index < methodOrder[i - 1].index) {
|
|
107
99
|
context.report({
|
|
108
100
|
node: getMemberKeyNode(methodOrder[i].node),
|
|
109
101
|
messageId: 'sortComp',
|
|
110
102
|
});
|
|
111
|
-
break;
|
|
103
|
+
break;
|
|
112
104
|
}
|
|
113
105
|
}
|
|
114
106
|
}
|
|
115
|
-
// oxlint-disable-next-line consistent-function-scoping
|
|
116
107
|
function getMemberName(member) {
|
|
117
108
|
if (member.type === 'MethodDefinition' && member.key.type === 'Identifier') {
|
|
118
109
|
return member.key.name;
|
|
119
110
|
}
|
|
120
|
-
// Handle PropertyDefinition (TypeScript parser)
|
|
121
111
|
if (member.type === 'PropertyDefinition' && member.key.type === 'Identifier') {
|
|
122
112
|
return member.key.name;
|
|
123
113
|
}
|
|
124
114
|
return null;
|
|
125
115
|
}
|
|
126
|
-
// oxlint-disable-next-line consistent-function-scoping
|
|
127
116
|
function getMemberKeyNode(member) {
|
|
128
117
|
if (member.type === 'MethodDefinition' && member.key.type === 'Identifier') {
|
|
129
118
|
return member.key;
|
|
130
119
|
}
|
|
131
|
-
// Handle PropertyDefinition (TypeScript parser)
|
|
132
120
|
if (member.type === 'PropertyDefinition' && member.key.type === 'Identifier') {
|
|
133
121
|
return member.key;
|
|
134
122
|
}
|
|
135
123
|
return member;
|
|
136
124
|
}
|
|
137
125
|
function getOrderIndex(name, member, sortOrder) {
|
|
138
|
-
// Static methods and properties
|
|
139
126
|
if (member.type === 'MethodDefinition' && member.static) {
|
|
140
127
|
if (name === 'render')
|
|
141
128
|
return sortOrder.indexOf('render');
|
|
142
129
|
return sortOrder.indexOf('static-methods');
|
|
143
130
|
}
|
|
144
|
-
// Handle PropertyDefinition (TypeScript parser)
|
|
145
131
|
if (member.type === 'PropertyDefinition' && member.static) {
|
|
146
132
|
return sortOrder.indexOf('static-variables');
|
|
147
133
|
}
|
|
148
|
-
// Instance variables - Handle PropertyDefinition (TypeScript parser)
|
|
149
134
|
if (member.type === 'PropertyDefinition' && !member.static) {
|
|
150
135
|
return sortOrder.indexOf('instance-variables');
|
|
151
136
|
}
|
|
152
|
-
// Lifecycle methods
|
|
153
137
|
if (LIFECYCLE_METHODS.has(name)) {
|
|
154
138
|
return sortOrder.indexOf('lifecycle');
|
|
155
139
|
}
|
|
156
|
-
// Render method
|
|
157
140
|
if (name === 'render') {
|
|
158
141
|
return sortOrder.indexOf('render');
|
|
159
142
|
}
|
|
160
|
-
// Everything else
|
|
161
143
|
return sortOrder.indexOf('everything-else');
|
|
162
144
|
}
|
|
163
145
|
},
|
|
@@ -1,9 +1,4 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.stateInConstructor = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
@@ -37,7 +32,6 @@ exports.stateInConstructor = (0, eslint_devkit_1.createRule)({
|
|
|
37
32
|
}
|
|
38
33
|
},
|
|
39
34
|
};
|
|
40
|
-
// oxlint-disable-next-line consistent-function-scoping
|
|
41
35
|
function isReactComponent(node) {
|
|
42
36
|
if (!node.superClass)
|
|
43
37
|
return false;
|
|
@@ -56,22 +50,18 @@ exports.stateInConstructor = (0, eslint_devkit_1.createRule)({
|
|
|
56
50
|
let hasConstructorWithState = false;
|
|
57
51
|
let hasStateProperty = false;
|
|
58
52
|
for (const member of node.body.body) {
|
|
59
|
-
// Check for state property (PropertyDefinition is used by TypeScript parser)
|
|
60
53
|
if (member.type === 'PropertyDefinition' &&
|
|
61
54
|
member.key.type === 'Identifier' &&
|
|
62
55
|
member.key.name === 'state') {
|
|
63
56
|
hasStateProperty = true;
|
|
64
57
|
}
|
|
65
|
-
// Check for constructor with state initialization
|
|
66
58
|
if (member.type === 'MethodDefinition' &&
|
|
67
59
|
member.key.type === 'Identifier' &&
|
|
68
60
|
member.key.name === 'constructor') {
|
|
69
61
|
hasConstructorWithState = hasStateInitializationInConstructor(member);
|
|
70
62
|
}
|
|
71
63
|
}
|
|
72
|
-
// Report if state is initialized as property instead of in constructor
|
|
73
64
|
if (hasStateProperty && !hasConstructorWithState) {
|
|
74
|
-
// Find the state property to report on
|
|
75
65
|
for (const member of node.body.body) {
|
|
76
66
|
if (member.type === 'PropertyDefinition' &&
|
|
77
67
|
member.key.type === 'Identifier' &&
|
|
@@ -85,7 +75,6 @@ exports.stateInConstructor = (0, eslint_devkit_1.createRule)({
|
|
|
85
75
|
}
|
|
86
76
|
}
|
|
87
77
|
}
|
|
88
|
-
// oxlint-disable-next-line consistent-function-scoping
|
|
89
78
|
function hasStateInitializationInConstructor(constructor) {
|
|
90
79
|
if (constructor.value.type === 'FunctionExpression') {
|
|
91
80
|
const body = constructor.value.body;
|