@tcy/eslint-rules 1.9.1 → 1.10.0-beta.1
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/coverage/clover.xml +133 -62
- package/coverage/coverage-final.json +2 -1
- package/coverage/lcov-report/index.html +19 -19
- package/coverage/lcov-report/src/array-elements-per-line.js.html +1 -1
- package/coverage/lcov-report/src/back/back.js.html +5 -2
- package/coverage/lcov-report/src/back/http.js.html +1 -1
- package/coverage/lcov-report/src/back/index.html +1 -1
- package/coverage/lcov-report/src/back/recommended.js.html +1 -1
- package/coverage/lcov-report/src/bin/eslint-cache-guard.js.html +1 -1
- package/coverage/lcov-report/src/bin/index.html +1 -1
- package/coverage/lcov-report/src/client/client.js.html +5 -2
- package/coverage/lcov-report/src/client/index.html +1 -1
- package/coverage/lcov-report/src/client/recommended.js.html +1 -1
- package/coverage/lcov-report/src/consistent-const-scoping.js.html +703 -0
- package/coverage/lcov-report/src/decorator-type-enforcement.js.html +1 -1
- package/coverage/lcov-report/src/enforce-tailwind-important.js.html +116 -125
- package/coverage/lcov-report/src/export-specifiers-per-line.js.html +1 -1
- package/coverage/lcov-report/src/import-specifiers-per-line.js.html +1 -1
- package/coverage/lcov-report/src/index.html +35 -20
- package/coverage/lcov-report/src/index.js.html +1 -1
- package/coverage/lcov-report/src/jsx-boolean-shorthand.js.html +1 -1
- package/coverage/lcov-report/src/jsx-expression-string-literals.js.html +1 -1
- package/coverage/lcov-report/src/jsx-ref-string-exists.js.html +1 -1
- package/coverage/lcov-report/src/no-static-key-in-map.js.html +1 -1
- package/coverage/lcov-report/src/plugin.js.html +8 -2
- package/coverage/lcov-report/src/recommended.js.html +1 -1
- package/coverage/lcov-report/src/require-fragment-key.js.html +1 -1
- package/coverage/lcov-report/src/route-dto-required.js.html +1 -1
- package/coverage/lcov-report/src/v-model-name-match.js.html +1 -1
- package/coverage/lcov-report/src/vuejs-define-component.js.html +1 -1
- package/coverage/lcov-report/src/vuejs-define-workspace.js.html +1 -1
- package/coverage/lcov.info +342 -135
- package/decl/back/back.d.ts +17 -0
- package/decl/client/client.d.ts +17 -0
- package/decl/consistent-const-scoping.d.ts +63 -0
- package/decl/plugin.d.ts +16 -0
- package/package.json +6 -6
- package/src/back/back.js +1 -0
- package/src/client/client.js +1 -0
- package/src/consistent-const-scoping.js +206 -0
- package/src/enforce-tailwind-important.js +13 -16
- package/src/plugin.js +2 -0
- package/tests/consistent-const-scoping.spec.js +53 -0
- package/tests/enforce-tailwind-important.spec.js +100 -1
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview consistent-const-scoping
|
|
3
|
+
*
|
|
4
|
+
* Reports a `const` declared inside a function when its initializer does not
|
|
5
|
+
* depend on anything from that function (or any function nesting it) — no
|
|
6
|
+
* parameter, no local variable, no `this` / `super` / `arguments`:
|
|
7
|
+
*
|
|
8
|
+
* function makeGreeter() {
|
|
9
|
+
* const hello = () => console.log('hello'); // ✗ recreated every call
|
|
10
|
+
* return { hello };
|
|
11
|
+
* }
|
|
12
|
+
*
|
|
13
|
+
* // ✓
|
|
14
|
+
* const hello = () => console.log('hello');
|
|
15
|
+
* function makeGreeter() {
|
|
16
|
+
* return { hello };
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* A value with no runtime input to the function is identical on every call,
|
|
20
|
+
* so declaring it inside the function just rebuilds the same thing each
|
|
21
|
+
* time for no benefit and buries a module-level concern inside call-time
|
|
22
|
+
* code.
|
|
23
|
+
*
|
|
24
|
+
* Only a *pure* initializer is flagged — a literal, a template literal /
|
|
25
|
+
* array / object built from pure values, or a function / arrow expression.
|
|
26
|
+
* A `CallExpression`, `NewExpression`, `await`, `yield`, tagged template, or
|
|
27
|
+
* any other expression that can run a side effect or produce a different
|
|
28
|
+
* result depending on *when* it runs is deliberately left alone: unlike a
|
|
29
|
+
* function definition (which does not execute until called), moving one of
|
|
30
|
+
* those changes it from running once per call to running once at module
|
|
31
|
+
* load — that is a behavior change, not just a relocation, and this rule
|
|
32
|
+
* never suggests it.
|
|
33
|
+
*
|
|
34
|
+
* It stays silent when:
|
|
35
|
+
*
|
|
36
|
+
* - the `const` is already at module scope — nothing to hoist past
|
|
37
|
+
* - the id is a destructuring pattern (`const { a } = …`) — out of scope
|
|
38
|
+
* for this rule
|
|
39
|
+
* - the initializer reads a parameter, an outer local, `this`, `super`,
|
|
40
|
+
* or `arguments` — moving it would change what it reads
|
|
41
|
+
* - the initializer is not provably side-effect-free (see above)
|
|
42
|
+
*/
|
|
43
|
+
/** A value that evaluates the same way, with no side effects, wherever it runs. */
|
|
44
|
+
export declare const isPureExpression: (node: any) => any;
|
|
45
|
+
/** Does `node` contain a `this` / `super` that would resolve outside of it (through arrow boundaries only)? */
|
|
46
|
+
export declare const containsBlockingThis: (node: any) => any;
|
|
47
|
+
declare const _default: {
|
|
48
|
+
meta: {
|
|
49
|
+
type: string;
|
|
50
|
+
docs: {
|
|
51
|
+
description: string;
|
|
52
|
+
recommended: boolean;
|
|
53
|
+
};
|
|
54
|
+
schema: never[];
|
|
55
|
+
messages: {
|
|
56
|
+
hoistable: string;
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
create(context: any): {
|
|
60
|
+
VariableDeclarator(node: any): void;
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
export default _default;
|
package/decl/plugin.d.ts
CHANGED
|
@@ -243,5 +243,21 @@ declare const _default: {
|
|
|
243
243
|
JSXElement: (node: any) => void;
|
|
244
244
|
};
|
|
245
245
|
};
|
|
246
|
+
'consistent-const-scoping': {
|
|
247
|
+
meta: {
|
|
248
|
+
type: string;
|
|
249
|
+
docs: {
|
|
250
|
+
description: string;
|
|
251
|
+
recommended: boolean;
|
|
252
|
+
};
|
|
253
|
+
schema: never[];
|
|
254
|
+
messages: {
|
|
255
|
+
hoistable: string;
|
|
256
|
+
};
|
|
257
|
+
};
|
|
258
|
+
create(context: any): {
|
|
259
|
+
VariableDeclarator(node: any): void;
|
|
260
|
+
};
|
|
261
|
+
};
|
|
246
262
|
};
|
|
247
263
|
export default _default;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tcy/eslint-rules",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.0-beta.1",
|
|
4
4
|
"description": "```typescript import { plugin as customRules } from 'tcy@eslint-rules/src/index.mjs';",
|
|
5
5
|
"homepage": "https://github.com/tcastelly/eslint-rules#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -47,12 +47,12 @@
|
|
|
47
47
|
"test": "jest"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
|
-
"@babel/core": "^8.0.
|
|
51
|
-
"@babel/preset-env": "^8.0.
|
|
50
|
+
"@babel/core": "^8.0.6",
|
|
51
|
+
"@babel/preset-env": "^8.0.6",
|
|
52
52
|
"@types/jest": "^30.0.0",
|
|
53
|
-
"@types/node": "^26.
|
|
54
|
-
"babel-jest": "^30.5.
|
|
55
|
-
"jest": "^30.5.
|
|
53
|
+
"@types/node": "^26.6.2",
|
|
54
|
+
"babel-jest": "^30.5.2",
|
|
55
|
+
"jest": "^30.5.2",
|
|
56
56
|
"rimraf": "^6.1.3",
|
|
57
57
|
"typescript": "^7.0.2"
|
|
58
58
|
}
|
package/src/back/back.js
CHANGED
package/src/client/client.js
CHANGED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview consistent-const-scoping
|
|
3
|
+
*
|
|
4
|
+
* Reports a `const` declared inside a function when its initializer does not
|
|
5
|
+
* depend on anything from that function (or any function nesting it) — no
|
|
6
|
+
* parameter, no local variable, no `this` / `super` / `arguments`:
|
|
7
|
+
*
|
|
8
|
+
* function makeGreeter() {
|
|
9
|
+
* const hello = () => console.log('hello'); // ✗ recreated every call
|
|
10
|
+
* return { hello };
|
|
11
|
+
* }
|
|
12
|
+
*
|
|
13
|
+
* // ✓
|
|
14
|
+
* const hello = () => console.log('hello');
|
|
15
|
+
* function makeGreeter() {
|
|
16
|
+
* return { hello };
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* A value with no runtime input to the function is identical on every call,
|
|
20
|
+
* so declaring it inside the function just rebuilds the same thing each
|
|
21
|
+
* time for no benefit and buries a module-level concern inside call-time
|
|
22
|
+
* code.
|
|
23
|
+
*
|
|
24
|
+
* Only a *pure* initializer is flagged — a literal, a template literal /
|
|
25
|
+
* array / object built from pure values, or a function / arrow expression.
|
|
26
|
+
* A `CallExpression`, `NewExpression`, `await`, `yield`, tagged template, or
|
|
27
|
+
* any other expression that can run a side effect or produce a different
|
|
28
|
+
* result depending on *when* it runs is deliberately left alone: unlike a
|
|
29
|
+
* function definition (which does not execute until called), moving one of
|
|
30
|
+
* those changes it from running once per call to running once at module
|
|
31
|
+
* load — that is a behavior change, not just a relocation, and this rule
|
|
32
|
+
* never suggests it.
|
|
33
|
+
*
|
|
34
|
+
* It stays silent when:
|
|
35
|
+
*
|
|
36
|
+
* - the `const` is already at module scope — nothing to hoist past
|
|
37
|
+
* - the id is a destructuring pattern (`const { a } = …`) — out of scope
|
|
38
|
+
* for this rule
|
|
39
|
+
* - the initializer reads a parameter, an outer local, `this`, `super`,
|
|
40
|
+
* or `arguments` — moving it would change what it reads
|
|
41
|
+
* - the initializer is not provably side-effect-free (see above)
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
const LITERAL_TYPES = new Set([
|
|
45
|
+
'Literal', // espree: string / number / boolean / null / regex
|
|
46
|
+
'StringLiteral', // @babel/parser
|
|
47
|
+
'NumericLiteral',
|
|
48
|
+
'BooleanLiteral',
|
|
49
|
+
'BigIntLiteral',
|
|
50
|
+
'NullLiteral',
|
|
51
|
+
'RegExpLiteral',
|
|
52
|
+
]);
|
|
53
|
+
|
|
54
|
+
const UNWRAP_TYPES = new Set([
|
|
55
|
+
'ParenthesizedExpression',
|
|
56
|
+
'TSAsExpression',
|
|
57
|
+
'TSNonNullExpression',
|
|
58
|
+
'TSSatisfiesExpression',
|
|
59
|
+
]);
|
|
60
|
+
|
|
61
|
+
const FUNCTION_TYPES = new Set(['FunctionExpression', 'ArrowFunctionExpression']);
|
|
62
|
+
|
|
63
|
+
/** A value that evaluates the same way, with no side effects, wherever it runs. */
|
|
64
|
+
export const isPureExpression = (node) => {
|
|
65
|
+
if (!node) return false;
|
|
66
|
+
if (node.type === 'ThisExpression' || node.type === 'Super') return false;
|
|
67
|
+
if (LITERAL_TYPES.has(node.type)) return true;
|
|
68
|
+
if (UNWRAP_TYPES.has(node.type)) return isPureExpression(node.expression);
|
|
69
|
+
if (FUNCTION_TYPES.has(node.type)) return true;
|
|
70
|
+
if (node.type === 'Identifier') return true;
|
|
71
|
+
if (node.type === 'TemplateLiteral') return node.expressions.every(isPureExpression);
|
|
72
|
+
|
|
73
|
+
if (node.type === 'UnaryExpression') {
|
|
74
|
+
return node.operator !== 'delete' && isPureExpression(node.argument);
|
|
75
|
+
}
|
|
76
|
+
if (node.type === 'BinaryExpression' || node.type === 'LogicalExpression') {
|
|
77
|
+
return isPureExpression(node.left) && isPureExpression(node.right);
|
|
78
|
+
}
|
|
79
|
+
if (node.type === 'ConditionalExpression') {
|
|
80
|
+
return isPureExpression(node.test)
|
|
81
|
+
&& isPureExpression(node.consequent)
|
|
82
|
+
&& isPureExpression(node.alternate);
|
|
83
|
+
}
|
|
84
|
+
if (node.type === 'MemberExpression' || node.type === 'OptionalMemberExpression') {
|
|
85
|
+
return isPureExpression(node.object) && (!node.computed || isPureExpression(node.property));
|
|
86
|
+
}
|
|
87
|
+
if (node.type === 'ArrayExpression') {
|
|
88
|
+
return node.elements.every((el) => {
|
|
89
|
+
if (el === null) return true;
|
|
90
|
+
if (el.type === 'SpreadElement') return isPureExpression(el.argument);
|
|
91
|
+
return isPureExpression(el);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
if (node.type === 'ObjectExpression') {
|
|
95
|
+
return node.properties.every((prop) => {
|
|
96
|
+
if (prop.type === 'SpreadElement') return isPureExpression(prop.argument);
|
|
97
|
+
if (prop.computed && !isPureExpression(prop.key)) return false;
|
|
98
|
+
return isPureExpression(prop.value);
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return false;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/** Types that bind their own `this` — a `this` / `super` past this point belongs to them, not the const's init. */
|
|
106
|
+
const THIS_BOUNDARY_TYPES = new Set([
|
|
107
|
+
'FunctionDeclaration',
|
|
108
|
+
'FunctionExpression',
|
|
109
|
+
'ClassDeclaration',
|
|
110
|
+
'ClassExpression',
|
|
111
|
+
]);
|
|
112
|
+
|
|
113
|
+
/** Does `node` contain a `this` / `super` that would resolve outside of it (through arrow boundaries only)? */
|
|
114
|
+
export const containsBlockingThis = (node) => {
|
|
115
|
+
if (!node || typeof node.type !== 'string') return false;
|
|
116
|
+
if (node.type === 'ThisExpression' || node.type === 'Super') return true;
|
|
117
|
+
if (THIS_BOUNDARY_TYPES.has(node.type)) return false;
|
|
118
|
+
|
|
119
|
+
return Object.keys(node).some((key) => {
|
|
120
|
+
if (key === 'parent') return false;
|
|
121
|
+
const value = node[key];
|
|
122
|
+
if (Array.isArray(value)) {
|
|
123
|
+
return value.some((child) => child && typeof child.type === 'string' && containsBlockingThis(child));
|
|
124
|
+
}
|
|
125
|
+
return !!value && typeof value.type === 'string' && containsBlockingThis(value);
|
|
126
|
+
});
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const describeFunction = (fnNode) => {
|
|
130
|
+
if (fnNode.id?.name) return fnNode.id.name;
|
|
131
|
+
const { parent } = fnNode;
|
|
132
|
+
if (parent?.type === 'VariableDeclarator' && parent.id.type === 'Identifier') return parent.id.name;
|
|
133
|
+
if (parent?.type === 'Property' && parent.key.type === 'Identifier') return parent.key.name;
|
|
134
|
+
return '(anonymous function)';
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
export default {
|
|
138
|
+
meta: {
|
|
139
|
+
type: 'suggestion',
|
|
140
|
+
docs: {
|
|
141
|
+
description:
|
|
142
|
+
'Disallow a `const` inside a function when its (side-effect-free) value does not '
|
|
143
|
+
+ 'depend on that function\'s parameters, locals, `this`, or `arguments` — declare it '
|
|
144
|
+
+ 'outside so it is not rebuilt on every call.',
|
|
145
|
+
recommended: false,
|
|
146
|
+
},
|
|
147
|
+
schema: [],
|
|
148
|
+
messages: {
|
|
149
|
+
hoistable:
|
|
150
|
+
'`{{name}}` does not depend on anything from `{{fnName}}`\'s scope — declare it '
|
|
151
|
+
+ 'outside the function so it is not rebuilt on every call.',
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
|
|
155
|
+
create(context) {
|
|
156
|
+
const { sourceCode } = context;
|
|
157
|
+
|
|
158
|
+
/** Every scope from `node` up to (excluding) module/global scope, or `null` if none is a function. */
|
|
159
|
+
const blockingChain = (node) => {
|
|
160
|
+
const chain = [];
|
|
161
|
+
let scope = sourceCode.getScope(node);
|
|
162
|
+
let hasFunctionAncestor = false;
|
|
163
|
+
|
|
164
|
+
while (scope && scope.type !== 'global' && scope.type !== 'module') {
|
|
165
|
+
chain.push(scope);
|
|
166
|
+
if (scope.type === 'function') hasFunctionAncestor = true;
|
|
167
|
+
scope = scope.upper;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return hasFunctionAncestor ? chain : null;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const isReferencedIn = (chain, range) => chain.some((scope) => scope.variables.some(
|
|
174
|
+
(variable) => variable.references.some(
|
|
175
|
+
(ref) => ref.identifier.range[0] >= range[0] && ref.identifier.range[1] <= range[1],
|
|
176
|
+
),
|
|
177
|
+
));
|
|
178
|
+
|
|
179
|
+
const nearestFunctionScope = (chain) => chain.find((scope) => scope.type === 'function');
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
VariableDeclarator(node) {
|
|
183
|
+
if (node.parent.type !== 'VariableDeclaration' || node.parent.kind !== 'const') return;
|
|
184
|
+
if (node.id.type !== 'Identifier' || !node.init) return;
|
|
185
|
+
|
|
186
|
+
const chain = blockingChain(node);
|
|
187
|
+
if (!chain) return; // already at module scope
|
|
188
|
+
|
|
189
|
+
if (!isPureExpression(node.init)) return;
|
|
190
|
+
if (containsBlockingThis(node.init)) return;
|
|
191
|
+
if (isReferencedIn(chain, node.init.range)) return;
|
|
192
|
+
|
|
193
|
+
const fnScope = nearestFunctionScope(chain);
|
|
194
|
+
|
|
195
|
+
context.report({
|
|
196
|
+
node,
|
|
197
|
+
messageId: 'hoistable',
|
|
198
|
+
data: {
|
|
199
|
+
name: node.id.name,
|
|
200
|
+
fnName: describeFunction(fnScope.block),
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
},
|
|
204
|
+
};
|
|
205
|
+
},
|
|
206
|
+
};
|
|
@@ -116,24 +116,13 @@ export default {
|
|
|
116
116
|
|
|
117
117
|
switch (node.type) {
|
|
118
118
|
case 'Literal':
|
|
119
|
-
|
|
119
|
+
// skip non-string literals, e.g. `cond && 0`
|
|
120
|
+
if (typeof node.value === 'string') {
|
|
121
|
+
reportIfMissing(node, node.value);
|
|
122
|
+
}
|
|
120
123
|
break;
|
|
121
124
|
case 'ArrayExpression':
|
|
122
|
-
node.elements.forEach(
|
|
123
|
-
if (!el) {
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
if (el.type === 'Literal') {
|
|
128
|
-
reportIfMissing(el, el.value);
|
|
129
|
-
} else if (el.type === 'ObjectExpression') {
|
|
130
|
-
el.properties.forEach((p) => {
|
|
131
|
-
if (p.key?.type === 'Literal' && typeof p.key.value === 'string') {
|
|
132
|
-
reportIfMissing(p.key, p.key.value);
|
|
133
|
-
}
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
});
|
|
125
|
+
node.elements.forEach(processExpression);
|
|
137
126
|
break;
|
|
138
127
|
case 'ObjectExpression':
|
|
139
128
|
node.properties.forEach((p) => {
|
|
@@ -168,6 +157,14 @@ export default {
|
|
|
168
157
|
processExpression(node.consequent);
|
|
169
158
|
processExpression(node.alternate);
|
|
170
159
|
break;
|
|
160
|
+
case 'LogicalExpression':
|
|
161
|
+
// `cond && 'cls'`: only the right side is a class
|
|
162
|
+
// `cls || 'cls'` / `cls ?? 'cls'`: both sides can be classes
|
|
163
|
+
if (node.operator !== '&&') {
|
|
164
|
+
processExpression(node.left);
|
|
165
|
+
}
|
|
166
|
+
processExpression(node.right);
|
|
167
|
+
break;
|
|
171
168
|
default:
|
|
172
169
|
break;
|
|
173
170
|
}
|
package/src/plugin.js
CHANGED
|
@@ -12,6 +12,7 @@ import vuejsDefineWorkspace from './vuejs-define-workspace.js'
|
|
|
12
12
|
import routeDtoRequired from './route-dto-required.js'
|
|
13
13
|
import requireFragmentKey from './require-fragment-key.js'
|
|
14
14
|
import noStaticKeyInMap from './no-static-key-in-map.js'
|
|
15
|
+
import consistentConstScoping from './consistent-const-scoping.js'
|
|
15
16
|
|
|
16
17
|
export default {
|
|
17
18
|
'import-specifiers-per-line': importSpecifiersPerLineRule,
|
|
@@ -28,4 +29,5 @@ export default {
|
|
|
28
29
|
'route-dto-required': routeDtoRequired,
|
|
29
30
|
'require-fragment-key': requireFragmentKey,
|
|
30
31
|
'no-static-key-in-map': noStaticKeyInMap,
|
|
32
|
+
'consistent-const-scoping': consistentConstScoping,
|
|
31
33
|
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { parse } from '@babel/parser';
|
|
2
|
+
import { containsBlockingThis, isPureExpression } from '../src/consistent-const-scoping';
|
|
3
|
+
|
|
4
|
+
// ─── isPureExpression ───────────────────────────────────────────────────────
|
|
5
|
+
|
|
6
|
+
describe('GIVEN "consistent-const-scoping" / isPureExpression', () => {
|
|
7
|
+
const expr = (code) => parse(`(${code})`, { plugins: ['typescript'] }).program.body[0].expression;
|
|
8
|
+
|
|
9
|
+
it.each([
|
|
10
|
+
['"hello"', true],
|
|
11
|
+
['42', true],
|
|
12
|
+
['/foo/g', true],
|
|
13
|
+
['`hello`', true],
|
|
14
|
+
['() => console.log("hi")', true],
|
|
15
|
+
['function () { return 1; }', true],
|
|
16
|
+
['{ a: 1, b: [1, 2, "x"] }', true],
|
|
17
|
+
['[1, 2, 3]', true],
|
|
18
|
+
['a.b.c', true],
|
|
19
|
+
['1 + 2', true],
|
|
20
|
+
['a ? 1 : 2', true],
|
|
21
|
+
['console.log("hi")', false],
|
|
22
|
+
['new Date()', false],
|
|
23
|
+
['Math.random()', false],
|
|
24
|
+
['`hello ${getName()}`', false],
|
|
25
|
+
['this.value', false],
|
|
26
|
+
['[foo()]', false],
|
|
27
|
+
['{ a: foo() }', false],
|
|
28
|
+
])('THEN %s → pure:%s', (code, pure) => {
|
|
29
|
+
expect(isPureExpression(expr(code))).toBe(pure);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// ─── containsBlockingThis ───────────────────────────────────────────────────
|
|
34
|
+
|
|
35
|
+
describe('GIVEN "consistent-const-scoping" / containsBlockingThis', () => {
|
|
36
|
+
const expr = (code) => parse(`(${code})`, { plugins: ['typescript'] }).program.body[0].expression;
|
|
37
|
+
|
|
38
|
+
it('THEN a bare `this` is blocking', () => {
|
|
39
|
+
expect(containsBlockingThis(expr('this.value'))).toBe(true);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('THEN `this` inside an arrow (inherited) is blocking', () => {
|
|
43
|
+
expect(containsBlockingThis(expr('() => this.value'))).toBe(true);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('THEN `this` inside a nested function (own binding) is NOT blocking', () => {
|
|
47
|
+
expect(containsBlockingThis(expr('function () { return this.value; }'))).toBe(false);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('THEN no `this` at all is not blocking', () => {
|
|
51
|
+
expect(containsBlockingThis(expr('() => 42'))).toBe(false);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { parse } from '@babel/parser';
|
|
2
|
+
import rule, {
|
|
2
3
|
DEFAULT_IGNORED_CLASSES,
|
|
3
4
|
isMissingImportant,
|
|
4
5
|
resolveIgnored,
|
|
@@ -54,3 +55,101 @@ describe('GIVEN "enforce-tailwind-important"', () => {
|
|
|
54
55
|
});
|
|
55
56
|
});
|
|
56
57
|
});
|
|
58
|
+
|
|
59
|
+
// ─── rule (end-to-end) ──────────────────────────────────────────────────────
|
|
60
|
+
|
|
61
|
+
/** Run the rule on `code`, return reported classes and the auto-fixed source. */
|
|
62
|
+
const lint = (code, options = []) => {
|
|
63
|
+
const ast = parse(code, { sourceType: 'module', plugins: ['jsx', 'estree'] });
|
|
64
|
+
const reports = [];
|
|
65
|
+
const context = {
|
|
66
|
+
options,
|
|
67
|
+
getSourceCode: () => ({ getText: (node) => code.slice(node.start, node.end) }),
|
|
68
|
+
report: (r) => reports.push(r),
|
|
69
|
+
};
|
|
70
|
+
const visitors = rule.create(context);
|
|
71
|
+
|
|
72
|
+
const walk = (node) => {
|
|
73
|
+
if (!node || typeof node.type !== 'string') return;
|
|
74
|
+
visitors[node.type]?.(node);
|
|
75
|
+
Object.keys(node).forEach((key) => {
|
|
76
|
+
const value = node[key];
|
|
77
|
+
if (Array.isArray(value)) value.forEach(walk);
|
|
78
|
+
else if (value && typeof value.type === 'string') walk(value);
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
walk(ast.program);
|
|
82
|
+
|
|
83
|
+
// apply fixes from the end so offsets stay valid
|
|
84
|
+
let output = code;
|
|
85
|
+
reports
|
|
86
|
+
.map((r) => r.fix({ replaceText: (node, text) => ({ node, text }) }))
|
|
87
|
+
.sort((a, b) => b.node.start - a.node.start)
|
|
88
|
+
.forEach(({ node, text }) => {
|
|
89
|
+
output = output.slice(0, node.start) + text + output.slice(node.end);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
return { reports, output };
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
describe('GIVEN "enforce-tailwind-important" / logical expressions', () => {
|
|
96
|
+
it('THEN `cond && "cls"` is flagged and fixed', () => {
|
|
97
|
+
const { reports, output } = lint("const x = <div class={readonly && 'hidden'} />;");
|
|
98
|
+
|
|
99
|
+
expect(reports).toHaveLength(1);
|
|
100
|
+
expect(output).toBe("const x = <div class={readonly && 'hidden!'} />;");
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('THEN only the missing classes are fixed', () => {
|
|
104
|
+
const { output } = lint("const x = <div className={readonly && 'something! else'} />;");
|
|
105
|
+
|
|
106
|
+
expect(output).toBe("const x = <div className={readonly && 'something! else!'} />;");
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('THEN already-important classes are not flagged', () => {
|
|
110
|
+
const { reports } = lint("const x = <div class={readonly && 'something! else!'} />;");
|
|
111
|
+
|
|
112
|
+
expect(reports).toHaveLength(0);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('THEN both sides of `||` / `??` are checked', () => {
|
|
116
|
+
const { output } = lint("const x = <div class={(a || 'flex') ?? 'grid'} />;");
|
|
117
|
+
|
|
118
|
+
expect(output).toBe("const x = <div class={(a || 'flex!') ?? 'grid!'} />;");
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('THEN the left side of `&&` is not treated as a class', () => {
|
|
122
|
+
const { reports } = lint("const x = <div class={'foo' && 'bar!'} />;");
|
|
123
|
+
|
|
124
|
+
expect(reports).toHaveLength(0);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('THEN nested ternary / logical expressions are checked', () => {
|
|
128
|
+
const { output } = lint("const x = <div class={a ? b && 'p-2' : 'm-2'} />;");
|
|
129
|
+
|
|
130
|
+
expect(output).toBe("const x = <div class={a ? b && 'p-2!' : 'm-2!'} />;");
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('THEN logical expressions inside an array are checked', () => {
|
|
134
|
+
const { reports, output } = lint(
|
|
135
|
+
"const x = <div class={[readonly && 'hidden', 'something', 'else']} />;",
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
expect(reports).toHaveLength(3);
|
|
139
|
+
expect(output).toBe(
|
|
140
|
+
"const x = <div class={[readonly && 'hidden!', 'something!', 'else!']} />;",
|
|
141
|
+
);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it('THEN array elements that are already important are not flagged', () => {
|
|
145
|
+
const { reports } = lint(
|
|
146
|
+
"const x = <div class={[readonly && 'hidden!', 'something!', { 'p-2!': a }]} />;",
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
expect(reports).toHaveLength(0);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('THEN non-string literals do not crash', () => {
|
|
153
|
+
expect(() => lint('const x = <div class={count && 0} />;')).not.toThrow();
|
|
154
|
+
});
|
|
155
|
+
});
|