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,212 @@
|
|
|
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.requiredAttributes = void 0;
|
|
9
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
+
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
11
|
+
exports.requiredAttributes = (0, eslint_devkit_2.createRule)({
|
|
12
|
+
name: 'required-attributes',
|
|
13
|
+
meta: {
|
|
14
|
+
type: 'problem',
|
|
15
|
+
docs: {
|
|
16
|
+
description: 'Enforce required attributes on React components with customizable ignore lists',
|
|
17
|
+
},
|
|
18
|
+
fixable: 'code',
|
|
19
|
+
hasSuggestions: true,
|
|
20
|
+
messages: {
|
|
21
|
+
// 🎯 Token optimization: 41% reduction (54→32 tokens) - required attributes for form/a11y
|
|
22
|
+
missingAttribute: (0, eslint_devkit_1.formatLLMMessage)({
|
|
23
|
+
icon: '📝',
|
|
24
|
+
issueName: 'Missing required attribute',
|
|
25
|
+
cwe: 'CWE-252',
|
|
26
|
+
description: '{{element}} missing {{attribute}}',
|
|
27
|
+
severity: 'MEDIUM',
|
|
28
|
+
fix: 'Add {{attribute}}="{{suggestedValue}}" ({{purpose}})',
|
|
29
|
+
documentationLink: 'https://www.w3.org/WAI/fundamentals/accessibility-intro/',
|
|
30
|
+
}),
|
|
31
|
+
addAttribute: (0, eslint_devkit_1.formatLLMMessage)({
|
|
32
|
+
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
33
|
+
issueName: 'Add Attribute',
|
|
34
|
+
description: 'Add required attribute',
|
|
35
|
+
severity: 'LOW',
|
|
36
|
+
fix: '{{attribute}}="{{suggestedValue}}"',
|
|
37
|
+
documentationLink: 'https://www.w3.org/WAI/fundamentals/accessibility-intro/',
|
|
38
|
+
}),
|
|
39
|
+
},
|
|
40
|
+
schema: [
|
|
41
|
+
{
|
|
42
|
+
type: 'object',
|
|
43
|
+
properties: {
|
|
44
|
+
attributes: {
|
|
45
|
+
type: 'array',
|
|
46
|
+
items: {
|
|
47
|
+
type: 'object',
|
|
48
|
+
properties: {
|
|
49
|
+
attribute: { type: 'string' },
|
|
50
|
+
ignoreTags: {
|
|
51
|
+
type: 'array',
|
|
52
|
+
items: { type: 'string' },
|
|
53
|
+
},
|
|
54
|
+
message: { type: 'string' },
|
|
55
|
+
suggestedValue: { type: 'string' },
|
|
56
|
+
},
|
|
57
|
+
required: ['attribute'],
|
|
58
|
+
},
|
|
59
|
+
default: [],
|
|
60
|
+
},
|
|
61
|
+
ignoreComponents: {
|
|
62
|
+
type: 'array',
|
|
63
|
+
items: { type: 'string' },
|
|
64
|
+
default: [],
|
|
65
|
+
description: 'Components to ignore globally',
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
additionalProperties: false,
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
},
|
|
72
|
+
defaultOptions: [
|
|
73
|
+
{
|
|
74
|
+
attributes: [],
|
|
75
|
+
ignoreComponents: [],
|
|
76
|
+
},
|
|
77
|
+
],
|
|
78
|
+
create(context) {
|
|
79
|
+
const options = context.options[0] || {};
|
|
80
|
+
const { attributes = [], ignoreComponents = [] } = options || {};
|
|
81
|
+
/**
|
|
82
|
+
* Get element name from JSX opening element
|
|
83
|
+
*/
|
|
84
|
+
const getElementName = (node) => {
|
|
85
|
+
if (node.name.type === 'JSXIdentifier') {
|
|
86
|
+
return node.name.name;
|
|
87
|
+
}
|
|
88
|
+
if (node.name.type === 'JSXMemberExpression') {
|
|
89
|
+
// Handle cases like <Box.Item>
|
|
90
|
+
let current = node.name;
|
|
91
|
+
const parts = [];
|
|
92
|
+
while (current.type === 'JSXMemberExpression') {
|
|
93
|
+
if (current.property.type === 'JSXIdentifier') {
|
|
94
|
+
parts.unshift(current.property.name);
|
|
95
|
+
}
|
|
96
|
+
current = current.object;
|
|
97
|
+
}
|
|
98
|
+
if (current.type === 'JSXIdentifier') {
|
|
99
|
+
parts.unshift(current.name);
|
|
100
|
+
}
|
|
101
|
+
return parts.join('.');
|
|
102
|
+
}
|
|
103
|
+
return null;
|
|
104
|
+
};
|
|
105
|
+
/**
|
|
106
|
+
* Check if element has attribute
|
|
107
|
+
*/
|
|
108
|
+
const hasAttribute = (node, attrName) => {
|
|
109
|
+
return node.attributes.some((attr) => {
|
|
110
|
+
if (attr.type !== 'JSXAttribute')
|
|
111
|
+
return false;
|
|
112
|
+
if (attr.name.type !== 'JSXIdentifier')
|
|
113
|
+
return false;
|
|
114
|
+
return attr.name.name === attrName;
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* Get attribute purpose based on name
|
|
119
|
+
*/
|
|
120
|
+
const getAttributePurpose = (attribute) => {
|
|
121
|
+
const purposes = {
|
|
122
|
+
'data-testid': 'Testing & QA automation',
|
|
123
|
+
'aria-label': 'Accessibility for screen readers',
|
|
124
|
+
'aria-describedby': 'Accessibility description',
|
|
125
|
+
'aria-labelledby': 'Accessibility label reference',
|
|
126
|
+
'role': 'Accessibility role definition',
|
|
127
|
+
'tabIndex': 'Keyboard navigation order',
|
|
128
|
+
'alt': 'Image accessibility',
|
|
129
|
+
'title': 'Tooltip / additional context',
|
|
130
|
+
'id': 'Element identification',
|
|
131
|
+
};
|
|
132
|
+
return purposes[attribute] || 'Required for consistency';
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* Get default suggested value for attribute
|
|
136
|
+
*/
|
|
137
|
+
/**
|
|
138
|
+
* Check if an element is a form element that should have type/name attributes
|
|
139
|
+
*/
|
|
140
|
+
const isFormElement = (elementName) => {
|
|
141
|
+
return ['input', 'button', 'select', 'textarea'].includes(elementName.toLowerCase());
|
|
142
|
+
};
|
|
143
|
+
const getDefaultSuggestedValue = (attribute, elementName) => {
|
|
144
|
+
if (attribute === 'data-testid') {
|
|
145
|
+
// Generate kebab-case testid from element name
|
|
146
|
+
const kebab = elementName.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
|
|
147
|
+
return kebab;
|
|
148
|
+
}
|
|
149
|
+
if (attribute.startsWith('aria-')) {
|
|
150
|
+
return 'TODO: Add descriptive label';
|
|
151
|
+
}
|
|
152
|
+
if (attribute === 'tabIndex') {
|
|
153
|
+
return '0';
|
|
154
|
+
}
|
|
155
|
+
return 'TODO';
|
|
156
|
+
};
|
|
157
|
+
return {
|
|
158
|
+
JSXOpeningElement(node) {
|
|
159
|
+
const elementName = getElementName(node);
|
|
160
|
+
if (!elementName)
|
|
161
|
+
return;
|
|
162
|
+
// Check if element is in global ignore list
|
|
163
|
+
if (ignoreComponents.includes(elementName)) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
// Check each required attribute
|
|
167
|
+
for (const attrRule of attributes) {
|
|
168
|
+
const { attribute, ignoreTags = [], message, suggestedValue } = attrRule;
|
|
169
|
+
// Check if this element should be ignored for this attribute
|
|
170
|
+
if (ignoreTags.includes(elementName)) {
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
// SKIP: type/name attributes only apply to form elements
|
|
174
|
+
if ((attribute === 'type' || attribute === 'name') && !isFormElement(elementName)) {
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
// Check if attribute exists
|
|
178
|
+
if (hasAttribute(node, attribute)) {
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
// Attribute is missing - report violation
|
|
182
|
+
const purpose = message || getAttributePurpose(attribute);
|
|
183
|
+
const defaultValue = suggestedValue || getDefaultSuggestedValue(attribute, elementName);
|
|
184
|
+
context.report({
|
|
185
|
+
node,
|
|
186
|
+
messageId: 'missingAttribute',
|
|
187
|
+
data: {
|
|
188
|
+
element: elementName,
|
|
189
|
+
attribute,
|
|
190
|
+
suggestedValue: defaultValue,
|
|
191
|
+
purpose,
|
|
192
|
+
},
|
|
193
|
+
suggest: [
|
|
194
|
+
{
|
|
195
|
+
messageId: 'addAttribute',
|
|
196
|
+
data: {
|
|
197
|
+
attribute,
|
|
198
|
+
suggestedValue: defaultValue,
|
|
199
|
+
},
|
|
200
|
+
fix: (fixer) => {
|
|
201
|
+
// Insert attribute after element name
|
|
202
|
+
const nameNode = node.name;
|
|
203
|
+
return fixer.insertTextAfter(nameNode, ` ${attribute}="${defaultValue}"`);
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
],
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
},
|
|
212
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
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: sort-comp
|
|
8
|
+
* Enforce component method ordering
|
|
9
|
+
*/
|
|
10
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
11
|
+
export interface Options {
|
|
12
|
+
order?: string[];
|
|
13
|
+
groups?: Record<string, string[]>;
|
|
14
|
+
}
|
|
15
|
+
export declare const sortComp: TSESLint.RuleModule<"sortComp", [Options], unknown, TSESLint.RuleListener> & {
|
|
16
|
+
name: string;
|
|
17
|
+
};
|
|
@@ -0,0 +1,160 @@
|
|
|
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.sortComp = void 0;
|
|
9
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
+
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
11
|
+
const DEFAULT_ORDER = [
|
|
12
|
+
'static-variables',
|
|
13
|
+
'static-methods',
|
|
14
|
+
'instance-variables',
|
|
15
|
+
'lifecycle',
|
|
16
|
+
'everything-else',
|
|
17
|
+
'render'
|
|
18
|
+
];
|
|
19
|
+
const LIFECYCLE_METHODS = [
|
|
20
|
+
'constructor',
|
|
21
|
+
'getDerivedStateFromProps',
|
|
22
|
+
'componentWillMount',
|
|
23
|
+
'UNSAFE_componentWillMount',
|
|
24
|
+
'componentDidMount',
|
|
25
|
+
'componentWillReceiveProps',
|
|
26
|
+
'UNSAFE_componentWillReceiveProps',
|
|
27
|
+
'shouldComponentUpdate',
|
|
28
|
+
'componentWillUpdate',
|
|
29
|
+
'UNSAFE_componentWillUpdate',
|
|
30
|
+
'getSnapshotBeforeUpdate',
|
|
31
|
+
'componentDidUpdate',
|
|
32
|
+
'componentDidCatch',
|
|
33
|
+
'componentWillUnmount',
|
|
34
|
+
];
|
|
35
|
+
exports.sortComp = (0, eslint_devkit_1.createRule)({
|
|
36
|
+
name: 'sort-comp',
|
|
37
|
+
meta: {
|
|
38
|
+
type: 'problem',
|
|
39
|
+
docs: {
|
|
40
|
+
description: 'Enforce component method ordering',
|
|
41
|
+
},
|
|
42
|
+
messages: {
|
|
43
|
+
sortComp: (0, eslint_devkit_2.formatLLMMessage)({
|
|
44
|
+
icon: eslint_devkit_2.MessageIcons.WARNING,
|
|
45
|
+
issueName: 'Method Order Violation',
|
|
46
|
+
description: 'Component methods are not in the correct order',
|
|
47
|
+
severity: 'LOW',
|
|
48
|
+
fix: 'Reorder methods according to component lifecycle',
|
|
49
|
+
documentationLink: 'https://react.dev/reference/react/Component',
|
|
50
|
+
}),
|
|
51
|
+
},
|
|
52
|
+
schema: [
|
|
53
|
+
{
|
|
54
|
+
type: 'object',
|
|
55
|
+
properties: {
|
|
56
|
+
order: {
|
|
57
|
+
type: 'array',
|
|
58
|
+
items: { type: 'string' },
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
additionalProperties: false,
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
},
|
|
65
|
+
defaultOptions: [{ order: DEFAULT_ORDER }],
|
|
66
|
+
create(context) {
|
|
67
|
+
const [options] = context.options;
|
|
68
|
+
const order = options?.order ?? DEFAULT_ORDER;
|
|
69
|
+
return {
|
|
70
|
+
ClassDeclaration(node) {
|
|
71
|
+
if (isReactComponent(node)) {
|
|
72
|
+
checkMethodOrder(node, order);
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
function isReactComponent(node) {
|
|
77
|
+
if (!node.superClass)
|
|
78
|
+
return false;
|
|
79
|
+
if (node.superClass.type === 'Identifier') {
|
|
80
|
+
return node.superClass.name === 'Component' || node.superClass.name === 'PureComponent';
|
|
81
|
+
}
|
|
82
|
+
if (node.superClass.type === 'MemberExpression') {
|
|
83
|
+
return (node.superClass.object.type === 'Identifier' &&
|
|
84
|
+
node.superClass.object.name === 'React' &&
|
|
85
|
+
node.superClass.property.type === 'Identifier' &&
|
|
86
|
+
(node.superClass.property.name === 'Component' || node.superClass.property.name === 'PureComponent'));
|
|
87
|
+
}
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
function checkMethodOrder(node, order) {
|
|
91
|
+
const members = node.body.body;
|
|
92
|
+
const methodOrder = [];
|
|
93
|
+
// Collect all methods/properties with their order index
|
|
94
|
+
for (let i = 0; i < members.length; i++) {
|
|
95
|
+
const member = members[i];
|
|
96
|
+
const methodName = getMemberName(member);
|
|
97
|
+
if (methodName) {
|
|
98
|
+
const orderIndex = getOrderIndex(methodName, member, order);
|
|
99
|
+
methodOrder.push({ name: methodName, index: orderIndex, node: member });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// Check if methods are in correct order
|
|
103
|
+
for (let i = 1; i < methodOrder.length; i++) {
|
|
104
|
+
if (methodOrder[i].index < methodOrder[i - 1].index) {
|
|
105
|
+
context.report({
|
|
106
|
+
node: getMemberKeyNode(methodOrder[i].node),
|
|
107
|
+
messageId: 'sortComp',
|
|
108
|
+
});
|
|
109
|
+
break; // Only report the first violation
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function getMemberName(member) {
|
|
114
|
+
if (member.type === 'MethodDefinition' && member.key.type === 'Identifier') {
|
|
115
|
+
return member.key.name;
|
|
116
|
+
}
|
|
117
|
+
// Handle PropertyDefinition (TypeScript parser)
|
|
118
|
+
if (member.type === 'PropertyDefinition' && member.key.type === 'Identifier') {
|
|
119
|
+
return member.key.name;
|
|
120
|
+
}
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
function getMemberKeyNode(member) {
|
|
124
|
+
if (member.type === 'MethodDefinition' && member.key.type === 'Identifier') {
|
|
125
|
+
return member.key;
|
|
126
|
+
}
|
|
127
|
+
// Handle PropertyDefinition (TypeScript parser)
|
|
128
|
+
if (member.type === 'PropertyDefinition' && member.key.type === 'Identifier') {
|
|
129
|
+
return member.key;
|
|
130
|
+
}
|
|
131
|
+
return member;
|
|
132
|
+
}
|
|
133
|
+
function getOrderIndex(name, member, order) {
|
|
134
|
+
// Static methods and properties
|
|
135
|
+
if (member.type === 'MethodDefinition' && member.static) {
|
|
136
|
+
if (name === 'render')
|
|
137
|
+
return order.indexOf('render');
|
|
138
|
+
return order.indexOf('static-methods');
|
|
139
|
+
}
|
|
140
|
+
// Handle PropertyDefinition (TypeScript parser)
|
|
141
|
+
if (member.type === 'PropertyDefinition' && member.static) {
|
|
142
|
+
return order.indexOf('static-variables');
|
|
143
|
+
}
|
|
144
|
+
// Instance variables - Handle PropertyDefinition (TypeScript parser)
|
|
145
|
+
if (member.type === 'PropertyDefinition' && !member.static) {
|
|
146
|
+
return order.indexOf('instance-variables');
|
|
147
|
+
}
|
|
148
|
+
// Lifecycle methods
|
|
149
|
+
if (LIFECYCLE_METHODS.includes(name)) {
|
|
150
|
+
return order.indexOf('lifecycle');
|
|
151
|
+
}
|
|
152
|
+
// Render method
|
|
153
|
+
if (name === 'render') {
|
|
154
|
+
return order.indexOf('render');
|
|
155
|
+
}
|
|
156
|
+
// Everything else
|
|
157
|
+
return order.indexOf('everything-else');
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
});
|
|
@@ -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: state-in-constructor
|
|
8
|
+
* Enforce state initialization in constructor
|
|
9
|
+
*/
|
|
10
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
11
|
+
export declare const stateInConstructor: TSESLint.RuleModule<"stateInConstructor", [], unknown, TSESLint.RuleListener> & {
|
|
12
|
+
name: string;
|
|
13
|
+
};
|
|
@@ -0,0 +1,103 @@
|
|
|
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.stateInConstructor = void 0;
|
|
9
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
+
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
11
|
+
exports.stateInConstructor = (0, eslint_devkit_1.createRule)({
|
|
12
|
+
name: 'state-in-constructor',
|
|
13
|
+
meta: {
|
|
14
|
+
type: 'problem',
|
|
15
|
+
docs: {
|
|
16
|
+
description: 'Enforce state initialization in constructor',
|
|
17
|
+
},
|
|
18
|
+
schema: [],
|
|
19
|
+
messages: {
|
|
20
|
+
stateInConstructor: (0, eslint_devkit_2.formatLLMMessage)({
|
|
21
|
+
icon: eslint_devkit_2.MessageIcons.WARNING,
|
|
22
|
+
issueName: 'State Not in Constructor',
|
|
23
|
+
description: 'State should be initialized in constructor',
|
|
24
|
+
severity: 'MEDIUM',
|
|
25
|
+
fix: 'Move state initialization to constructor method',
|
|
26
|
+
documentationLink: 'https://react.dev/reference/react/Component#constructor',
|
|
27
|
+
}),
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
defaultOptions: [],
|
|
31
|
+
create(context) {
|
|
32
|
+
return {
|
|
33
|
+
ClassDeclaration(node) {
|
|
34
|
+
if (isReactComponent(node)) {
|
|
35
|
+
checkStateInitialization(node);
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
function isReactComponent(node) {
|
|
40
|
+
if (!node.superClass)
|
|
41
|
+
return false;
|
|
42
|
+
if (node.superClass.type === 'Identifier') {
|
|
43
|
+
return node.superClass.name === 'Component' || node.superClass.name === 'PureComponent';
|
|
44
|
+
}
|
|
45
|
+
if (node.superClass.type === 'MemberExpression') {
|
|
46
|
+
return (node.superClass.object.type === 'Identifier' &&
|
|
47
|
+
node.superClass.object.name === 'React' &&
|
|
48
|
+
node.superClass.property.type === 'Identifier' &&
|
|
49
|
+
(node.superClass.property.name === 'Component' || node.superClass.property.name === 'PureComponent'));
|
|
50
|
+
}
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
function checkStateInitialization(node) {
|
|
54
|
+
let hasConstructorWithState = false;
|
|
55
|
+
let hasStateProperty = false;
|
|
56
|
+
for (const member of node.body.body) {
|
|
57
|
+
// Check for state property (PropertyDefinition is used by TypeScript parser)
|
|
58
|
+
if (member.type === 'PropertyDefinition' &&
|
|
59
|
+
member.key.type === 'Identifier' &&
|
|
60
|
+
member.key.name === 'state') {
|
|
61
|
+
hasStateProperty = true;
|
|
62
|
+
}
|
|
63
|
+
// Check for constructor with state initialization
|
|
64
|
+
if (member.type === 'MethodDefinition' &&
|
|
65
|
+
member.key.type === 'Identifier' &&
|
|
66
|
+
member.key.name === 'constructor') {
|
|
67
|
+
hasConstructorWithState = hasStateInitializationInConstructor(member);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
// Report if state is initialized as property instead of in constructor
|
|
71
|
+
if (hasStateProperty && !hasConstructorWithState) {
|
|
72
|
+
// Find the state property to report on
|
|
73
|
+
for (const member of node.body.body) {
|
|
74
|
+
if (member.type === 'PropertyDefinition' &&
|
|
75
|
+
member.key.type === 'Identifier' &&
|
|
76
|
+
member.key.name === 'state') {
|
|
77
|
+
context.report({
|
|
78
|
+
node: member.key,
|
|
79
|
+
messageId: 'stateInConstructor',
|
|
80
|
+
});
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function hasStateInitializationInConstructor(constructor) {
|
|
87
|
+
if (constructor.value.type === 'FunctionExpression') {
|
|
88
|
+
const body = constructor.value.body;
|
|
89
|
+
for (const statement of body.body) {
|
|
90
|
+
if (statement.type === 'ExpressionStatement' &&
|
|
91
|
+
statement.expression.type === 'AssignmentExpression' &&
|
|
92
|
+
statement.expression.left.type === 'MemberExpression' &&
|
|
93
|
+
statement.expression.left.object.type === 'ThisExpression' &&
|
|
94
|
+
statement.expression.left.property.type === 'Identifier' &&
|
|
95
|
+
statement.expression.left.property.name === 'state') {
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
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: static-property-placement
|
|
8
|
+
* Enforce static property placement
|
|
9
|
+
*/
|
|
10
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
11
|
+
export interface Options {
|
|
12
|
+
propertyGroups?: Array<{
|
|
13
|
+
name: string;
|
|
14
|
+
properties: string[];
|
|
15
|
+
}>;
|
|
16
|
+
childClass?: 'first' | 'last';
|
|
17
|
+
}
|
|
18
|
+
export declare const staticPropertyPlacement: TSESLint.RuleModule<"staticPropertyPlacement", [Options], unknown, TSESLint.RuleListener> & {
|
|
19
|
+
name: string;
|
|
20
|
+
};
|