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,134 @@
|
|
|
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.preferStatelessFunction = void 0;
|
|
9
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
+
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
11
|
+
exports.preferStatelessFunction = (0, eslint_devkit_1.createRule)({
|
|
12
|
+
name: 'prefer-stateless-function',
|
|
13
|
+
meta: {
|
|
14
|
+
type: 'problem',
|
|
15
|
+
docs: {
|
|
16
|
+
description: 'Prefer stateless functions',
|
|
17
|
+
},
|
|
18
|
+
messages: {
|
|
19
|
+
preferStatelessFunction: (0, eslint_devkit_2.formatLLMMessage)({
|
|
20
|
+
icon: eslint_devkit_2.MessageIcons.MIGRATION,
|
|
21
|
+
issueName: 'Class Component',
|
|
22
|
+
description: 'Class component without state or lifecycle methods',
|
|
23
|
+
severity: 'MEDIUM',
|
|
24
|
+
fix: 'Convert to functional component',
|
|
25
|
+
documentationLink: 'https://react.dev/learn/your-first-component',
|
|
26
|
+
}),
|
|
27
|
+
},
|
|
28
|
+
schema: [
|
|
29
|
+
{
|
|
30
|
+
type: 'object',
|
|
31
|
+
properties: {
|
|
32
|
+
ignorePureComponents: {
|
|
33
|
+
type: 'boolean',
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
additionalProperties: false,
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
},
|
|
40
|
+
defaultOptions: [{ ignorePureComponents: false }],
|
|
41
|
+
create(context) {
|
|
42
|
+
const [options] = context.options;
|
|
43
|
+
const ignorePureComponents = options?.ignorePureComponents ?? false;
|
|
44
|
+
return {
|
|
45
|
+
ClassDeclaration(node) {
|
|
46
|
+
if (!isReactComponent(node))
|
|
47
|
+
return;
|
|
48
|
+
// Skip if it's a PureComponent and we're ignoring them
|
|
49
|
+
if (ignorePureComponents && isPureComponent(node))
|
|
50
|
+
return;
|
|
51
|
+
// Check if class has state or lifecycle methods
|
|
52
|
+
if (!hasState(node) && !hasLifecycleMethods(node)) {
|
|
53
|
+
context.report({
|
|
54
|
+
node: node.id || node,
|
|
55
|
+
messageId: 'preferStatelessFunction',
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
function isReactComponent(node) {
|
|
61
|
+
if (!node.superClass)
|
|
62
|
+
return false;
|
|
63
|
+
if (node.superClass.type === 'Identifier') {
|
|
64
|
+
return node.superClass.name === 'Component' || node.superClass.name === 'PureComponent';
|
|
65
|
+
}
|
|
66
|
+
if (node.superClass.type === 'MemberExpression') {
|
|
67
|
+
return (node.superClass.object.type === 'Identifier' &&
|
|
68
|
+
node.superClass.object.name === 'React' &&
|
|
69
|
+
node.superClass.property.type === 'Identifier' &&
|
|
70
|
+
(node.superClass.property.name === 'Component' || node.superClass.property.name === 'PureComponent'));
|
|
71
|
+
}
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
function isPureComponent(node) {
|
|
75
|
+
if (!node.superClass)
|
|
76
|
+
return false;
|
|
77
|
+
if (node.superClass.type === 'Identifier') {
|
|
78
|
+
return node.superClass.name === 'PureComponent';
|
|
79
|
+
}
|
|
80
|
+
if (node.superClass.type === 'MemberExpression') {
|
|
81
|
+
return (node.superClass.object.type === 'Identifier' &&
|
|
82
|
+
node.superClass.object.name === 'React' &&
|
|
83
|
+
node.superClass.property.type === 'Identifier' &&
|
|
84
|
+
node.superClass.property.name === 'PureComponent');
|
|
85
|
+
}
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
function hasState(node) {
|
|
89
|
+
for (const member of node.body.body) {
|
|
90
|
+
// PropertyDefinition is used by TypeScript parser for class properties
|
|
91
|
+
if (member.type === 'PropertyDefinition' &&
|
|
92
|
+
member.key.type === 'Identifier' &&
|
|
93
|
+
member.key.name === 'state') {
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
if (member.type === 'MethodDefinition' &&
|
|
97
|
+
member.key.type === 'Identifier' &&
|
|
98
|
+
member.key.name === 'constructor') {
|
|
99
|
+
// Check if constructor sets this.state
|
|
100
|
+
const body = member.value.body;
|
|
101
|
+
if (!body)
|
|
102
|
+
continue;
|
|
103
|
+
for (const statement of body.body) {
|
|
104
|
+
if (statement.type === 'ExpressionStatement' &&
|
|
105
|
+
statement.expression.type === 'AssignmentExpression' &&
|
|
106
|
+
statement.expression.left.type === 'MemberExpression' &&
|
|
107
|
+
statement.expression.left.object.type === 'ThisExpression' &&
|
|
108
|
+
statement.expression.left.property.type === 'Identifier' &&
|
|
109
|
+
statement.expression.left.property.name === 'state') {
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
function hasLifecycleMethods(node) {
|
|
118
|
+
const lifecycleMethods = [
|
|
119
|
+
'componentDidMount', 'componentDidUpdate', 'componentWillUnmount',
|
|
120
|
+
'componentWillMount', 'componentWillReceiveProps', 'componentWillUpdate',
|
|
121
|
+
'shouldComponentUpdate', 'getDerivedStateFromProps', 'getSnapshotBeforeUpdate',
|
|
122
|
+
'UNSAFE_componentWillMount', 'UNSAFE_componentWillReceiveProps', 'UNSAFE_componentWillUpdate',
|
|
123
|
+
];
|
|
124
|
+
for (const member of node.body.body) {
|
|
125
|
+
if (member.type === 'MethodDefinition' &&
|
|
126
|
+
member.key.type === 'Identifier' &&
|
|
127
|
+
lifecycleMethods.includes(member.key.name)) {
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
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: prop-types
|
|
8
|
+
* Enforce prop types usage
|
|
9
|
+
*/
|
|
10
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
11
|
+
export interface Options {
|
|
12
|
+
ignore?: string[];
|
|
13
|
+
customValidators?: string[];
|
|
14
|
+
skipUndeclared?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export declare const propTypes: TSESLint.RuleModule<"propTypes", [Options], unknown, TSESLint.RuleListener> & {
|
|
17
|
+
name: string;
|
|
18
|
+
};
|
|
@@ -0,0 +1,153 @@
|
|
|
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.propTypes = void 0;
|
|
9
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
+
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
11
|
+
exports.propTypes = (0, eslint_devkit_1.createRule)({
|
|
12
|
+
name: 'prop-types',
|
|
13
|
+
meta: {
|
|
14
|
+
type: 'problem',
|
|
15
|
+
docs: {
|
|
16
|
+
description: 'Enforce prop types usage',
|
|
17
|
+
},
|
|
18
|
+
schema: [
|
|
19
|
+
{
|
|
20
|
+
type: 'object',
|
|
21
|
+
properties: {
|
|
22
|
+
ignore: {
|
|
23
|
+
type: 'array',
|
|
24
|
+
items: { type: 'string' },
|
|
25
|
+
},
|
|
26
|
+
customValidators: {
|
|
27
|
+
type: 'array',
|
|
28
|
+
items: { type: 'string' },
|
|
29
|
+
},
|
|
30
|
+
skipUndeclared: {
|
|
31
|
+
type: 'boolean',
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
additionalProperties: false,
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
messages: {
|
|
38
|
+
propTypes: (0, eslint_devkit_2.formatLLMMessage)({
|
|
39
|
+
icon: eslint_devkit_2.MessageIcons.WARNING,
|
|
40
|
+
issueName: 'Missing PropTypes',
|
|
41
|
+
description: 'Component missing prop type validation',
|
|
42
|
+
severity: 'MEDIUM',
|
|
43
|
+
fix: 'Add propTypes static property or use TypeScript',
|
|
44
|
+
documentationLink: 'https://react.dev/learn/components-and-props#specifying-a-default-prop',
|
|
45
|
+
}),
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
defaultOptions: [{}],
|
|
49
|
+
create(context) {
|
|
50
|
+
const [options] = context.options;
|
|
51
|
+
const ignore = options?.ignore ?? [];
|
|
52
|
+
const skipUndeclared = options?.skipUndeclared ?? false;
|
|
53
|
+
const components = [];
|
|
54
|
+
const propTypesMap = new Map();
|
|
55
|
+
return {
|
|
56
|
+
// Collect class components
|
|
57
|
+
ClassDeclaration(node) {
|
|
58
|
+
if (isReactComponent(node) && !ignore.includes(node.id?.name || '')) {
|
|
59
|
+
components.push(node);
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
// Collect propTypes (PropertyDefinition in TypeScript ESLint)
|
|
63
|
+
'PropertyDefinition[key.name="propTypes"]'(node) {
|
|
64
|
+
if (node.parent.type === 'ClassBody') {
|
|
65
|
+
const classNode = node.parent.parent;
|
|
66
|
+
if (classNode.id?.name) {
|
|
67
|
+
propTypesMap.set(classNode.id.name, node);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
// Check components at the end
|
|
72
|
+
'Program:exit'() {
|
|
73
|
+
for (const component of components) {
|
|
74
|
+
const componentName = component.id?.name;
|
|
75
|
+
if (!componentName)
|
|
76
|
+
continue;
|
|
77
|
+
const propTypesProp = propTypesMap.get(componentName);
|
|
78
|
+
// Skip if component has propTypes or if skipUndeclared is true and no props are used
|
|
79
|
+
if (propTypesProp || (skipUndeclared && !hasPropsUsage(component))) {
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
context.report({
|
|
83
|
+
node: component.id || component,
|
|
84
|
+
messageId: 'propTypes',
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
function isReactComponent(node) {
|
|
90
|
+
if (!node.superClass)
|
|
91
|
+
return false;
|
|
92
|
+
if (node.superClass.type === 'Identifier') {
|
|
93
|
+
return node.superClass.name === 'Component' || node.superClass.name === 'PureComponent';
|
|
94
|
+
}
|
|
95
|
+
if (node.superClass.type === 'MemberExpression') {
|
|
96
|
+
return (node.superClass.object.type === 'Identifier' &&
|
|
97
|
+
node.superClass.object.name === 'React' &&
|
|
98
|
+
node.superClass.property.type === 'Identifier' &&
|
|
99
|
+
(node.superClass.property.name === 'Component' || node.superClass.property.name === 'PureComponent'));
|
|
100
|
+
}
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
function hasPropsUsage(node) {
|
|
104
|
+
// Simple check: look for this.props usage
|
|
105
|
+
for (const member of node.body.body) {
|
|
106
|
+
if (member.type === 'MethodDefinition') {
|
|
107
|
+
const body = member.value.body;
|
|
108
|
+
if (containsPropsAccess(body)) {
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
function containsPropsAccess(node, visited = new Set()) {
|
|
116
|
+
if (!node)
|
|
117
|
+
return false;
|
|
118
|
+
if (visited.has(node))
|
|
119
|
+
return false;
|
|
120
|
+
visited.add(node);
|
|
121
|
+
if (node.type === 'MemberExpression' &&
|
|
122
|
+
node.object.type === 'ThisExpression' &&
|
|
123
|
+
node.property.type === 'Identifier' &&
|
|
124
|
+
node.property.name === 'props') {
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
// Skip non-child keys to avoid circular references
|
|
128
|
+
const skipKeys = new Set(['parent', 'range', 'loc', 'start', 'end', 'tokens', 'comments']);
|
|
129
|
+
for (const key in node) {
|
|
130
|
+
if (skipKeys.has(key))
|
|
131
|
+
continue;
|
|
132
|
+
const child = node[key];
|
|
133
|
+
if (child && typeof child === 'object') {
|
|
134
|
+
if (Array.isArray(child)) {
|
|
135
|
+
for (const item of child) {
|
|
136
|
+
if (item && typeof item === 'object' && 'type' in item) {
|
|
137
|
+
if (containsPropsAccess(item, visited)) {
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
else if ('type' in child) {
|
|
144
|
+
if (containsPropsAccess(child, visited)) {
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
});
|
|
@@ -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: react-in-jsx-scope
|
|
8
|
+
* Ensure React is in scope
|
|
9
|
+
*/
|
|
10
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
11
|
+
export declare const reactInJsxScope: TSESLint.RuleModule<"reactInJsxScope", [], unknown, TSESLint.RuleListener> & {
|
|
12
|
+
name: string;
|
|
13
|
+
};
|
|
@@ -0,0 +1,94 @@
|
|
|
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.reactInJsxScope = void 0;
|
|
9
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
+
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
11
|
+
exports.reactInJsxScope = (0, eslint_devkit_1.createRule)({
|
|
12
|
+
name: 'react-in-jsx-scope',
|
|
13
|
+
meta: {
|
|
14
|
+
type: 'problem',
|
|
15
|
+
docs: {
|
|
16
|
+
description: 'Ensure React is in scope',
|
|
17
|
+
},
|
|
18
|
+
schema: [],
|
|
19
|
+
messages: {
|
|
20
|
+
reactInJsxScope: (0, eslint_devkit_2.formatLLMMessage)({
|
|
21
|
+
icon: eslint_devkit_2.MessageIcons.WARNING,
|
|
22
|
+
issueName: 'React Not in Scope',
|
|
23
|
+
description: 'React must be in scope when using JSX',
|
|
24
|
+
severity: 'CRITICAL',
|
|
25
|
+
fix: 'Add import React from "react" at the top of the file',
|
|
26
|
+
documentationLink: 'https://react.dev/learn/add-react-to-a-website#optional-using-jsx-without-a-build-step',
|
|
27
|
+
}),
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
defaultOptions: [],
|
|
31
|
+
create(context) {
|
|
32
|
+
let hasReactImport = false;
|
|
33
|
+
let hasJsxUsage = false;
|
|
34
|
+
return {
|
|
35
|
+
ImportDeclaration(node) {
|
|
36
|
+
// Check for React import
|
|
37
|
+
if (node.source.value === 'react') {
|
|
38
|
+
for (const specifier of node.specifiers) {
|
|
39
|
+
if (specifier.type === 'ImportDefaultSpecifier' &&
|
|
40
|
+
specifier.local.name === 'React') {
|
|
41
|
+
hasReactImport = true;
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
if (specifier.type === 'ImportNamespaceSpecifier' &&
|
|
45
|
+
specifier.local.name === 'React') {
|
|
46
|
+
hasReactImport = true;
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
// Check for JSX usage
|
|
53
|
+
JSXElement() {
|
|
54
|
+
hasJsxUsage = true;
|
|
55
|
+
},
|
|
56
|
+
JSXFragment() {
|
|
57
|
+
hasJsxUsage = true;
|
|
58
|
+
},
|
|
59
|
+
// Check at the end of the program
|
|
60
|
+
'Program:exit'(node) {
|
|
61
|
+
if (hasJsxUsage && !hasReactImport) {
|
|
62
|
+
// Find the first JSX element to report on
|
|
63
|
+
for (const statement of node.body) {
|
|
64
|
+
if (statement.type === 'ExpressionStatement' && statement.expression.type === 'JSXElement') {
|
|
65
|
+
context.report({
|
|
66
|
+
node: statement.expression.openingElement.name,
|
|
67
|
+
messageId: 'reactInJsxScope',
|
|
68
|
+
});
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
if (statement.type === 'VariableDeclaration') {
|
|
72
|
+
for (const declarator of statement.declarations) {
|
|
73
|
+
if (declarator.init && declarator.init.type === 'JSXElement') {
|
|
74
|
+
context.report({
|
|
75
|
+
node: declarator.init.openingElement.name,
|
|
76
|
+
messageId: 'reactInJsxScope',
|
|
77
|
+
});
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (statement.type === 'ReturnStatement' && statement.argument && statement.argument.type === 'JSXElement') {
|
|
83
|
+
context.report({
|
|
84
|
+
node: statement.argument.openingElement.name,
|
|
85
|
+
messageId: 'reactInJsxScope',
|
|
86
|
+
});
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
},
|
|
94
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Ofri Peretz
|
|
3
|
+
* Licensed under the MIT License. Use of this source code is governed by the
|
|
4
|
+
* MIT license that can be found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* ESLint Rule: require-default-props
|
|
8
|
+
* Require default props
|
|
9
|
+
*/
|
|
10
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
11
|
+
export interface Options {
|
|
12
|
+
forbidDefaultForRequired?: boolean;
|
|
13
|
+
}
|
|
14
|
+
export declare const requireDefaultProps: TSESLint.RuleModule<"requireDefaultProps", [Options], unknown, TSESLint.RuleListener> & {
|
|
15
|
+
name: string;
|
|
16
|
+
};
|
|
@@ -0,0 +1,146 @@
|
|
|
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.requireDefaultProps = void 0;
|
|
9
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
+
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
11
|
+
exports.requireDefaultProps = (0, eslint_devkit_1.createRule)({
|
|
12
|
+
name: 'require-default-props',
|
|
13
|
+
meta: {
|
|
14
|
+
type: 'problem',
|
|
15
|
+
docs: {
|
|
16
|
+
description: 'Require default props',
|
|
17
|
+
},
|
|
18
|
+
messages: {
|
|
19
|
+
requireDefaultProps: (0, eslint_devkit_2.formatLLMMessage)({
|
|
20
|
+
icon: eslint_devkit_2.MessageIcons.WARNING,
|
|
21
|
+
issueName: 'Missing Default Props',
|
|
22
|
+
description: 'Prop is not required but has no default value',
|
|
23
|
+
severity: 'MEDIUM',
|
|
24
|
+
fix: 'Add defaultProps or make prop required in propTypes',
|
|
25
|
+
documentationLink: 'https://react.dev/learn/components-and-props#specifying-a-default-prop',
|
|
26
|
+
}),
|
|
27
|
+
},
|
|
28
|
+
schema: [
|
|
29
|
+
{
|
|
30
|
+
type: 'object',
|
|
31
|
+
properties: {
|
|
32
|
+
forbidDefaultForRequired: {
|
|
33
|
+
type: 'boolean',
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
additionalProperties: false,
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
},
|
|
40
|
+
defaultOptions: [{}],
|
|
41
|
+
create(context) {
|
|
42
|
+
const [options] = context.options;
|
|
43
|
+
const forbidDefaultForRequired = options?.forbidDefaultForRequired ?? false;
|
|
44
|
+
const propTypes = new Map();
|
|
45
|
+
const defaultProps = new Map();
|
|
46
|
+
const reactComponents = new Set();
|
|
47
|
+
return {
|
|
48
|
+
// Track React component classes
|
|
49
|
+
ClassDeclaration(node) {
|
|
50
|
+
if (isReactComponent(node) && node.id?.name) {
|
|
51
|
+
reactComponents.add(node.id.name);
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
// Collect propTypes (PropertyDefinition is used by TypeScript parser)
|
|
55
|
+
'PropertyDefinition[key.name="propTypes"]'(node) {
|
|
56
|
+
if (node.value && node.value.type === 'ObjectExpression') {
|
|
57
|
+
const componentName = getComponentName(node);
|
|
58
|
+
if (componentName) {
|
|
59
|
+
const propsMap = new Map();
|
|
60
|
+
for (const prop of node.value.properties) {
|
|
61
|
+
if (prop.type === 'Property' && prop.key.type === 'Identifier') {
|
|
62
|
+
propsMap.set(prop.key.name, prop);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
propTypes.set(componentName, propsMap);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
// Collect defaultProps (PropertyDefinition is used by TypeScript parser)
|
|
70
|
+
'PropertyDefinition[key.name="defaultProps"]'(node) {
|
|
71
|
+
if (node.value && node.value.type === 'ObjectExpression') {
|
|
72
|
+
const componentName = getComponentName(node);
|
|
73
|
+
if (componentName) {
|
|
74
|
+
const propsMap = new Map();
|
|
75
|
+
for (const prop of node.value.properties) {
|
|
76
|
+
if (prop.type === 'Property' && prop.key.type === 'Identifier') {
|
|
77
|
+
propsMap.set(prop.key.name, prop);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
defaultProps.set(componentName, propsMap);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
// Check at the end
|
|
85
|
+
'Program:exit'() {
|
|
86
|
+
for (const [componentName, propTypesMap] of propTypes) {
|
|
87
|
+
// Only check React components
|
|
88
|
+
if (!reactComponents.has(componentName)) {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const defaultsMap = defaultProps.get(componentName);
|
|
92
|
+
for (const [propName, propType] of propTypesMap) {
|
|
93
|
+
const hasDefault = defaultsMap?.has(propName);
|
|
94
|
+
const isRequired = isRequiredProp(propType);
|
|
95
|
+
if (!isRequired && !hasDefault) {
|
|
96
|
+
// Find the component node to report on
|
|
97
|
+
// This is a simplified version - in practice, we'd need to track component nodes
|
|
98
|
+
context.report({
|
|
99
|
+
node: propType.key,
|
|
100
|
+
messageId: 'requireDefaultProps',
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
if (forbidDefaultForRequired && isRequired && hasDefault && defaultsMap) {
|
|
104
|
+
const defaultProp = defaultsMap.get(propName);
|
|
105
|
+
if (defaultProp) {
|
|
106
|
+
context.report({
|
|
107
|
+
node: defaultProp.key,
|
|
108
|
+
messageId: 'requireDefaultProps',
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
function getComponentName(node) {
|
|
117
|
+
if (node.parent.type === 'ClassBody' && node.parent.parent.type === 'ClassDeclaration') {
|
|
118
|
+
return node.parent.parent.id?.name || null;
|
|
119
|
+
}
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
function isReactComponent(node) {
|
|
123
|
+
if (!node.superClass)
|
|
124
|
+
return false;
|
|
125
|
+
if (node.superClass.type === 'Identifier') {
|
|
126
|
+
return node.superClass.name === 'Component' || node.superClass.name === 'PureComponent';
|
|
127
|
+
}
|
|
128
|
+
if (node.superClass.type === 'MemberExpression') {
|
|
129
|
+
return (node.superClass.object.type === 'Identifier' &&
|
|
130
|
+
node.superClass.object.name === 'React' &&
|
|
131
|
+
node.superClass.property.type === 'Identifier' &&
|
|
132
|
+
(node.superClass.property.name === 'Component' || node.superClass.property.name === 'PureComponent'));
|
|
133
|
+
}
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
function isRequiredProp(propType) {
|
|
137
|
+
// Check if prop type ends with .isRequired
|
|
138
|
+
if (propType.value.type === 'MemberExpression' &&
|
|
139
|
+
propType.value.property.type === 'Identifier' &&
|
|
140
|
+
propType.value.property.name === 'isRequired') {
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Ofri Peretz
|
|
3
|
+
* Licensed under the MIT License. Use of this source code is governed by the
|
|
4
|
+
* MIT license that can be found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* ESLint Rule: require-optimization
|
|
8
|
+
* Require performance optimizations for React components (requires deep performance analysis)
|
|
9
|
+
*/
|
|
10
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
11
|
+
type MessageIds = 'considerMemo' | 'considerUseMemo' | 'considerUseCallback' | 'considerLazy' | 'considerPureComponent' | 'performanceOptimization';
|
|
12
|
+
export interface Options {
|
|
13
|
+
/** Suggest React.memo for components with expensive renders */
|
|
14
|
+
suggestMemo?: boolean;
|
|
15
|
+
/** Suggest useMemo for expensive computations */
|
|
16
|
+
suggestUseMemo?: boolean;
|
|
17
|
+
/** Suggest useCallback for event handlers */
|
|
18
|
+
suggestUseCallback?: boolean;
|
|
19
|
+
/** Minimum number of props to trigger memo suggestion */
|
|
20
|
+
minPropsForMemo?: number;
|
|
21
|
+
}
|
|
22
|
+
type RuleOptions = [Options?];
|
|
23
|
+
export declare const requireOptimization: TSESLint.RuleModule<MessageIds, RuleOptions, unknown, TSESLint.RuleListener> & {
|
|
24
|
+
name: string;
|
|
25
|
+
};
|
|
26
|
+
export {};
|