@tcy/eslint-rules 1.10.0-beta.1 → 1.10.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.
Files changed (40) hide show
  1. package/README.md +540 -6
  2. package/coverage/clover.xml +90 -75
  3. package/coverage/coverage-final.json +1 -1
  4. package/coverage/lcov-report/index.html +19 -19
  5. package/coverage/lcov-report/src/array-elements-per-line.js.html +1 -1
  6. package/coverage/lcov-report/src/back/back.js.html +1 -1
  7. package/coverage/lcov-report/src/back/http.js.html +1 -1
  8. package/coverage/lcov-report/src/back/index.html +1 -1
  9. package/coverage/lcov-report/src/back/recommended.js.html +1 -1
  10. package/coverage/lcov-report/src/bin/eslint-cache-guard.js.html +1 -1
  11. package/coverage/lcov-report/src/bin/index.html +1 -1
  12. package/coverage/lcov-report/src/client/client.js.html +1 -1
  13. package/coverage/lcov-report/src/client/index.html +1 -1
  14. package/coverage/lcov-report/src/client/recommended.js.html +1 -1
  15. package/coverage/lcov-report/src/consistent-const-scoping.js.html +237 -12
  16. package/coverage/lcov-report/src/decorator-type-enforcement.js.html +1 -1
  17. package/coverage/lcov-report/src/enforce-tailwind-important.js.html +1 -1
  18. package/coverage/lcov-report/src/export-specifiers-per-line.js.html +1 -1
  19. package/coverage/lcov-report/src/import-specifiers-per-line.js.html +1 -1
  20. package/coverage/lcov-report/src/index.html +20 -20
  21. package/coverage/lcov-report/src/index.js.html +1 -1
  22. package/coverage/lcov-report/src/jsx-boolean-shorthand.js.html +1 -1
  23. package/coverage/lcov-report/src/jsx-expression-string-literals.js.html +1 -1
  24. package/coverage/lcov-report/src/jsx-ref-string-exists.js.html +1 -1
  25. package/coverage/lcov-report/src/no-static-key-in-map.js.html +1 -1
  26. package/coverage/lcov-report/src/plugin.js.html +1 -1
  27. package/coverage/lcov-report/src/recommended.js.html +1 -1
  28. package/coverage/lcov-report/src/require-fragment-key.js.html +1 -1
  29. package/coverage/lcov-report/src/route-dto-required.js.html +1 -1
  30. package/coverage/lcov-report/src/v-model-name-match.js.html +1 -1
  31. package/coverage/lcov-report/src/vuejs-define-component.js.html +1 -1
  32. package/coverage/lcov-report/src/vuejs-define-workspace.js.html +1 -1
  33. package/coverage/lcov.info +248 -203
  34. package/decl/back/back.d.ts +14 -1
  35. package/decl/client/client.d.ts +14 -1
  36. package/decl/consistent-const-scoping.d.ts +37 -1
  37. package/decl/plugin.d.ts +14 -1
  38. package/package.json +1 -1
  39. package/src/consistent-const-scoping.js +76 -1
  40. package/tests/consistent-const-scoping.spec.js +50 -1
@@ -39,6 +39,17 @@
39
39
  * - the initializer reads a parameter, an outer local, `this`, `super`,
40
40
  * or `arguments` — moving it would change what it reads
41
41
  * - the initializer is not provably side-effect-free (see above)
42
+ *
43
+ * Which *shape* of initializer gets flagged is configurable via the `types`
44
+ * option, so different parts of a codebase can opt into different strictness
45
+ * levels (e.g. only arrow functions in most folders, arrow functions +
46
+ * primitives in a folder that wants to be stricter):
47
+ *
48
+ * 'consistent-const-scoping': ['error', { types: ['arrow-function', 'primitive'] }]
49
+ *
50
+ * Recognized values: 'primitive', 'template-literal', 'expression' (identifier /
51
+ * unary / binary / logical / conditional / member access), 'array', 'object',
52
+ * 'arrow-function', 'function-expression'. Defaults to `['arrow-function']`.
42
53
  */
43
54
 
44
55
  const LITERAL_TYPES = new Set([
@@ -60,6 +71,55 @@ const UNWRAP_TYPES = new Set([
60
71
 
61
72
  const FUNCTION_TYPES = new Set(['FunctionExpression', 'ArrowFunctionExpression']);
62
73
 
74
+ /** All initializer categories the `types` option can select. */
75
+ export const ALL_INIT_TYPES = [
76
+ 'primitive',
77
+ 'template-literal',
78
+ 'expression',
79
+ 'array',
80
+ 'object',
81
+ 'arrow-function',
82
+ 'function-expression',
83
+ ];
84
+
85
+ export const DEFAULT_INIT_TYPES = ['arrow-function'];
86
+
87
+ const CATEGORY_BY_TYPE = {
88
+ ArrowFunctionExpression: 'arrow-function',
89
+ FunctionExpression: 'function-expression',
90
+ ArrayExpression: 'array',
91
+ ObjectExpression: 'object',
92
+ TemplateLiteral: 'template-literal',
93
+ };
94
+
95
+ const unwrap = (node) => (node && UNWRAP_TYPES.has(node.type) ? unwrap(node.expression) : node);
96
+
97
+ /**
98
+ * Which `types` category a (already known-pure) initializer belongs to, after
99
+ * unwrapping the same `as` / `!` / `satisfies` / parens wrappers the purity
100
+ * check sees through. `null` for a node that isn't a recognized initializer
101
+ * shape at all.
102
+ */
103
+ export const classifyInit = (node) => {
104
+ const inner = unwrap(node);
105
+ if (!inner) return null;
106
+ if (LITERAL_TYPES.has(inner.type)) return 'primitive';
107
+ if (CATEGORY_BY_TYPE[inner.type]) return CATEGORY_BY_TYPE[inner.type];
108
+ if (
109
+ inner.type === 'Identifier'
110
+ || inner.type === 'UnaryExpression'
111
+ || inner.type === 'BinaryExpression'
112
+ || inner.type === 'LogicalExpression'
113
+ || inner.type === 'ConditionalExpression'
114
+ || inner.type === 'MemberExpression'
115
+ || inner.type === 'OptionalMemberExpression'
116
+ ) return 'expression';
117
+ return null;
118
+ };
119
+
120
+ /** Build the set of enabled initializer categories from the rule options. */
121
+ export const resolveInitTypes = (options) => new Set((options && options.types) || DEFAULT_INIT_TYPES);
122
+
63
123
  /** A value that evaluates the same way, with no side effects, wherever it runs. */
64
124
  export const isPureExpression = (node) => {
65
125
  if (!node) return false;
@@ -144,7 +204,20 @@ export default {
144
204
  + 'outside so it is not rebuilt on every call.',
145
205
  recommended: false,
146
206
  },
147
- schema: [],
207
+ schema: [
208
+ {
209
+ type: 'object',
210
+ properties: {
211
+ types: {
212
+ type: 'array',
213
+ items: { enum: ALL_INIT_TYPES },
214
+ uniqueItems: true,
215
+ default: DEFAULT_INIT_TYPES,
216
+ },
217
+ },
218
+ additionalProperties: false,
219
+ },
220
+ ],
148
221
  messages: {
149
222
  hoistable:
150
223
  '`{{name}}` does not depend on anything from `{{fnName}}`\'s scope — declare it '
@@ -154,6 +227,7 @@ export default {
154
227
 
155
228
  create(context) {
156
229
  const { sourceCode } = context;
230
+ const enabledTypes = resolveInitTypes(context.options[0]);
157
231
 
158
232
  /** Every scope from `node` up to (excluding) module/global scope, or `null` if none is a function. */
159
233
  const blockingChain = (node) => {
@@ -189,6 +263,7 @@ export default {
189
263
  if (!isPureExpression(node.init)) return;
190
264
  if (containsBlockingThis(node.init)) return;
191
265
  if (isReferencedIn(chain, node.init.range)) return;
266
+ if (!enabledTypes.has(classifyInit(node.init))) return;
192
267
 
193
268
  const fnScope = nearestFunctionScope(chain);
194
269
 
@@ -1,5 +1,11 @@
1
1
  import { parse } from '@babel/parser';
2
- import { containsBlockingThis, isPureExpression } from '../src/consistent-const-scoping';
2
+ import {
3
+ classifyInit,
4
+ containsBlockingThis,
5
+ DEFAULT_INIT_TYPES,
6
+ isPureExpression,
7
+ resolveInitTypes,
8
+ } from '../src/consistent-const-scoping';
3
9
 
4
10
  // ─── isPureExpression ───────────────────────────────────────────────────────
5
11
 
@@ -51,3 +57,46 @@ describe('GIVEN "consistent-const-scoping" / containsBlockingThis', () => {
51
57
  expect(containsBlockingThis(expr('() => 42'))).toBe(false);
52
58
  });
53
59
  });
60
+
61
+ // ─── classifyInit ───────────────────────────────────────────────────────────
62
+
63
+ describe('GIVEN "consistent-const-scoping" / classifyInit', () => {
64
+ const expr = (code) => parse(`(${code})`, { plugins: ['typescript'] }).program.body[0].expression;
65
+
66
+ it.each([
67
+ ['"hello"', 'primitive'],
68
+ ['42', 'primitive'],
69
+ ['/foo/g', 'primitive'],
70
+ ['`hello`', 'template-literal'],
71
+ ['() => 1', 'arrow-function'],
72
+ ['function () { return 1; }', 'function-expression'],
73
+ ['[1, 2, 3]', 'array'],
74
+ ['{ a: 1 }', 'object'],
75
+ ['a', 'expression'],
76
+ ['a.b.c', 'expression'],
77
+ ['1 + 2', 'expression'],
78
+ ['a ? 1 : 2', 'expression'],
79
+ ])('THEN %s → %s', (code, category) => {
80
+ expect(classifyInit(expr(code))).toBe(category);
81
+ });
82
+
83
+ it('THEN it unwraps `as` / `!` / `satisfies` / parens before classifying', () => {
84
+ expect(classifyInit(expr('(() => 1) as unknown'))).toBe('arrow-function');
85
+ expect(classifyInit(expr('(1)!'))).toBe('primitive');
86
+ expect(classifyInit(expr('({ a: 1 } satisfies unknown)'))).toBe('object');
87
+ });
88
+ });
89
+
90
+ // ─── resolveInitTypes ───────────────────────────────────────────────────────
91
+
92
+ describe('GIVEN "consistent-const-scoping" / resolveInitTypes', () => {
93
+ it('THEN it defaults to arrow-function only', () => {
94
+ expect(resolveInitTypes()).toEqual(new Set(DEFAULT_INIT_TYPES));
95
+ expect(resolveInitTypes(undefined)).toEqual(new Set(['arrow-function']));
96
+ });
97
+
98
+ it('THEN an explicit "types" option overrides the default', () => {
99
+ expect(resolveInitTypes({ types: ['arrow-function', 'primitive'] }))
100
+ .toEqual(new Set(['arrow-function', 'primitive']));
101
+ });
102
+ });