@tcy/eslint-rules 1.12.0 → 1.12.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 +185 -122
- package/coverage/coverage-final.json +1 -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 +1 -1
- 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 +1 -1
- 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 +1 -1
- 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 +20 -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/no-unused-setup-return.js.html +382 -49
- package/coverage/lcov-report/src/no-v-model-on-prop.js.html +1 -1
- package/coverage/lcov-report/src/plugin.js.html +1 -1
- 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 +388 -271
- package/decl/no-unused-setup-return.d.ts +7 -0
- package/package.json +1 -1
- package/src/no-unused-setup-return.js +115 -4
- package/tests/no-unused-setup-return.spec.js +49 -1
|
@@ -43,6 +43,11 @@ declare const _default: {
|
|
|
43
43
|
export default _default;
|
|
44
44
|
/** `useXxx.ts`, but not the render function */
|
|
45
45
|
export declare const isComposableFile: (filename: any) => boolean;
|
|
46
|
+
/**
|
|
47
|
+
* The composable is imported by a file of another directory: relative specifiers must resolve to
|
|
48
|
+
* it, other ones (`@/...`) are matched by file name
|
|
49
|
+
*/
|
|
50
|
+
export declare const isImportedElsewhere: (filename: any) => any;
|
|
46
51
|
export declare const stripComments: (text: any) => any;
|
|
47
52
|
/** `name` found as a whole word in one of the texts */
|
|
48
53
|
export declare const isUsed: (name: any, texts: any) => any;
|
|
@@ -57,6 +62,8 @@ export declare const getWholeContextUsage: (text: any) => {
|
|
|
57
62
|
};
|
|
58
63
|
/** `usesWholeContext`: the render hides the keys it reads */
|
|
59
64
|
export declare const usesWholeContext: (text: any) => boolean;
|
|
65
|
+
/** Files imported as `render` (or `renderXxx`) by relative path */
|
|
66
|
+
export declare const readImportedRenders: (text: any, dir: any) => any[];
|
|
60
67
|
/** Text of the file imported as `name` with a relative path, `null` if not resolvable */
|
|
61
68
|
export declare const readImported: (name: any, text: any, dir: any) => any;
|
|
62
69
|
/** `Component.ts`: not a composable, not the render */
|
package/package.json
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
// but never reports a used one. `ref="name"` is a use too.
|
|
17
17
|
//
|
|
18
18
|
// The directory is skipped when:
|
|
19
|
+
// - the composable is imported by a file of another directory (a component reusing it)
|
|
19
20
|
// - it has no `Component.ts` (a file calling `defineComponent`) importing the composable:
|
|
20
21
|
// a composable shared by many components, or used through inject / by a parent
|
|
21
22
|
// - a render file spreads (`...c`), or passes the whole context to a function (`foo(c)`) that
|
|
@@ -59,6 +60,9 @@ export default {
|
|
|
59
60
|
// otherwise it is shared (inject, parent, ...) and the keys are read from elsewhere
|
|
60
61
|
const base = path.basename(filename).replace(EXT, '');
|
|
61
62
|
if (isComposable) {
|
|
63
|
+
// used by a component of another directory: its keys are read there
|
|
64
|
+
if (isImportedElsewhere(filename)) return {};
|
|
65
|
+
|
|
62
66
|
const component = siblings.find((s) => !s.inner && isComponentFile(s));
|
|
63
67
|
if (!component || !new RegExp(`from\\s+['"]\\./${escapeRegExp(base)}['"]`).test(component.text)) return {};
|
|
64
68
|
} else if (!/\bdefineComponent\s*\(/.test(context.sourceCode.text)) {
|
|
@@ -66,6 +70,16 @@ export default {
|
|
|
66
70
|
}
|
|
67
71
|
const isTarget = (fn, names) => (isComposable ? isExported(fn, names) : isSetup(fn));
|
|
68
72
|
|
|
73
|
+
// the render can be shared and live elsewhere (`import render from '../useXxxRender'`)
|
|
74
|
+
const componentText = isComposable
|
|
75
|
+
? siblings.find((s) => !s.inner && isComponentFile(s))?.text
|
|
76
|
+
: context.sourceCode.text;
|
|
77
|
+
for (const text of readImportedRenders(componentText ?? '', path.dirname(filename))) {
|
|
78
|
+
siblings.push({
|
|
79
|
+
name: 'render', inner: false, isRender: true, isComposable: false, text,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
69
83
|
const texts = siblings.map((s) => s.text);
|
|
70
84
|
|
|
71
85
|
// the render passes its whole context: `...c` hides the keys that are read, `foo(c)` is
|
|
@@ -153,6 +167,94 @@ const readSiblings = (filename) => {
|
|
|
153
167
|
|
|
154
168
|
const MAX_DEPTH = 4;
|
|
155
169
|
|
|
170
|
+
// ─── importers of a composable, in the whole project ────────────────────────
|
|
171
|
+
|
|
172
|
+
const IMPORT_RE = /(?:from|import)\s*\(?\s*['"]([^'"\n]+)['"]/g;
|
|
173
|
+
const SKIP_DIRS = new Set(['node_modules', 'dist', 'dist_types', 'coverage', '.git', 'decl']);
|
|
174
|
+
const INDEX_TTL = 5000;
|
|
175
|
+
|
|
176
|
+
const fileImports = new Map(); // file → { mtimeMs, specs }
|
|
177
|
+
const indexes = new Map(); // root → { time, byBase: Map<basename, [{ file, spec }]> }
|
|
178
|
+
|
|
179
|
+
const findRoot = (dir) => {
|
|
180
|
+
let d = dir;
|
|
181
|
+
while (true) {
|
|
182
|
+
if (fs.existsSync(path.join(d, 'package.json'))) return d;
|
|
183
|
+
const up = path.dirname(d);
|
|
184
|
+
if (up === d) return dir;
|
|
185
|
+
d = up;
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const collectFiles = (dir, out) => {
|
|
190
|
+
let entries;
|
|
191
|
+
try {
|
|
192
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
193
|
+
} catch {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
for (const e of entries) {
|
|
197
|
+
if (e.isDirectory()) {
|
|
198
|
+
if (!SKIP_DIRS.has(e.name) && !e.name.startsWith('.')) collectFiles(path.join(dir, e.name), out);
|
|
199
|
+
} else if (EXT.test(e.name)) {
|
|
200
|
+
out.push(path.join(dir, e.name));
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
const getIndex = (root) => {
|
|
206
|
+
const hit = indexes.get(root);
|
|
207
|
+
if (hit && Date.now() - hit.time < INDEX_TTL) return hit.byBase;
|
|
208
|
+
|
|
209
|
+
const src = path.join(root, 'src');
|
|
210
|
+
const files = [];
|
|
211
|
+
collectFiles(fs.existsSync(src) ? src : root, files);
|
|
212
|
+
|
|
213
|
+
const byBase = new Map();
|
|
214
|
+
for (const file of files) {
|
|
215
|
+
let specs;
|
|
216
|
+
try {
|
|
217
|
+
const { mtimeMs } = fs.statSync(file);
|
|
218
|
+
const cached = fileImports.get(file);
|
|
219
|
+
if (cached?.mtimeMs === mtimeMs) {
|
|
220
|
+
specs = cached.specs;
|
|
221
|
+
} else {
|
|
222
|
+
specs = [...fs.readFileSync(file, 'utf8').matchAll(IMPORT_RE)].map((m) => m[1]);
|
|
223
|
+
fileImports.set(file, { mtimeMs, specs });
|
|
224
|
+
}
|
|
225
|
+
} catch {
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
for (const spec of specs) {
|
|
230
|
+
const base = path.basename(spec).replace(EXT, '');
|
|
231
|
+
if (!byBase.has(base)) byBase.set(base, []);
|
|
232
|
+
byBase.get(base).push({ file, spec });
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
indexes.set(root, { time: Date.now(), byBase });
|
|
237
|
+
return byBase;
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* The composable is imported by a file of another directory: relative specifiers must resolve to
|
|
242
|
+
* it, other ones (`@/...`) are matched by file name
|
|
243
|
+
*/
|
|
244
|
+
export const isImportedElsewhere = (filename) => {
|
|
245
|
+
const dir = path.dirname(filename);
|
|
246
|
+
const base = path.basename(filename).replace(EXT, '');
|
|
247
|
+
const importers = getIndex(findRoot(dir)).get(base) ?? [];
|
|
248
|
+
const target = path.join(dir, base);
|
|
249
|
+
|
|
250
|
+
return importers.some(({ file, spec }) => {
|
|
251
|
+
if (path.dirname(file) === dir) return false;
|
|
252
|
+
if (!spec.startsWith('.')) return true;
|
|
253
|
+
return path.resolve(path.dirname(file), spec).replace(EXT, '') === target;
|
|
254
|
+
});
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
// `inner` depends on the directory being linted, so it is not part of the cached file
|
|
156
258
|
// files of the directory, `inner` the ones of its sub-directories: a child component can read
|
|
157
259
|
// the keys through the context type
|
|
158
260
|
const walk = (dir, self, depth, result) => {
|
|
@@ -178,19 +280,18 @@ const walk = (dir, self, depth, result) => {
|
|
|
178
280
|
const { mtimeMs } = fs.statSync(file);
|
|
179
281
|
const hit = cache.get(file);
|
|
180
282
|
if (hit?.mtimeMs === mtimeMs) {
|
|
181
|
-
result.push(hit.sibling);
|
|
283
|
+
result.push({ ...hit.sibling, inner: depth > 0 });
|
|
182
284
|
continue;
|
|
183
285
|
}
|
|
184
286
|
|
|
185
287
|
const sibling = {
|
|
186
288
|
name,
|
|
187
|
-
inner: depth > 0,
|
|
188
289
|
isRender: /^useRender\./.test(name),
|
|
189
290
|
isComposable: /^use[A-Z]/.test(name),
|
|
190
291
|
text: stripComments(fs.readFileSync(file, 'utf8')),
|
|
191
292
|
};
|
|
192
293
|
cache.set(file, { mtimeMs, sibling });
|
|
193
|
-
result.push(sibling);
|
|
294
|
+
result.push({ ...sibling, inner: depth > 0 });
|
|
194
295
|
} catch {
|
|
195
296
|
// unreadable file, ignored
|
|
196
297
|
}
|
|
@@ -225,7 +326,7 @@ export const getWholeContextUsage = (text) => {
|
|
|
225
326
|
if (!ctx?.name) return { spread: false, callees: [] };
|
|
226
327
|
|
|
227
328
|
const n = escapeRegExp(ctx.name);
|
|
228
|
-
if (new RegExp(`\\.\\.\\.${n}(?![\\w
|
|
329
|
+
if (new RegExp(`\\.\\.\\.${n}(?![\\w$.\\[?])`).test(ctx.rest)) return { spread: true, callees: [] };
|
|
229
330
|
|
|
230
331
|
const callees = [];
|
|
231
332
|
const call = new RegExp(`([\\w$]+)\\s*\\(([^()]*,\\s*)?${n}\\s*[,)]`, 'g');
|
|
@@ -242,6 +343,16 @@ export const usesWholeContext = (text) => {
|
|
|
242
343
|
return spread || callees.length > 0;
|
|
243
344
|
};
|
|
244
345
|
|
|
346
|
+
/** Files imported as `render` (or `renderXxx`) by relative path */
|
|
347
|
+
export const readImportedRenders = (text, dir) => {
|
|
348
|
+
const result = [];
|
|
349
|
+
for (const m of text.matchAll(/import\s+(render\w*)\s+from\s+['"](\.[^'"]*)['"]/gi)) {
|
|
350
|
+
const content = readImported(m[1], text, dir);
|
|
351
|
+
if (content !== null) result.push(content);
|
|
352
|
+
}
|
|
353
|
+
return result;
|
|
354
|
+
};
|
|
355
|
+
|
|
245
356
|
const RESOLVE_EXT = ['.ts', '.tsx', '.js', '/index.ts', '/index.tsx', '/index.js'];
|
|
246
357
|
|
|
247
358
|
/** Text of the file imported as `name` with a relative path, `null` if not resolvable */
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
1
4
|
import {
|
|
2
|
-
getKeyName, getWholeContextUsage, isComponentFileName, isComposableFile, isUsed, stripComments, usesWholeContext,
|
|
5
|
+
getKeyName, getWholeContextUsage, isComponentFileName, isComposableFile, isImportedElsewhere, readImportedRenders, isUsed, stripComments, usesWholeContext,
|
|
3
6
|
} from '../src/no-unused-setup-return';
|
|
4
7
|
|
|
5
8
|
describe('GIVEN "no-unused-setup-return" / isComposableFile', () => {
|
|
@@ -69,6 +72,7 @@ describe('GIVEN "no-unused-setup-return" / getWholeContextUsage', () => {
|
|
|
69
72
|
['export default function (c: Foo) { return <A>{render(c)}</A>; }', { spread: false, callees: ['render'] }],
|
|
70
73
|
['export default function (c: Foo) { return <A>{f(value, row, c)}</A>; }', { spread: false, callees: ['f'] }],
|
|
71
74
|
['export default function (c: Foo) { return <A v={c.a} />; }', { spread: false, callees: [] }],
|
|
75
|
+
['export default function (c: Foo) { return <A {...c.$attrs} {...c?.b} {...c[k]} />; }', { spread: false, callees: [] }],
|
|
72
76
|
])('THEN %s → %j', (text, expected) => {
|
|
73
77
|
expect(getWholeContextUsage(text)).toEqual(expected);
|
|
74
78
|
});
|
|
@@ -86,3 +90,47 @@ describe('GIVEN "no-unused-setup-return" / isComponentFileName', () => {
|
|
|
86
90
|
expect(isComponentFileName(file)).toBe(expected);
|
|
87
91
|
});
|
|
88
92
|
});
|
|
93
|
+
|
|
94
|
+
describe('GIVEN "no-unused-setup-return" / isImportedElsewhere', () => {
|
|
95
|
+
let dir;
|
|
96
|
+
beforeAll(() => {
|
|
97
|
+
dir = fs.mkdtempSync(path.join(os.tmpdir(), 'nusr-'));
|
|
98
|
+
const w = (f, t) => { fs.mkdirSync(path.dirname(path.join(dir, f)), { recursive: true }); fs.writeFileSync(path.join(dir, f), t); };
|
|
99
|
+
w('package.json', '{}');
|
|
100
|
+
w('src/a/useA.ts', 'export default () => ({})');
|
|
101
|
+
w('src/a/A.ts', "import setup from './useA';");
|
|
102
|
+
w('src/b/B.ts', "import useA from '../a/useA';");
|
|
103
|
+
w('src/c/useC.ts', 'export default () => ({})');
|
|
104
|
+
w('src/c/C.ts', "import setup from './useC';");
|
|
105
|
+
w('src/d/useD.ts', 'export default () => ({})');
|
|
106
|
+
w('src/e/E.ts', "import useD from '../x/useD';");
|
|
107
|
+
});
|
|
108
|
+
afterAll(() => fs.rmSync(dir, { recursive: true, force: true }));
|
|
109
|
+
|
|
110
|
+
it('THEN imported by another directory → true', () => {
|
|
111
|
+
expect(isImportedElsewhere(path.join(dir, 'src/a/useA.ts'))).toBe(true);
|
|
112
|
+
});
|
|
113
|
+
it('THEN imported only by its own directory → false', () => {
|
|
114
|
+
expect(isImportedElsewhere(path.join(dir, 'src/c/useC.ts'))).toBe(false);
|
|
115
|
+
});
|
|
116
|
+
it('THEN a relative import resolving elsewhere is ignored → false', () => {
|
|
117
|
+
expect(isImportedElsewhere(path.join(dir, 'src/d/useD.ts'))).toBe(false);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
describe('GIVEN "no-unused-setup-return" / readImportedRenders', () => {
|
|
122
|
+
it('THEN reads a shared render of the parent directory', () => {
|
|
123
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'nusr-r-'));
|
|
124
|
+
fs.mkdirSync(path.join(dir, 'child'));
|
|
125
|
+
fs.writeFileSync(path.join(dir, 'useShared.tsx'), '// c.nope\nexport default (c) => c.formTitle;');
|
|
126
|
+
const texts = readImportedRenders("import render from '../useShared';\nimport other from './x';", path.join(dir, 'child'));
|
|
127
|
+
expect(texts).toHaveLength(1);
|
|
128
|
+
expect(isUsed('formTitle', texts)).toBe(true);
|
|
129
|
+
expect(isUsed('nope', texts)).toBe(false);
|
|
130
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('THEN an alias import is ignored', () => {
|
|
134
|
+
expect(readImportedRenders("import render from '@/x/render';", '/tmp')).toEqual([]);
|
|
135
|
+
});
|
|
136
|
+
});
|