@tcy/eslint-rules 1.9.2 → 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 +78 -4
- package/coverage/coverage-final.json +1 -0
- 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 +1 -1
- 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 +24 -9
- 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 +219 -0
- 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/plugin.js +2 -0
- package/tests/consistent-const-scoping.spec.js +53 -0
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
|
+
};
|
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
|
+
});
|