eslint-plugin-react-features 1.1.4 → 1.2.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 +72 -0
- package/README.md +80 -61
- package/package.json +6 -2
- package/src/index.d.ts +630 -16
- package/src/index.js +66 -18
- package/src/rules/component-api/no-arbitrary-token-class.d.ts +17 -0
- package/src/rules/component-api/no-arbitrary-token-class.js +85 -0
- package/src/rules/component-api/no-default-test-id.d.ts +15 -0
- package/src/rules/component-api/no-default-test-id.js +53 -0
- package/src/rules/component-api/no-inline-style.d.ts +15 -0
- package/src/rules/component-api/no-inline-style.js +63 -0
- package/src/rules/component-api/no-is-prefix-prop.d.ts +17 -0
- package/src/rules/component-api/no-is-prefix-prop.js +66 -0
- package/src/rules/component-api/no-kind-prop-discriminator.d.ts +25 -0
- package/src/rules/component-api/no-kind-prop-discriminator.js +62 -0
- package/src/rules/component-api/no-raw-color-literal.d.ts +16 -0
- package/src/rules/component-api/no-raw-color-literal.js +63 -0
- package/src/rules/component-api/no-wrapper-sub-component.d.ts +23 -0
- package/src/rules/component-api/no-wrapper-sub-component.js +103 -0
- package/src/rules/component-api/require-data-slot.d.ts +15 -0
- package/src/rules/component-api/require-data-slot.js +45 -0
- package/src/rules/performance/react-no-inline-functions.js +0 -1
|
@@ -0,0 +1,23 @@
|
|
|
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: no-wrapper-sub-component
|
|
8
|
+
* Enforces R12: don't create wrapper sub-components when an existing
|
|
9
|
+
* primitive does the job. Heuristic: a function component whose entire body
|
|
10
|
+
* is `return <Primitive {...props} />;` — a pure passthrough wrapper that
|
|
11
|
+
* adds no structural behavior.
|
|
12
|
+
*
|
|
13
|
+
* Exempt cases:
|
|
14
|
+
* - The wrapper adds at least one literal attribute (className, data-slot,
|
|
15
|
+
* aria-*, role) — that's structural behavior.
|
|
16
|
+
* - The wrapper adds hook calls or computed values before returning.
|
|
17
|
+
* - The wrapper renames the component for re-export with no other change —
|
|
18
|
+
* still flagged; consumers should import the primitive directly.
|
|
19
|
+
*/
|
|
20
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
21
|
+
export declare const noWrapperSubComponent: TSESLint.RuleModule<"wrapperSubComponent", [], unknown, TSESLint.RuleListener> & {
|
|
22
|
+
name: string;
|
|
23
|
+
};
|
|
@@ -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.noWrapperSubComponent = void 0;
|
|
9
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
+
const isPassthroughBody = (body) => {
|
|
11
|
+
// Arrow function with concise body: () => <X {...props} />
|
|
12
|
+
if (body.type === 'JSXElement')
|
|
13
|
+
return body;
|
|
14
|
+
// Block body with a single return statement
|
|
15
|
+
if (body.type !== 'BlockStatement')
|
|
16
|
+
return null;
|
|
17
|
+
if (body.body.length !== 1)
|
|
18
|
+
return null;
|
|
19
|
+
const stmt = body.body[0];
|
|
20
|
+
if (stmt.type !== 'ReturnStatement')
|
|
21
|
+
return null;
|
|
22
|
+
if (!stmt.argument || stmt.argument.type !== 'JSXElement')
|
|
23
|
+
return null;
|
|
24
|
+
return stmt.argument;
|
|
25
|
+
};
|
|
26
|
+
const isSpreadOnlyJsx = (el) => {
|
|
27
|
+
const attrs = el.openingElement.attributes;
|
|
28
|
+
if (attrs.length !== 1)
|
|
29
|
+
return false;
|
|
30
|
+
const attr = attrs[0];
|
|
31
|
+
if (attr.type !== 'JSXSpreadAttribute')
|
|
32
|
+
return false;
|
|
33
|
+
if (attr.argument.type !== 'Identifier')
|
|
34
|
+
return false;
|
|
35
|
+
// children must be empty (self-closing or empty)
|
|
36
|
+
return el.children.length === 0;
|
|
37
|
+
};
|
|
38
|
+
const isPascalCaseIdentifier = (s) => /^[A-Z]/.test(s);
|
|
39
|
+
const isPascalCaseJsxName = (name) => {
|
|
40
|
+
if (name.type === 'JSXIdentifier')
|
|
41
|
+
return isPascalCaseIdentifier(name.name);
|
|
42
|
+
if (name.type === 'JSXMemberExpression')
|
|
43
|
+
return true;
|
|
44
|
+
return false;
|
|
45
|
+
};
|
|
46
|
+
exports.noWrapperSubComponent = (0, eslint_devkit_1.createRule)({
|
|
47
|
+
name: 'no-wrapper-sub-component',
|
|
48
|
+
meta: {
|
|
49
|
+
type: 'problem',
|
|
50
|
+
docs: {
|
|
51
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/component-api/no-wrapper-sub-component.md',
|
|
52
|
+
description: 'Ban pure-passthrough wrapper sub-components — slot the primitive directly (R12)',
|
|
53
|
+
},
|
|
54
|
+
schema: [],
|
|
55
|
+
messages: {
|
|
56
|
+
wrapperSubComponent: (0, eslint_devkit_1.formatLLMMessage)({
|
|
57
|
+
icon: eslint_devkit_1.MessageIcons.WARNING,
|
|
58
|
+
issueName: 'Passthrough wrapper component',
|
|
59
|
+
description: 'Component `{{name}}` is a pure passthrough wrapper — slot the primitive directly (R12)',
|
|
60
|
+
severity: 'MEDIUM',
|
|
61
|
+
fix: 'Delete the wrapper and let consumers import the primitive. If the wrapper must exist, give it structural behavior (a slot, a default prop, or composition logic).',
|
|
62
|
+
documentationLink: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/component-api/no-wrapper-sub-component.md',
|
|
63
|
+
}),
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
defaultOptions: [],
|
|
67
|
+
create(context) {
|
|
68
|
+
const checkFn = (node, name) => {
|
|
69
|
+
if (!isPascalCaseIdentifier(name))
|
|
70
|
+
return;
|
|
71
|
+
const jsx = isPassthroughBody(node.body);
|
|
72
|
+
if (!jsx)
|
|
73
|
+
return;
|
|
74
|
+
if (!isSpreadOnlyJsx(jsx))
|
|
75
|
+
return;
|
|
76
|
+
if (!isPascalCaseJsxName(jsx.openingElement.name))
|
|
77
|
+
return;
|
|
78
|
+
context.report({
|
|
79
|
+
node,
|
|
80
|
+
messageId: 'wrapperSubComponent',
|
|
81
|
+
data: { name },
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
return {
|
|
85
|
+
FunctionDeclaration(node) {
|
|
86
|
+
if (!node.id)
|
|
87
|
+
return;
|
|
88
|
+
checkFn(node, node.id.name);
|
|
89
|
+
},
|
|
90
|
+
VariableDeclarator(node) {
|
|
91
|
+
if (node.id.type !== 'Identifier')
|
|
92
|
+
return;
|
|
93
|
+
if (!node.init)
|
|
94
|
+
return;
|
|
95
|
+
if (node.init.type !== 'ArrowFunctionExpression' &&
|
|
96
|
+
node.init.type !== 'FunctionExpression') {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
checkFn(node.init, node.id.name);
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
},
|
|
103
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
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-data-slot
|
|
8
|
+
* Enforces R6: every named composition part carries `data-slot="<part-name>"`
|
|
9
|
+
* so consumers can style by slot. Heuristic: a JSX element that carries
|
|
10
|
+
* `data-testid` is a named part and must also carry `data-slot`.
|
|
11
|
+
*/
|
|
12
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
13
|
+
export declare const requireDataSlot: TSESLint.RuleModule<"missingDataSlot", [], unknown, TSESLint.RuleListener> & {
|
|
14
|
+
name: string;
|
|
15
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
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.requireDataSlot = void 0;
|
|
9
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
+
const hasNamedAttribute = (node, name) => node.attributes.some((attr) => attr.type === 'JSXAttribute' &&
|
|
11
|
+
attr.name.type === 'JSXIdentifier' &&
|
|
12
|
+
attr.name.name === name);
|
|
13
|
+
exports.requireDataSlot = (0, eslint_devkit_1.createRule)({
|
|
14
|
+
name: 'require-data-slot',
|
|
15
|
+
meta: {
|
|
16
|
+
type: 'problem',
|
|
17
|
+
docs: {
|
|
18
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/component-api/require-data-slot.md',
|
|
19
|
+
description: 'Named composition parts must carry data-slot (R6)',
|
|
20
|
+
},
|
|
21
|
+
schema: [],
|
|
22
|
+
messages: {
|
|
23
|
+
missingDataSlot: (0, eslint_devkit_1.formatLLMMessage)({
|
|
24
|
+
icon: eslint_devkit_1.MessageIcons.WARNING,
|
|
25
|
+
issueName: 'Missing data-slot',
|
|
26
|
+
description: 'JSX element has data-testid but no data-slot (R6)',
|
|
27
|
+
severity: 'MEDIUM',
|
|
28
|
+
fix: 'Add `data-slot="<part-name>"` so consumers can style by slot.',
|
|
29
|
+
documentationLink: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/component-api/require-data-slot.md',
|
|
30
|
+
}),
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
defaultOptions: [],
|
|
34
|
+
create(context) {
|
|
35
|
+
return {
|
|
36
|
+
JSXOpeningElement(node) {
|
|
37
|
+
if (!hasNamedAttribute(node, 'data-testid'))
|
|
38
|
+
return;
|
|
39
|
+
if (hasNamedAttribute(node, 'data-slot'))
|
|
40
|
+
return;
|
|
41
|
+
context.report({ node, messageId: 'missingDataSlot' });
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
},
|
|
45
|
+
});
|
|
@@ -16,7 +16,6 @@ exports.reactNoInlineFunctions = (0, eslint_devkit_2.createRule)({
|
|
|
16
16
|
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-react-features/docs/rules/react-no-inline-functions.md',
|
|
17
17
|
description: 'Prevent inline functions in React renders with performance metrics',
|
|
18
18
|
},
|
|
19
|
-
fixable: 'code',
|
|
20
19
|
hasSuggestions: true,
|
|
21
20
|
messages: {
|
|
22
21
|
// 🎯 Token optimization: 42% reduction (48→28 tokens) - inline functions cause unnecessary re-renders
|