eslint-plugin-react-features 1.2.9 → 1.2.10
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 +2 -2
- package/src/index.js +1 -179
- package/src/oxlint.js +1 -3
- package/src/rules/component-api/no-arbitrary-token-class.js +1 -74
- package/src/rules/component-api/no-default-test-id.js +1 -46
- package/src/rules/component-api/no-inline-style.js +1 -57
- package/src/rules/component-api/no-is-prefix-prop.js +1 -59
- package/src/rules/component-api/no-kind-prop-discriminator.js +1 -57
- package/src/rules/component-api/no-raw-color-literal.js +1 -55
- package/src/rules/component-api/no-wrapper-sub-component.js +1 -95
- package/src/rules/component-api/require-data-slot.js +1 -40
- package/src/rules/migration/react-class-to-hooks.js +1 -159
- package/src/rules/performance/no-unnecessary-rerenders.js +1 -185
- package/src/rules/performance/react-no-inline-functions.js +1 -187
- package/src/rules/performance/react-render-optimization.js +1 -256
- package/src/rules/react/checked-requires-onchange-or-readonly.js +1 -68
- package/src/rules/react/default-props-match-prop-types.js +1 -99
- package/src/rules/react/display-name.js +1 -133
- package/src/rules/react/hooks-exhaustive-deps.js +1 -466
- package/src/rules/react/jsx-handler-names.js +1 -50
- package/src/rules/react/jsx-key.js +1 -323
- package/src/rules/react/jsx-max-depth.js +1 -71
- package/src/rules/react/jsx-no-bind.js +1 -55
- package/src/rules/react/jsx-no-duplicate-props.js +1 -66
- package/src/rules/react/jsx-no-literals.js +1 -79
- package/src/rules/react/jsx-no-script-url.js +1 -119
- package/src/rules/react/jsx-no-target-blank.js +1 -163
- package/src/rules/react/no-access-state-in-setstate.js +1 -80
- package/src/rules/react/no-adjacent-inline-elements.js +1 -72
- package/src/rules/react/no-arrow-function-lifecycle.js +1 -57
- package/src/rules/react/no-children-prop.js +1 -54
- package/src/rules/react/no-danger-with-children.js +1 -99
- package/src/rules/react/no-danger.js +1 -43
- package/src/rules/react/no-deprecated.js +1 -130
- package/src/rules/react/no-did-mount-set-state.js +1 -50
- package/src/rules/react/no-did-update-set-state.js +1 -71
- package/src/rules/react/no-direct-mutation-state.js +3 -211
- package/src/rules/react/no-find-dom-node.js +1 -50
- package/src/rules/react/no-invalid-html-attribute.js +1 -80
- package/src/rules/react/no-is-mounted.js +1 -46
- package/src/rules/react/no-multi-comp.js +1 -150
- package/src/rules/react/no-namespace.js +1 -37
- package/src/rules/react/no-object-type-as-default-prop.js +1 -53
- package/src/rules/react/no-redundant-should-component-update.js +1 -50
- package/src/rules/react/no-render-return-value.js +1 -67
- package/src/rules/react/no-set-state.js +1 -45
- package/src/rules/react/no-string-refs.js +1 -61
- package/src/rules/react/no-this-in-sfc.js +1 -55
- package/src/rules/react/no-typos.js +1 -90
- package/src/rules/react/no-unescaped-entities.js +1 -44
- package/src/rules/react/no-unknown-property.js +1 -124
- package/src/rules/react/no-unsafe.js +1 -106
- package/src/rules/react/prefer-es6-class.js +1 -50
- package/src/rules/react/prefer-stateless-function.js +1 -126
- package/src/rules/react/prop-types.js +1 -143
- package/src/rules/react/react-in-jsx-scope.js +1 -86
- package/src/rules/react/require-default-props.js +1 -134
- package/src/rules/react/require-optimization.js +1 -397
- package/src/rules/react/require-render-return.js +1 -79
- package/src/rules/react/required-attributes.js +1 -185
- package/src/rules/react/sort-comp.js +1 -146
- package/src/rules/react/state-in-constructor.js +1 -95
- package/src/rules/react/static-property-placement.js +1 -122
- package/src/rules/react/void-dom-elements-no-children.js +1 -89
- package/src/types/index.js +1 -2
|
@@ -1,187 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.reactNoInlineFunctions = void 0;
|
|
4
|
-
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
-
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
6
|
-
exports.reactNoInlineFunctions = (0, eslint_devkit_2.createRule)({
|
|
7
|
-
name: 'react-no-inline-functions',
|
|
8
|
-
meta: {
|
|
9
|
-
type: 'suggestion',
|
|
10
|
-
docs: {
|
|
11
|
-
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/react-no-inline-functions.md',
|
|
12
|
-
description: 'Prevent inline functions in React renders with performance metrics',
|
|
13
|
-
},
|
|
14
|
-
hasSuggestions: true,
|
|
15
|
-
messages: {
|
|
16
|
-
inlineFunction: (0, eslint_devkit_1.formatLLMMessage)({
|
|
17
|
-
icon: eslint_devkit_1.MessageIcons.PERFORMANCE,
|
|
18
|
-
issueName: 'Inline function',
|
|
19
|
-
description: 'Inline function detected',
|
|
20
|
-
severity: 'MEDIUM',
|
|
21
|
-
fix: 'Use useCallback hook or extract to component method',
|
|
22
|
-
documentationLink: 'https://react.dev/reference/react/useCallback',
|
|
23
|
-
}),
|
|
24
|
-
useCallback: (0, eslint_devkit_1.formatLLMMessage)({
|
|
25
|
-
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
26
|
-
issueName: 'Use useCallback',
|
|
27
|
-
description: 'Wrap with useCallback',
|
|
28
|
-
severity: 'LOW',
|
|
29
|
-
fix: 'const handler = useCallback(() => {}, [deps])',
|
|
30
|
-
documentationLink: 'https://react.dev/reference/react/useCallback',
|
|
31
|
-
}),
|
|
32
|
-
extractFunction: (0, eslint_devkit_1.formatLLMMessage)({
|
|
33
|
-
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
34
|
-
issueName: 'Extract Function',
|
|
35
|
-
description: 'Extract to component method',
|
|
36
|
-
severity: 'LOW',
|
|
37
|
-
fix: 'Define function outside JSX or in useCallback',
|
|
38
|
-
documentationLink: 'https://react.dev/learn/your-first-component',
|
|
39
|
-
}),
|
|
40
|
-
strategyUseCallback: (0, eslint_devkit_1.formatLLMMessage)({
|
|
41
|
-
icon: eslint_devkit_1.MessageIcons.STRATEGY,
|
|
42
|
-
issueName: 'useCallback Strategy',
|
|
43
|
-
description: 'Use useCallback to memoize',
|
|
44
|
-
severity: 'LOW',
|
|
45
|
-
fix: 'useCallback(() => {}, [dependencies])',
|
|
46
|
-
documentationLink: 'https://react.dev/reference/react/useCallback',
|
|
47
|
-
}),
|
|
48
|
-
strategyUseMemo: (0, eslint_devkit_1.formatLLMMessage)({
|
|
49
|
-
icon: eslint_devkit_1.MessageIcons.STRATEGY,
|
|
50
|
-
issueName: 'useMemo Strategy',
|
|
51
|
-
description: 'Use useMemo for computed values',
|
|
52
|
-
severity: 'LOW',
|
|
53
|
-
fix: 'useMemo(() => computedValue, [deps])',
|
|
54
|
-
documentationLink: 'https://react.dev/reference/react/useMemo',
|
|
55
|
-
}),
|
|
56
|
-
strategyExtract: (0, eslint_devkit_1.formatLLMMessage)({
|
|
57
|
-
icon: eslint_devkit_1.MessageIcons.STRATEGY,
|
|
58
|
-
issueName: 'Extract Strategy',
|
|
59
|
-
description: 'Extract function outside render',
|
|
60
|
-
severity: 'LOW',
|
|
61
|
-
fix: 'Define function before return statement',
|
|
62
|
-
documentationLink: 'https://react.dev/learn/your-first-component',
|
|
63
|
-
}),
|
|
64
|
-
},
|
|
65
|
-
schema: [
|
|
66
|
-
{
|
|
67
|
-
type: 'object',
|
|
68
|
-
properties: {
|
|
69
|
-
allowInEventHandlers: {
|
|
70
|
-
type: 'boolean',
|
|
71
|
-
default: false,
|
|
72
|
-
},
|
|
73
|
-
minArraySize: {
|
|
74
|
-
type: 'number',
|
|
75
|
-
default: 5,
|
|
76
|
-
description: 'Minimum array size to report inline functions in .map()',
|
|
77
|
-
},
|
|
78
|
-
},
|
|
79
|
-
additionalProperties: false,
|
|
80
|
-
},
|
|
81
|
-
],
|
|
82
|
-
},
|
|
83
|
-
defaultOptions: [
|
|
84
|
-
{
|
|
85
|
-
allowInEventHandlers: false,
|
|
86
|
-
minArraySize: 5,
|
|
87
|
-
},
|
|
88
|
-
],
|
|
89
|
-
create(context) {
|
|
90
|
-
const options = context.options[0] || {};
|
|
91
|
-
const { allowInEventHandlers = false, minArraySize = 10 } = options;
|
|
92
|
-
const isInJSX = (node) => {
|
|
93
|
-
let parent = node.parent;
|
|
94
|
-
while (parent) {
|
|
95
|
-
if (parent.type === 'JSXExpressionContainer' || parent.type === 'JSXAttribute') {
|
|
96
|
-
return true;
|
|
97
|
-
}
|
|
98
|
-
parent = parent.parent;
|
|
99
|
-
}
|
|
100
|
-
return false;
|
|
101
|
-
};
|
|
102
|
-
const isInArrayMethod = (node) => {
|
|
103
|
-
const parent = node.parent;
|
|
104
|
-
if (parent?.type === 'CallExpression') {
|
|
105
|
-
const callee = parent.callee;
|
|
106
|
-
if (callee.type === 'MemberExpression' && callee.property.type === 'Identifier') {
|
|
107
|
-
const method = callee.property.name;
|
|
108
|
-
if (['map', 'forEach', 'filter', 'reduce', 'sort'].includes(method)) {
|
|
109
|
-
return { inArray: true, method, estimatedSize: minArraySize };
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
return { inArray: false };
|
|
114
|
-
};
|
|
115
|
-
const calculateImpact = (arrayInfo) => {
|
|
116
|
-
if (arrayInfo.inArray && arrayInfo.estimatedSize && arrayInfo.estimatedSize > 50) {
|
|
117
|
-
return {
|
|
118
|
-
severity: 'high',
|
|
119
|
-
estimatedSlowdown: '30-50ms per render',
|
|
120
|
-
affectedMetric: 'Interaction to Next Paint (INP)',
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
return {
|
|
124
|
-
severity: 'medium',
|
|
125
|
-
estimatedSlowdown: '15-30ms per render',
|
|
126
|
-
affectedMetric: 'Interaction to Next Paint (INP)',
|
|
127
|
-
};
|
|
128
|
-
};
|
|
129
|
-
const isInJSXExpressionContainer = (node) => {
|
|
130
|
-
let current = node;
|
|
131
|
-
while (current) {
|
|
132
|
-
if (current.type === 'JSXExpressionContainer') {
|
|
133
|
-
return true;
|
|
134
|
-
}
|
|
135
|
-
current = current.parent;
|
|
136
|
-
}
|
|
137
|
-
return false;
|
|
138
|
-
};
|
|
139
|
-
return {
|
|
140
|
-
'JSXExpressionContainer > ArrowFunctionExpression, JSXExpressionContainer > FunctionExpression'(node) {
|
|
141
|
-
if (!isInJSX(node))
|
|
142
|
-
return;
|
|
143
|
-
const parent = node.parent;
|
|
144
|
-
if (allowInEventHandlers && parent?.parent?.type === 'JSXAttribute') {
|
|
145
|
-
const attr = parent.parent;
|
|
146
|
-
if (attr.name.type === 'JSXIdentifier' && attr.name.name.startsWith('on')) {
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
const arrayInfo = isInArrayMethod(node);
|
|
151
|
-
const impact = calculateImpact(arrayInfo);
|
|
152
|
-
context.report({
|
|
153
|
-
node,
|
|
154
|
-
messageId: 'inlineFunction',
|
|
155
|
-
data: {
|
|
156
|
-
impact: impact.severity,
|
|
157
|
-
location: arrayInfo.inArray ? `${arrayInfo.method}() call` : 'JSX prop',
|
|
158
|
-
},
|
|
159
|
-
});
|
|
160
|
-
},
|
|
161
|
-
'CallExpression > ArrowFunctionExpression, CallExpression > FunctionExpression'(node) {
|
|
162
|
-
if (!isInJSXExpressionContainer(node))
|
|
163
|
-
return;
|
|
164
|
-
let current = node;
|
|
165
|
-
while (current) {
|
|
166
|
-
if (current.type === 'JSXAttribute') {
|
|
167
|
-
const attr = current;
|
|
168
|
-
if (allowInEventHandlers && attr.name.type === 'JSXIdentifier' && attr.name.name.startsWith('on')) {
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
current = current.parent;
|
|
173
|
-
}
|
|
174
|
-
const arrayInfo = isInArrayMethod(node);
|
|
175
|
-
const impact = calculateImpact(arrayInfo);
|
|
176
|
-
context.report({
|
|
177
|
-
node,
|
|
178
|
-
messageId: 'inlineFunction',
|
|
179
|
-
data: {
|
|
180
|
-
impact: impact.severity,
|
|
181
|
-
location: arrayInfo.inArray ? `${arrayInfo.method}() call` : 'JSX prop',
|
|
182
|
-
},
|
|
183
|
-
});
|
|
184
|
-
},
|
|
185
|
-
};
|
|
186
|
-
},
|
|
187
|
-
});
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.reactNoInlineFunctions=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");exports.reactNoInlineFunctions=(0,eslint_devkit_2.createRule)({name:"react-no-inline-functions",meta:{type:"suggestion",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/react-no-inline-functions.md",description:"Prevent inline functions in React renders with performance metrics"},hasSuggestions:true,messages:{inlineFunction:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.PERFORMANCE,issueName:"Inline function",description:"Inline function detected",severity:"MEDIUM",fix:"Use useCallback hook or extract to component method",documentationLink:"https://react.dev/reference/react/useCallback"}),useCallback:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Use useCallback",description:"Wrap with useCallback",severity:"LOW",fix:"const handler = useCallback(() => {}, [deps])",documentationLink:"https://react.dev/reference/react/useCallback"}),extractFunction:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Extract Function",description:"Extract to component method",severity:"LOW",fix:"Define function outside JSX or in useCallback",documentationLink:"https://react.dev/learn/your-first-component"}),strategyUseCallback:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.STRATEGY,issueName:"useCallback Strategy",description:"Use useCallback to memoize",severity:"LOW",fix:"useCallback(() => {}, [dependencies])",documentationLink:"https://react.dev/reference/react/useCallback"}),strategyUseMemo:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.STRATEGY,issueName:"useMemo Strategy",description:"Use useMemo for computed values",severity:"LOW",fix:"useMemo(() => computedValue, [deps])",documentationLink:"https://react.dev/reference/react/useMemo"}),strategyExtract:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.STRATEGY,issueName:"Extract Strategy",description:"Extract function outside render",severity:"LOW",fix:"Define function before return statement",documentationLink:"https://react.dev/learn/your-first-component"})},schema:[{type:"object",properties:{allowInEventHandlers:{type:"boolean",default:false},minArraySize:{type:"number",default:5,description:"Minimum array size to report inline functions in .map()"}},additionalProperties:false}]},defaultOptions:[{allowInEventHandlers:false,minArraySize:5}],create(context){const options=context.options[0]||{};const{allowInEventHandlers=false,minArraySize=10}=options;const isInJSX=node=>{let parent=node.parent;while(parent){if(parent.type==="JSXExpressionContainer"||parent.type==="JSXAttribute"){return true}parent=parent.parent}return false};const isInArrayMethod=node=>{const parent=node.parent;if(parent?.type==="CallExpression"){const callee=parent.callee;if(callee.type==="MemberExpression"&&callee.property.type==="Identifier"){const method=callee.property.name;if(["map","forEach","filter","reduce","sort"].includes(method)){return{inArray:true,method,estimatedSize:minArraySize}}}}return{inArray:false}};const calculateImpact=arrayInfo=>{if(arrayInfo.inArray&&arrayInfo.estimatedSize&&arrayInfo.estimatedSize>50){return{severity:"high",estimatedSlowdown:"30-50ms per render",affectedMetric:"Interaction to Next Paint (INP)"}}return{severity:"medium",estimatedSlowdown:"15-30ms per render",affectedMetric:"Interaction to Next Paint (INP)"}};const isInJSXExpressionContainer=node=>{let current=node;while(current){if(current.type==="JSXExpressionContainer"){return true}current=current.parent}return false};return{"JSXExpressionContainer > ArrowFunctionExpression, JSXExpressionContainer > FunctionExpression"(node){if(!isInJSX(node))return;const parent=node.parent;if(allowInEventHandlers&&parent?.parent?.type==="JSXAttribute"){const attr=parent.parent;if(attr.name.type==="JSXIdentifier"&&attr.name.name.startsWith("on")){return}}const arrayInfo=isInArrayMethod(node);const impact=calculateImpact(arrayInfo);context.report({node,messageId:"inlineFunction",data:{impact:impact.severity,location:arrayInfo.inArray?`${arrayInfo.method}() call`:"JSX prop"}})},"CallExpression > ArrowFunctionExpression, CallExpression > FunctionExpression"(node){if(!isInJSXExpressionContainer(node))return;let current=node;while(current){if(current.type==="JSXAttribute"){const attr=current;if(allowInEventHandlers&&attr.name.type==="JSXIdentifier"&&attr.name.name.startsWith("on")){return}}current=current.parent}const arrayInfo=isInArrayMethod(node);const impact=calculateImpact(arrayInfo);context.report({node,messageId:"inlineFunction",data:{impact:impact.severity,location:arrayInfo.inArray?`${arrayInfo.method}() call`:"JSX prop"}})}}}});
|
|
@@ -1,256 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.reactRenderOptimization = void 0;
|
|
4
|
-
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
-
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
6
|
-
function isInlineObject(node) {
|
|
7
|
-
return node.type === 'ObjectExpression';
|
|
8
|
-
}
|
|
9
|
-
function isInlineArray(node) {
|
|
10
|
-
return node.type === 'ArrayExpression';
|
|
11
|
-
}
|
|
12
|
-
function isInlineArrowFunction(node) {
|
|
13
|
-
return node.type === 'ArrowFunctionExpression';
|
|
14
|
-
}
|
|
15
|
-
function isInlineFunctionExpression(node) {
|
|
16
|
-
return node.type === 'FunctionExpression';
|
|
17
|
-
}
|
|
18
|
-
function estimateComplexity(node) {
|
|
19
|
-
let complexity = 0;
|
|
20
|
-
if (node.type === 'ForStatement' || node.type === 'ForOfStatement' ||
|
|
21
|
-
node.type === 'ForInStatement' || node.type === 'WhileStatement') {
|
|
22
|
-
complexity += 3;
|
|
23
|
-
}
|
|
24
|
-
if (node.type === 'IfStatement' || node.type === 'ConditionalExpression') {
|
|
25
|
-
complexity += 1;
|
|
26
|
-
}
|
|
27
|
-
if (node.type === 'CallExpression') {
|
|
28
|
-
complexity += 1;
|
|
29
|
-
}
|
|
30
|
-
return complexity;
|
|
31
|
-
}
|
|
32
|
-
function isExpensiveComputation(node, minComplexity) {
|
|
33
|
-
if (node.type === 'BinaryExpression' || node.type === 'LogicalExpression') {
|
|
34
|
-
return false;
|
|
35
|
-
}
|
|
36
|
-
let current = node;
|
|
37
|
-
let depth = 0;
|
|
38
|
-
const maxDepth = 5;
|
|
39
|
-
while (current && depth < maxDepth) {
|
|
40
|
-
const complexity = estimateComplexity(current);
|
|
41
|
-
if (complexity >= minComplexity) {
|
|
42
|
-
return true;
|
|
43
|
-
}
|
|
44
|
-
if ('body' in current && current.body) {
|
|
45
|
-
if (Array.isArray(current.body)) {
|
|
46
|
-
for (const child of current.body) {
|
|
47
|
-
if (estimateComplexity(child) >= minComplexity) {
|
|
48
|
-
return true;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
current = current.parent ?? null;
|
|
54
|
-
depth++;
|
|
55
|
-
}
|
|
56
|
-
return false;
|
|
57
|
-
}
|
|
58
|
-
exports.reactRenderOptimization = (0, eslint_devkit_2.createRule)({
|
|
59
|
-
name: 'react-render-optimization',
|
|
60
|
-
meta: {
|
|
61
|
-
type: 'suggestion',
|
|
62
|
-
docs: {
|
|
63
|
-
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/react-render-optimization.md',
|
|
64
|
-
description: 'Detects unnecessary re-renders and expensive computations in React',
|
|
65
|
-
},
|
|
66
|
-
messages: {
|
|
67
|
-
unnecessaryRerender: (0, eslint_devkit_1.formatLLMMessage)({
|
|
68
|
-
icon: eslint_devkit_1.MessageIcons.PERFORMANCE,
|
|
69
|
-
issueName: 'Unnecessary re-render',
|
|
70
|
-
description: 'Inline {{type}} prop causes re-render on every render',
|
|
71
|
-
severity: 'MEDIUM',
|
|
72
|
-
fix: 'Extract to constant or use useMemo/useCallback',
|
|
73
|
-
documentationLink: 'https://react.dev/learn/render-and-commit',
|
|
74
|
-
}),
|
|
75
|
-
missingMemoization: (0, eslint_devkit_1.formatLLMMessage)({
|
|
76
|
-
icon: eslint_devkit_1.MessageIcons.PERFORMANCE,
|
|
77
|
-
issueName: 'Missing memoization',
|
|
78
|
-
description: 'Expensive computation should be memoized',
|
|
79
|
-
severity: 'MEDIUM',
|
|
80
|
-
fix: 'Wrap computation in useMemo hook',
|
|
81
|
-
documentationLink: 'https://react.dev/reference/react/useMemo',
|
|
82
|
-
}),
|
|
83
|
-
expensiveComputation: (0, eslint_devkit_1.formatLLMMessage)({
|
|
84
|
-
icon: eslint_devkit_1.MessageIcons.PERFORMANCE,
|
|
85
|
-
issueName: 'Expensive computation',
|
|
86
|
-
description: 'Complex computation detected (complexity: {{complexity}})',
|
|
87
|
-
severity: 'MEDIUM',
|
|
88
|
-
fix: 'Use useMemo to memoize result',
|
|
89
|
-
documentationLink: 'https://react.dev/reference/react/useMemo',
|
|
90
|
-
}),
|
|
91
|
-
useMemo: (0, eslint_devkit_1.formatLLMMessage)({
|
|
92
|
-
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
93
|
-
issueName: 'Use useMemo',
|
|
94
|
-
description: 'Memoize with useMemo',
|
|
95
|
-
severity: 'LOW',
|
|
96
|
-
fix: 'const memoized = useMemo(() => value, [deps])',
|
|
97
|
-
documentationLink: 'https://react.dev/reference/react/useMemo',
|
|
98
|
-
}),
|
|
99
|
-
useCallback: (0, eslint_devkit_1.formatLLMMessage)({
|
|
100
|
-
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
101
|
-
issueName: 'Use useCallback',
|
|
102
|
-
description: 'Memoize callback function',
|
|
103
|
-
severity: 'LOW',
|
|
104
|
-
fix: 'const handler = useCallback(() => {}, [deps])',
|
|
105
|
-
documentationLink: 'https://react.dev/reference/react/useCallback',
|
|
106
|
-
}),
|
|
107
|
-
useReactMemo: (0, eslint_devkit_1.formatLLMMessage)({
|
|
108
|
-
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
109
|
-
issueName: 'Use React.memo',
|
|
110
|
-
description: 'Wrap component with React.memo',
|
|
111
|
-
severity: 'LOW',
|
|
112
|
-
fix: 'export default React.memo(Component)',
|
|
113
|
-
documentationLink: 'https://react.dev/reference/react/memo',
|
|
114
|
-
}),
|
|
115
|
-
},
|
|
116
|
-
schema: [
|
|
117
|
-
{
|
|
118
|
-
type: 'object',
|
|
119
|
-
properties: {
|
|
120
|
-
detectInlineProps: {
|
|
121
|
-
type: 'boolean',
|
|
122
|
-
default: true,
|
|
123
|
-
description: 'Detect inline object/array props',
|
|
124
|
-
},
|
|
125
|
-
detectInlineFunctions: {
|
|
126
|
-
type: 'boolean',
|
|
127
|
-
default: true,
|
|
128
|
-
description: 'Detect inline function props',
|
|
129
|
-
},
|
|
130
|
-
detectExpensiveComputations: {
|
|
131
|
-
type: 'boolean',
|
|
132
|
-
default: true,
|
|
133
|
-
description: 'Detect expensive computations without useMemo',
|
|
134
|
-
},
|
|
135
|
-
minComputationComplexity: {
|
|
136
|
-
type: 'number',
|
|
137
|
-
default: 3,
|
|
138
|
-
minimum: 1,
|
|
139
|
-
description: 'Minimum computation complexity to flag',
|
|
140
|
-
},
|
|
141
|
-
ignoreInTests: {
|
|
142
|
-
type: 'boolean',
|
|
143
|
-
default: true,
|
|
144
|
-
description: 'Ignore in test files',
|
|
145
|
-
},
|
|
146
|
-
},
|
|
147
|
-
additionalProperties: false,
|
|
148
|
-
},
|
|
149
|
-
],
|
|
150
|
-
},
|
|
151
|
-
defaultOptions: [
|
|
152
|
-
{
|
|
153
|
-
detectInlineProps: true,
|
|
154
|
-
detectInlineFunctions: true,
|
|
155
|
-
detectExpensiveComputations: true,
|
|
156
|
-
minComputationComplexity: 3,
|
|
157
|
-
ignoreInTests: true,
|
|
158
|
-
},
|
|
159
|
-
],
|
|
160
|
-
create(context, [options = {}]) {
|
|
161
|
-
const { detectInlineProps = true, detectInlineFunctions = true, detectExpensiveComputations = true, minComputationComplexity = 3, ignoreInTests = true, } = options || {};
|
|
162
|
-
const filename = context.filename;
|
|
163
|
-
const isTestFile = ignoreInTests && /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
|
|
164
|
-
if (isTestFile) {
|
|
165
|
-
return {};
|
|
166
|
-
}
|
|
167
|
-
function checkJSXAttribute(node) {
|
|
168
|
-
try {
|
|
169
|
-
if (!node.value) {
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
let value = null;
|
|
173
|
-
if (node.value.type === 'JSXExpressionContainer') {
|
|
174
|
-
value = node.value.expression;
|
|
175
|
-
}
|
|
176
|
-
else if (node.value.type === 'Literal') {
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
if (!value) {
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
if (detectInlineProps && isInlineObject(value)) {
|
|
183
|
-
context.report({
|
|
184
|
-
node,
|
|
185
|
-
messageId: 'unnecessaryRerender',
|
|
186
|
-
data: {
|
|
187
|
-
type: 'object',
|
|
188
|
-
},
|
|
189
|
-
suggest: [
|
|
190
|
-
{
|
|
191
|
-
messageId: 'useMemo',
|
|
192
|
-
fix: () => null,
|
|
193
|
-
},
|
|
194
|
-
],
|
|
195
|
-
});
|
|
196
|
-
return;
|
|
197
|
-
}
|
|
198
|
-
if (detectInlineProps && isInlineArray(value)) {
|
|
199
|
-
context.report({
|
|
200
|
-
node,
|
|
201
|
-
messageId: 'unnecessaryRerender',
|
|
202
|
-
data: {
|
|
203
|
-
type: 'array',
|
|
204
|
-
},
|
|
205
|
-
suggest: [
|
|
206
|
-
{
|
|
207
|
-
messageId: 'useMemo',
|
|
208
|
-
fix: () => null,
|
|
209
|
-
},
|
|
210
|
-
],
|
|
211
|
-
});
|
|
212
|
-
return;
|
|
213
|
-
}
|
|
214
|
-
if (detectInlineFunctions &&
|
|
215
|
-
(isInlineArrowFunction(value) || isInlineFunctionExpression(value))) {
|
|
216
|
-
context.report({
|
|
217
|
-
node,
|
|
218
|
-
messageId: 'unnecessaryRerender',
|
|
219
|
-
data: {
|
|
220
|
-
type: 'function',
|
|
221
|
-
},
|
|
222
|
-
suggest: [
|
|
223
|
-
{
|
|
224
|
-
messageId: 'useCallback',
|
|
225
|
-
fix: () => null,
|
|
226
|
-
},
|
|
227
|
-
],
|
|
228
|
-
});
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
231
|
-
if (detectExpensiveComputations && isExpensiveComputation(value, minComputationComplexity)) {
|
|
232
|
-
const complexity = estimateComplexity(value);
|
|
233
|
-
context.report({
|
|
234
|
-
node,
|
|
235
|
-
messageId: 'expensiveComputation',
|
|
236
|
-
data: {
|
|
237
|
-
complexity: String(complexity),
|
|
238
|
-
},
|
|
239
|
-
suggest: [
|
|
240
|
-
{
|
|
241
|
-
messageId: 'useMemo',
|
|
242
|
-
fix: () => null,
|
|
243
|
-
},
|
|
244
|
-
],
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
catch {
|
|
249
|
-
return;
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
return {
|
|
253
|
-
JSXAttribute: checkJSXAttribute,
|
|
254
|
-
};
|
|
255
|
-
},
|
|
256
|
-
});
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.reactRenderOptimization=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");function isInlineObject(node){return node.type==="ObjectExpression"}function isInlineArray(node){return node.type==="ArrayExpression"}function isInlineArrowFunction(node){return node.type==="ArrowFunctionExpression"}function isInlineFunctionExpression(node){return node.type==="FunctionExpression"}function estimateComplexity(node){let complexity=0;if(node.type==="ForStatement"||node.type==="ForOfStatement"||node.type==="ForInStatement"||node.type==="WhileStatement"){complexity+=3}if(node.type==="IfStatement"||node.type==="ConditionalExpression"){complexity+=1}if(node.type==="CallExpression"){complexity+=1}return complexity}function isExpensiveComputation(node,minComplexity){if(node.type==="BinaryExpression"||node.type==="LogicalExpression"){return false}let current=node;let depth=0;const maxDepth=5;while(current&&depth<maxDepth){const complexity=estimateComplexity(current);if(complexity>=minComplexity){return true}if("body"in current&¤t.body){if(Array.isArray(current.body)){for(const child of current.body){if(estimateComplexity(child)>=minComplexity){return true}}}}current=current.parent??null;depth++}return false}exports.reactRenderOptimization=(0,eslint_devkit_2.createRule)({name:"react-render-optimization",meta:{type:"suggestion",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/react-render-optimization.md",description:"Detects unnecessary re-renders and expensive computations in React"},messages:{unnecessaryRerender:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.PERFORMANCE,issueName:"Unnecessary re-render",description:"Inline {{type}} prop causes re-render on every render",severity:"MEDIUM",fix:"Extract to constant or use useMemo/useCallback",documentationLink:"https://react.dev/learn/render-and-commit"}),missingMemoization:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.PERFORMANCE,issueName:"Missing memoization",description:"Expensive computation should be memoized",severity:"MEDIUM",fix:"Wrap computation in useMemo hook",documentationLink:"https://react.dev/reference/react/useMemo"}),expensiveComputation:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.PERFORMANCE,issueName:"Expensive computation",description:"Complex computation detected (complexity: {{complexity}})",severity:"MEDIUM",fix:"Use useMemo to memoize result",documentationLink:"https://react.dev/reference/react/useMemo"}),useMemo:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Use useMemo",description:"Memoize with useMemo",severity:"LOW",fix:"const memoized = useMemo(() => value, [deps])",documentationLink:"https://react.dev/reference/react/useMemo"}),useCallback:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Use useCallback",description:"Memoize callback function",severity:"LOW",fix:"const handler = useCallback(() => {}, [deps])",documentationLink:"https://react.dev/reference/react/useCallback"}),useReactMemo:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Use React.memo",description:"Wrap component with React.memo",severity:"LOW",fix:"export default React.memo(Component)",documentationLink:"https://react.dev/reference/react/memo"})},schema:[{type:"object",properties:{detectInlineProps:{type:"boolean",default:true,description:"Detect inline object/array props"},detectInlineFunctions:{type:"boolean",default:true,description:"Detect inline function props"},detectExpensiveComputations:{type:"boolean",default:true,description:"Detect expensive computations without useMemo"},minComputationComplexity:{type:"number",default:3,minimum:1,description:"Minimum computation complexity to flag"},ignoreInTests:{type:"boolean",default:true,description:"Ignore in test files"}},additionalProperties:false}]},defaultOptions:[{detectInlineProps:true,detectInlineFunctions:true,detectExpensiveComputations:true,minComputationComplexity:3,ignoreInTests:true}],create(context,[options={}]){const{detectInlineProps=true,detectInlineFunctions=true,detectExpensiveComputations=true,minComputationComplexity=3,ignoreInTests=true}=options||{};const filename=context.filename;const isTestFile=ignoreInTests&&/\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);if(isTestFile){return{}}function checkJSXAttribute(node){try{if(!node.value){return}let value=null;if(node.value.type==="JSXExpressionContainer"){value=node.value.expression}else if(node.value.type==="Literal"){return}if(!value){return}if(detectInlineProps&&isInlineObject(value)){context.report({node,messageId:"unnecessaryRerender",data:{type:"object"},suggest:[{messageId:"useMemo",fix:()=>null}]});return}if(detectInlineProps&&isInlineArray(value)){context.report({node,messageId:"unnecessaryRerender",data:{type:"array"},suggest:[{messageId:"useMemo",fix:()=>null}]});return}if(detectInlineFunctions&&(isInlineArrowFunction(value)||isInlineFunctionExpression(value))){context.report({node,messageId:"unnecessaryRerender",data:{type:"function"},suggest:[{messageId:"useCallback",fix:()=>null}]});return}if(detectExpensiveComputations&&isExpensiveComputation(value,minComputationComplexity)){const complexity=estimateComplexity(value);context.report({node,messageId:"expensiveComputation",data:{complexity:String(complexity)},suggest:[{messageId:"useMemo",fix:()=>null}]})}}catch{return}}return{JSXAttribute:checkJSXAttribute}}});
|
|
@@ -1,68 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.checkedRequiresOnchangeOrReadonly = void 0;
|
|
4
|
-
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
-
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
6
|
-
exports.checkedRequiresOnchangeOrReadonly = (0, eslint_devkit_1.createRule)({
|
|
7
|
-
name: 'checked-requires-onchange-or-readonly',
|
|
8
|
-
meta: {
|
|
9
|
-
type: 'problem',
|
|
10
|
-
docs: {
|
|
11
|
-
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/checked-requires-onchange-or-readonly.md',
|
|
12
|
-
description: 'Ensure controlled inputs have onChange or readOnly',
|
|
13
|
-
},
|
|
14
|
-
schema: [],
|
|
15
|
-
messages: {
|
|
16
|
-
checkedRequiresOnChangeOrReadOnly: (0, eslint_devkit_2.formatLLMMessage)({
|
|
17
|
-
icon: eslint_devkit_2.MessageIcons.WARNING,
|
|
18
|
-
issueName: 'Controlled Input Missing Handler',
|
|
19
|
-
description: 'Checked input missing onChange or readOnly',
|
|
20
|
-
severity: 'MEDIUM',
|
|
21
|
-
fix: 'Add onChange handler or readOnly attribute',
|
|
22
|
-
documentationLink: 'https://react.dev/reference/react-dom/components/input#controlling-an-input-with-a-state-variable',
|
|
23
|
-
}),
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
defaultOptions: [],
|
|
27
|
-
create(context) {
|
|
28
|
-
return {
|
|
29
|
-
JSXElement(node) {
|
|
30
|
-
if (node.openingElement.name.type !== 'JSXIdentifier') {
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
const tagName = node.openingElement.name.name;
|
|
34
|
-
if (!['input', 'textarea', 'select'].includes(tagName)) {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
const attributes = node.openingElement.attributes;
|
|
38
|
-
let hasChecked = false;
|
|
39
|
-
let hasValue = false;
|
|
40
|
-
let hasOnChange = false;
|
|
41
|
-
let hasReadOnly = false;
|
|
42
|
-
for (const attr of attributes) {
|
|
43
|
-
if (attr.type !== 'JSXAttribute')
|
|
44
|
-
continue;
|
|
45
|
-
const attrName = attr.name.type === 'JSXIdentifier' ? attr.name.name : '';
|
|
46
|
-
if (attrName === 'checked') {
|
|
47
|
-
hasChecked = true;
|
|
48
|
-
}
|
|
49
|
-
else if (attrName === 'value') {
|
|
50
|
-
hasValue = true;
|
|
51
|
-
}
|
|
52
|
-
else if (attrName === 'onChange' || attrName === 'onInput') {
|
|
53
|
-
hasOnChange = true;
|
|
54
|
-
}
|
|
55
|
-
else if (attrName === 'readOnly') {
|
|
56
|
-
hasReadOnly = true;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
if ((hasChecked || hasValue) && !hasOnChange && !hasReadOnly) {
|
|
60
|
-
context.report({
|
|
61
|
-
node: node.openingElement.name,
|
|
62
|
-
messageId: 'checkedRequiresOnChangeOrReadOnly',
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
},
|
|
66
|
-
};
|
|
67
|
-
},
|
|
68
|
-
});
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.checkedRequiresOnchangeOrReadonly=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");exports.checkedRequiresOnchangeOrReadonly=(0,eslint_devkit_1.createRule)({name:"checked-requires-onchange-or-readonly",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/checked-requires-onchange-or-readonly.md",description:"Ensure controlled inputs have onChange or readOnly"},schema:[],messages:{checkedRequiresOnChangeOrReadOnly:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.WARNING,issueName:"Controlled Input Missing Handler",description:"Checked input missing onChange or readOnly",severity:"MEDIUM",fix:"Add onChange handler or readOnly attribute",documentationLink:"https://react.dev/reference/react-dom/components/input#controlling-an-input-with-a-state-variable"})}},defaultOptions:[],create(context){return{JSXElement(node){if(node.openingElement.name.type!=="JSXIdentifier"){return}const tagName=node.openingElement.name.name;if(!["input","textarea","select"].includes(tagName)){return}const attributes=node.openingElement.attributes;let hasChecked=false;let hasValue=false;let hasOnChange=false;let hasReadOnly=false;for(const attr of attributes){if(attr.type!=="JSXAttribute")continue;const attrName=attr.name.type==="JSXIdentifier"?attr.name.name:"";if(attrName==="checked"){hasChecked=true}else if(attrName==="value"){hasValue=true}else if(attrName==="onChange"||attrName==="onInput"){hasOnChange=true}else if(attrName==="readOnly"){hasReadOnly=true}}if((hasChecked||hasValue)&&!hasOnChange&&!hasReadOnly){context.report({node:node.openingElement.name,messageId:"checkedRequiresOnChangeOrReadOnly"})}}}}});
|
|
@@ -1,99 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.defaultPropsMatchPropTypes = void 0;
|
|
4
|
-
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
-
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
6
|
-
exports.defaultPropsMatchPropTypes = (0, eslint_devkit_1.createRule)({
|
|
7
|
-
name: 'default-props-match-prop-types',
|
|
8
|
-
meta: {
|
|
9
|
-
type: 'problem',
|
|
10
|
-
docs: {
|
|
11
|
-
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/default-props-match-prop-types.md',
|
|
12
|
-
description: 'Validate default props match prop types',
|
|
13
|
-
},
|
|
14
|
-
schema: [],
|
|
15
|
-
messages: {
|
|
16
|
-
defaultPropsMatchPropTypes: (0, eslint_devkit_2.formatLLMMessage)({
|
|
17
|
-
icon: eslint_devkit_2.MessageIcons.WARNING,
|
|
18
|
-
issueName: 'Default Props Mismatch',
|
|
19
|
-
description: 'Default prop does not match prop type',
|
|
20
|
-
severity: 'MEDIUM',
|
|
21
|
-
fix: 'Update default prop to match propTypes or update propTypes',
|
|
22
|
-
documentationLink: 'https://react.dev/learn/components-and-props#default-props',
|
|
23
|
-
}),
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
defaultOptions: [],
|
|
27
|
-
create(context) {
|
|
28
|
-
const propTypes = new Map();
|
|
29
|
-
const defaultProps = new Map();
|
|
30
|
-
return {
|
|
31
|
-
'PropertyDefinition[key.name="propTypes"]'(node) {
|
|
32
|
-
if (node.value && node.value.type === 'ObjectExpression') {
|
|
33
|
-
for (const prop of node.value.properties) {
|
|
34
|
-
if (prop.type === 'Property' && prop.key.type === 'Identifier') {
|
|
35
|
-
propTypes.set(prop.key.name, prop);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
},
|
|
40
|
-
'PropertyDefinition[key.name="defaultProps"]'(node) {
|
|
41
|
-
if (node.value && node.value.type === 'ObjectExpression') {
|
|
42
|
-
for (const prop of node.value.properties) {
|
|
43
|
-
if (prop.type === 'Property' && prop.key.type === 'Identifier') {
|
|
44
|
-
defaultProps.set(prop.key.name, prop);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
},
|
|
49
|
-
'ClassDeclaration:exit'() {
|
|
50
|
-
for (const [propName, defaultProp] of defaultProps) {
|
|
51
|
-
const propType = propTypes.get(propName);
|
|
52
|
-
if (propType &&
|
|
53
|
-
defaultProp.value &&
|
|
54
|
-
propType.value &&
|
|
55
|
-
defaultProp.value.type !== 'TSEmptyBodyFunctionExpression' &&
|
|
56
|
-
propType.value.type !== 'TSEmptyBodyFunctionExpression' &&
|
|
57
|
-
!isPattern(defaultProp.value) &&
|
|
58
|
-
!isPattern(propType.value) &&
|
|
59
|
-
!isCompatibleDefaultValue(defaultProp.value, propType.value)) {
|
|
60
|
-
context.report({
|
|
61
|
-
node: defaultProp.key,
|
|
62
|
-
messageId: 'defaultPropsMatchPropTypes',
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
propTypes.clear();
|
|
67
|
-
defaultProps.clear();
|
|
68
|
-
},
|
|
69
|
-
};
|
|
70
|
-
},
|
|
71
|
-
});
|
|
72
|
-
function isCompatibleDefaultValue(defaultValue, propType) {
|
|
73
|
-
if (defaultValue.type === 'Literal' && propType.type === 'MemberExpression') {
|
|
74
|
-
const propTypeName = getPropTypeName(propType);
|
|
75
|
-
const defaultType = typeof defaultValue.value;
|
|
76
|
-
switch (propTypeName) {
|
|
77
|
-
case 'string':
|
|
78
|
-
return defaultType === 'string';
|
|
79
|
-
case 'number':
|
|
80
|
-
return defaultType === 'number';
|
|
81
|
-
case 'bool':
|
|
82
|
-
return defaultType === 'boolean';
|
|
83
|
-
default:
|
|
84
|
-
return true;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return true;
|
|
88
|
-
}
|
|
89
|
-
function getPropTypeName(node) {
|
|
90
|
-
if (node.object.type === 'Identifier' &&
|
|
91
|
-
node.object.name === 'PropTypes' &&
|
|
92
|
-
node.property.type === 'Identifier') {
|
|
93
|
-
return node.property.name;
|
|
94
|
-
}
|
|
95
|
-
return '';
|
|
96
|
-
}
|
|
97
|
-
function isPattern(node) {
|
|
98
|
-
return node.type === 'AssignmentPattern' || node.type === 'ArrayPattern' || node.type === 'ObjectPattern';
|
|
99
|
-
}
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.defaultPropsMatchPropTypes=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");exports.defaultPropsMatchPropTypes=(0,eslint_devkit_1.createRule)({name:"default-props-match-prop-types",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/default-props-match-prop-types.md",description:"Validate default props match prop types"},schema:[],messages:{defaultPropsMatchPropTypes:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.WARNING,issueName:"Default Props Mismatch",description:"Default prop does not match prop type",severity:"MEDIUM",fix:"Update default prop to match propTypes or update propTypes",documentationLink:"https://react.dev/learn/components-and-props#default-props"})}},defaultOptions:[],create(context){const propTypes=new Map;const defaultProps=new Map;return{'PropertyDefinition[key.name="propTypes"]'(node){if(node.value&&node.value.type==="ObjectExpression"){for(const prop of node.value.properties){if(prop.type==="Property"&&prop.key.type==="Identifier"){propTypes.set(prop.key.name,prop)}}}},'PropertyDefinition[key.name="defaultProps"]'(node){if(node.value&&node.value.type==="ObjectExpression"){for(const prop of node.value.properties){if(prop.type==="Property"&&prop.key.type==="Identifier"){defaultProps.set(prop.key.name,prop)}}}},"ClassDeclaration:exit"(){for(const[propName,defaultProp]of defaultProps){const propType=propTypes.get(propName);if(propType&&defaultProp.value&&propType.value&&defaultProp.value.type!=="TSEmptyBodyFunctionExpression"&&propType.value.type!=="TSEmptyBodyFunctionExpression"&&!isPattern(defaultProp.value)&&!isPattern(propType.value)&&!isCompatibleDefaultValue(defaultProp.value,propType.value)){context.report({node:defaultProp.key,messageId:"defaultPropsMatchPropTypes"})}}propTypes.clear();defaultProps.clear()}}}});function isCompatibleDefaultValue(defaultValue,propType){if(defaultValue.type==="Literal"&&propType.type==="MemberExpression"){const propTypeName=getPropTypeName(propType);const defaultType=typeof defaultValue.value;switch(propTypeName){case"string":return defaultType==="string";case"number":return defaultType==="number";case"bool":return defaultType==="boolean";default:return true}}return true}function getPropTypeName(node){if(node.object.type==="Identifier"&&node.object.name==="PropTypes"&&node.property.type==="Identifier"){return node.property.name}return""}function isPattern(node){return node.type==="AssignmentPattern"||node.type==="ArrayPattern"||node.type==="ObjectPattern"}
|