@tcy/eslint-rules 1.11.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.
Files changed (44) hide show
  1. package/README.md +26 -0
  2. package/coverage/clover.xml +203 -4
  3. package/coverage/coverage-final.json +1 -0
  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 +5 -2
  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 +1 -1
  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 +24 -9
  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/no-unused-setup-return.js.html +1342 -0
  27. package/coverage/lcov-report/src/no-v-model-on-prop.js.html +1 -1
  28. package/coverage/lcov-report/src/plugin.js.html +8 -2
  29. package/coverage/lcov-report/src/recommended.js.html +1 -1
  30. package/coverage/lcov-report/src/require-fragment-key.js.html +1 -1
  31. package/coverage/lcov-report/src/route-dto-required.js.html +1 -1
  32. package/coverage/lcov-report/src/v-model-name-match.js.html +1 -1
  33. package/coverage/lcov-report/src/vuejs-define-component.js.html +1 -1
  34. package/coverage/lcov-report/src/vuejs-define-workspace.js.html +1 -1
  35. package/coverage/lcov.info +434 -0
  36. package/decl/back/back.d.ts +42 -0
  37. package/decl/client/client.d.ts +43 -0
  38. package/decl/no-unused-setup-return.d.ts +71 -0
  39. package/decl/plugin.d.ts +42 -0
  40. package/package.json +2 -2
  41. package/src/client/client.js +1 -0
  42. package/src/no-unused-setup-return.js +419 -0
  43. package/src/plugin.js +2 -0
  44. package/tests/no-unused-setup-return.spec.js +136 -0
@@ -0,0 +1,71 @@
1
+ declare const _default: {
2
+ meta: {
3
+ type: string;
4
+ docs: {
5
+ description: string;
6
+ };
7
+ messages: {
8
+ unused: string;
9
+ };
10
+ schema: {
11
+ type: string;
12
+ properties: {
13
+ ignore: {
14
+ type: string;
15
+ items: {
16
+ type: string;
17
+ };
18
+ };
19
+ };
20
+ additionalProperties: boolean;
21
+ }[];
22
+ };
23
+ create(context: any): {
24
+ Program?: undefined;
25
+ FunctionDeclaration?: undefined;
26
+ 'FunctionDeclaration:exit'?: undefined;
27
+ FunctionExpression?: undefined;
28
+ 'FunctionExpression:exit'?: undefined;
29
+ ArrowFunctionExpression?: undefined;
30
+ 'ArrowFunctionExpression:exit'?: undefined;
31
+ ReturnStatement?: undefined;
32
+ } | {
33
+ Program(program: any): void;
34
+ FunctionDeclaration: (node: any) => void;
35
+ 'FunctionDeclaration:exit': () => void;
36
+ FunctionExpression: (node: any) => void;
37
+ 'FunctionExpression:exit': () => void;
38
+ ArrowFunctionExpression(node: any): void;
39
+ 'ArrowFunctionExpression:exit': () => void;
40
+ ReturnStatement(node: any): void;
41
+ };
42
+ };
43
+ export default _default;
44
+ /** `useXxx.ts`, but not the render function */
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;
51
+ export declare const stripComments: (text: any) => any;
52
+ /** `name` found as a whole word in one of the texts */
53
+ export declare const isUsed: (name: any, texts: any) => any;
54
+ /**
55
+ * How the render uses its whole context (`c`):
56
+ * - `{ spread: true }` `...c`: any key can be read
57
+ * - `{ callees: [...] }` `foo(a, c)`: the names of the called functions, they read keys of `c`
58
+ */
59
+ export declare const getWholeContextUsage: (text: any) => {
60
+ spread: boolean;
61
+ callees: any[];
62
+ };
63
+ /** `usesWholeContext`: the render hides the keys it reads */
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[];
67
+ /** Text of the file imported as `name` with a relative path, `null` if not resolvable */
68
+ export declare const readImported: (name: any, text: any, dir: any) => any;
69
+ /** `Component.ts`: not a composable, not the render */
70
+ export declare const isComponentFileName: (filename: any) => boolean;
71
+ export declare const getKeyName: (prop: any) => any;
package/decl/plugin.d.ts CHANGED
@@ -301,5 +301,47 @@ declare const _default: {
301
301
  JSXAttribute(node: any): void;
302
302
  };
303
303
  };
304
+ 'no-unused-setup-return': {
305
+ meta: {
306
+ type: string;
307
+ docs: {
308
+ description: string;
309
+ };
310
+ messages: {
311
+ unused: string;
312
+ };
313
+ schema: {
314
+ type: string;
315
+ properties: {
316
+ ignore: {
317
+ type: string;
318
+ items: {
319
+ type: string;
320
+ };
321
+ };
322
+ };
323
+ additionalProperties: boolean;
324
+ }[];
325
+ };
326
+ create(context: any): {
327
+ Program?: undefined;
328
+ FunctionDeclaration?: undefined;
329
+ 'FunctionDeclaration:exit'?: undefined;
330
+ FunctionExpression?: undefined;
331
+ 'FunctionExpression:exit'?: undefined;
332
+ ArrowFunctionExpression?: undefined;
333
+ 'ArrowFunctionExpression:exit'?: undefined;
334
+ ReturnStatement?: undefined;
335
+ } | {
336
+ Program(program: any): void;
337
+ FunctionDeclaration: (node: any) => void;
338
+ 'FunctionDeclaration:exit': () => void;
339
+ FunctionExpression: (node: any) => void;
340
+ 'FunctionExpression:exit': () => void;
341
+ ArrowFunctionExpression(node: any): void;
342
+ 'ArrowFunctionExpression:exit': () => void;
343
+ ReturnStatement(node: any): void;
344
+ };
345
+ };
304
346
  };
305
347
  export default _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tcy/eslint-rules",
3
- "version": "1.11.0",
3
+ "version": "1.12.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,7 +47,7 @@
47
47
  "test": "jest"
48
48
  },
49
49
  "devDependencies": {
50
- "@babel/core": "^8.1.0",
50
+ "@babel/core": "^8.1.2",
51
51
  "@babel/preset-env": "^8.0.7",
52
52
  "@types/jest": "^30.0.0",
53
53
  "@types/node": "^26.6.6",
@@ -22,5 +22,6 @@ export default {
22
22
  'tcy/no-static-key-in-map': 'error',
23
23
  'tcy/consistent-const-scoping': 'error',
24
24
  'tcy/no-v-model-on-prop': 'error',
25
+ 'tcy/no-unused-setup-return': 'error',
25
26
  }
26
27
  }
@@ -0,0 +1,419 @@
1
+ // Disallow a key returned by a composition function (`useXxx.ts`) or by the `setup` of `Component.ts`
2
+ // that nothing else of the component reads:
3
+ //
4
+ // Component.ts defineComponent + setup, spreads the composables in a single object
5
+ // useComponent.ts composable ← the rule runs here, on the returned object
6
+ // Component.ts the `setup` returned object is checked too
7
+ // useComponent2.ts other composable
8
+ // useRender.tsx render, reads the keys through `c.key`
9
+ //
10
+ // A returned key is used when its name is found in another file of the same directory (or of its
11
+ // sub-directories, a child can read it through the context type): the render,
12
+ // `Component.ts` or another composable (`useGridConfig(p, { columnFilteredName })`). A key used only
13
+ // by a composable is not reported, it can be a value shared between compositions, not rendered.
14
+ //
15
+ // The check is by name, on purpose: it can miss an unused key sharing its name with something else,
16
+ // but never reports a used one. `ref="name"` is a use too.
17
+ //
18
+ // The directory is skipped when:
19
+ // - the composable is imported by a file of another directory (a component reusing it)
20
+ // - it has no `Component.ts` (a file calling `defineComponent`) importing the composable:
21
+ // a composable shared by many components, or used through inject / by a parent
22
+ // - a render file spreads (`...c`), or passes the whole context to a function (`foo(c)`) that
23
+ // can't be resolved (relative import): any key can be read. A resolved function is searched too
24
+
25
+ import fs from 'node:fs';
26
+ import path from 'node:path';
27
+
28
+ export default {
29
+ meta: {
30
+ type: 'suggestion',
31
+ docs: {
32
+ description: 'Disallow a key returned by a composable that is not used by the component (render or other compositions).',
33
+ },
34
+ messages: {
35
+ unused: '"{{name}}" is returned by the setup but not used by the render or by another composition of the component.',
36
+ },
37
+ schema: [{
38
+ type: 'object',
39
+ properties: {
40
+ // keys that are never reported
41
+ ignore: { type: 'array', items: { type: 'string' } },
42
+ },
43
+ additionalProperties: false,
44
+ }],
45
+ },
46
+
47
+ create(context) {
48
+ const filename = context.filename ?? context.getFilename();
49
+
50
+ // `Component.ts` (`setup`) or a composable (`useXxx.ts`, default export)
51
+ const isComposable = isComposableFile(filename);
52
+ if (!isComposable && !isComponentFileName(filename)) return {};
53
+
54
+ const { ignore = [] } = context.options[0] || {};
55
+
56
+ const siblings = readSiblings(filename);
57
+ if (!siblings) return {};
58
+
59
+ // the composable is part of the setup of the component of its directory,
60
+ // otherwise it is shared (inject, parent, ...) and the keys are read from elsewhere
61
+ const base = path.basename(filename).replace(EXT, '');
62
+ if (isComposable) {
63
+ // used by a component of another directory: its keys are read there
64
+ if (isImportedElsewhere(filename)) return {};
65
+
66
+ const component = siblings.find((s) => !s.inner && isComponentFile(s));
67
+ if (!component || !new RegExp(`from\\s+['"]\\./${escapeRegExp(base)}['"]`).test(component.text)) return {};
68
+ } else if (!/\bdefineComponent\s*\(/.test(context.sourceCode.text)) {
69
+ return {};
70
+ }
71
+ const isTarget = (fn, names) => (isComposable ? isExported(fn, names) : isSetup(fn));
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
+
83
+ const texts = siblings.map((s) => s.text);
84
+
85
+ // the render passes its whole context: `...c` hides the keys that are read, `foo(c)` is
86
+ // resolved and the file of `foo` is searched too (not resolvable: every key can be used)
87
+ for (const s of siblings) {
88
+ if (!s.isRender || s.inner) continue;
89
+
90
+ const { spread, callees } = getWholeContextUsage(s.text);
91
+ if (spread) return {};
92
+
93
+ for (const callee of callees) {
94
+ const imported = readImported(callee, s.text, path.dirname(filename));
95
+ if (imported === null) return {};
96
+ texts.push(imported);
97
+ }
98
+ }
99
+
100
+ // function stack, only the exported function counts
101
+ const stack = [];
102
+ const enter = (node) => { stack.push(node); };
103
+ const leave = () => { stack.pop(); };
104
+
105
+ const exportedNames = new Set();
106
+
107
+ return {
108
+ Program(program) {
109
+ for (const node of program.body) {
110
+ if (node.type === 'ExportDefaultDeclaration' && node.declaration.type === 'Identifier') {
111
+ exportedNames.add(node.declaration.name);
112
+ }
113
+ }
114
+ },
115
+
116
+ FunctionDeclaration: enter,
117
+ 'FunctionDeclaration:exit': leave,
118
+ FunctionExpression: enter,
119
+ 'FunctionExpression:exit': leave,
120
+ ArrowFunctionExpression(node) {
121
+ enter(node);
122
+ // `=> ({ a, b })`
123
+ if (node.body.type === 'ObjectExpression' && isTarget(node, exportedNames)) {
124
+ check(context, node.body, texts, ignore);
125
+ }
126
+ },
127
+ 'ArrowFunctionExpression:exit': leave,
128
+
129
+ ReturnStatement(node) {
130
+ if (node.argument?.type !== 'ObjectExpression') return;
131
+ const fn = stack[stack.length - 1];
132
+ if (!fn || !isTarget(fn, exportedNames)) return;
133
+ check(context, node.argument, texts, ignore);
134
+ },
135
+ };
136
+ },
137
+ };
138
+
139
+ // ─── helpers ────────────────────────────────────────────────────────────────
140
+
141
+ const EXT = /\.(ts|tsx|js|jsx)$/;
142
+
143
+ /** `useXxx.ts`, but not the render function */
144
+ export const isComposableFile = (filename) => {
145
+ const base = path.basename(filename);
146
+ return /^use[A-Z].*\.(ts|js)$/.test(base) && !/^useRender\./.test(base) && !/\.(spec|test)\./.test(base);
147
+ };
148
+
149
+ const isComponentFile = (sibling) => !sibling.isComposable && /\bdefineComponent\s*\(/.test(sibling.text);
150
+
151
+ const cache = new Map();
152
+
153
+ /** Other files of the directory, `null` if it can't be read */
154
+ const readSiblings = (filename) => {
155
+ const dir = path.dirname(filename);
156
+ let names;
157
+ try {
158
+ names = fs.readdirSync(dir, { withFileTypes: true });
159
+ } catch {
160
+ return null;
161
+ }
162
+
163
+ const result = [];
164
+ walk(dir, filename, 0, result);
165
+ return result;
166
+ };
167
+
168
+ const MAX_DEPTH = 4;
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
258
+ // files of the directory, `inner` the ones of its sub-directories: a child component can read
259
+ // the keys through the context type
260
+ const walk = (dir, self, depth, result) => {
261
+ let entries;
262
+ try {
263
+ entries = fs.readdirSync(dir, { withFileTypes: true });
264
+ } catch {
265
+ return;
266
+ }
267
+
268
+ for (const entry of entries) {
269
+ const file = path.join(dir, entry.name);
270
+
271
+ if (entry.isDirectory()) {
272
+ if (depth < MAX_DEPTH && entry.name !== 'node_modules') walk(file, self, depth + 1, result);
273
+ continue;
274
+ }
275
+
276
+ const { name } = entry;
277
+ if (file === self || !EXT.test(name) || /\.(spec|test)\./.test(name) || /\.d\.ts$/.test(name)) continue;
278
+
279
+ try {
280
+ const { mtimeMs } = fs.statSync(file);
281
+ const hit = cache.get(file);
282
+ if (hit?.mtimeMs === mtimeMs) {
283
+ result.push({ ...hit.sibling, inner: depth > 0 });
284
+ continue;
285
+ }
286
+
287
+ const sibling = {
288
+ name,
289
+ isRender: /^useRender\./.test(name),
290
+ isComposable: /^use[A-Z]/.test(name),
291
+ text: stripComments(fs.readFileSync(file, 'utf8')),
292
+ };
293
+ cache.set(file, { mtimeMs, sibling });
294
+ result.push({ ...sibling, inner: depth > 0 });
295
+ } catch {
296
+ // unreadable file, ignored
297
+ }
298
+ }
299
+ };
300
+
301
+ export const stripComments = (text) => text
302
+ .replace(/\/\*[\s\S]*?\*\//g, '')
303
+ .replace(/(^|[^:'"`])\/\/.*$/gm, '$1');
304
+
305
+ const escapeRegExp = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
306
+
307
+ /** `name` found as a whole word in one of the texts */
308
+ export const isUsed = (name, texts) => {
309
+ const re = new RegExp(`(?<![\\w$])${escapeRegExp(name)}(?![\\w$])`);
310
+ return texts.some((t) => re.test(t));
311
+ };
312
+
313
+ /** Name of the first parameter of the render function, `null` if destructured / not found */
314
+ const getContextName = (text) => {
315
+ const m = /(?:function\s*\w*\s*|=\s*)\(\s*(?:\{[^}]*\}|(\w+))\s*(?::[^)]*)?\)/.exec(text);
316
+ return m ? { name: m[1] ?? null, rest: text.slice(m.index + m[0].length) } : null;
317
+ };
318
+
319
+ /**
320
+ * How the render uses its whole context (`c`):
321
+ * - `{ spread: true }` `...c`: any key can be read
322
+ * - `{ callees: [...] }` `foo(a, c)`: the names of the called functions, they read keys of `c`
323
+ */
324
+ export const getWholeContextUsage = (text) => {
325
+ const ctx = getContextName(text);
326
+ if (!ctx?.name) return { spread: false, callees: [] };
327
+
328
+ const n = escapeRegExp(ctx.name);
329
+ if (new RegExp(`\\.\\.\\.${n}(?![\\w$.\\[?])`).test(ctx.rest)) return { spread: true, callees: [] };
330
+
331
+ const callees = [];
332
+ const call = new RegExp(`([\\w$]+)\\s*\\(([^()]*,\\s*)?${n}\\s*[,)]`, 'g');
333
+ for (const m of ctx.rest.matchAll(call)) callees.push(m[1]);
334
+ // a bare `(c)` / `, c)` not matched above: unknown callee
335
+ const bare = new RegExp(`[(,]\\s*${n}\\s*[,)]`, 'g');
336
+ const unknown = [...ctx.rest.matchAll(bare)].length > callees.length;
337
+ return { spread: unknown, callees };
338
+ };
339
+
340
+ /** `usesWholeContext`: the render hides the keys it reads */
341
+ export const usesWholeContext = (text) => {
342
+ const { spread, callees } = getWholeContextUsage(text);
343
+ return spread || callees.length > 0;
344
+ };
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
+
356
+ const RESOLVE_EXT = ['.ts', '.tsx', '.js', '/index.ts', '/index.tsx', '/index.js'];
357
+
358
+ /** Text of the file imported as `name` with a relative path, `null` if not resolvable */
359
+ export const readImported = (name, text, dir) => {
360
+ const n = escapeRegExp(name);
361
+ const m = new RegExp(`import\\s+${n}\\s+from\\s+['"](\\.[^'"]*)['"]|import\\s*\\{[^}]*\\b${n}\\b[^}]*\\}\\s*from\\s*['"](\\.[^'"]*)['"]`).exec(text);
362
+ const rel = m && (m[1] ?? m[2]);
363
+ if (!rel) return null;
364
+
365
+ for (const ext of RESOLVE_EXT) {
366
+ const file = path.resolve(dir, rel + ext);
367
+ try {
368
+ return stripComments(fs.readFileSync(file, 'utf8'));
369
+ } catch {
370
+ // next candidate
371
+ }
372
+ }
373
+ return null;
374
+ };
375
+
376
+ /** `Component.ts`: not a composable, not the render */
377
+ export const isComponentFileName = (filename) => {
378
+ const base = path.basename(filename);
379
+ return /\.(ts|js)$/.test(base) && !/^use[A-Z]/.test(base) && !/\.(spec|test|d)\./.test(base);
380
+ };
381
+
382
+ /** `const setup = () => {}` or `defineComponent({ setup() {}, setup: () => {} })` */
383
+ const isSetup = (fn) => {
384
+ const { parent } = fn;
385
+ if (parent?.type === 'VariableDeclarator') return parent.id.type === 'Identifier' && parent.id.name === 'setup';
386
+ if (fn.type === 'FunctionDeclaration') return fn.id?.name === 'setup';
387
+ if (parent?.type === 'Property' || parent?.type === 'MethodDefinition') {
388
+ return !parent.computed && parent.key.type === 'Identifier' && parent.key.name === 'setup';
389
+ }
390
+ return false;
391
+ };
392
+
393
+ const isExported = (fn, exportedNames) => {
394
+ const { parent } = fn;
395
+ if (parent?.type === 'ExportDefaultDeclaration') return true;
396
+ if (fn.type === 'FunctionDeclaration' && fn.id && exportedNames.has(fn.id.name)) return true;
397
+ if (parent?.type === 'VariableDeclarator' && parent.id.type === 'Identifier' && exportedNames.has(parent.id.name)) {
398
+ return true;
399
+ }
400
+ return false;
401
+ };
402
+
403
+ export const getKeyName = (prop) => {
404
+ if (prop.type !== 'Property' || prop.computed) return null;
405
+ if (prop.key.type === 'Identifier') return prop.key.name;
406
+ if (prop.key.type === 'Literal' && typeof prop.key.value === 'string') return prop.key.value;
407
+ return null;
408
+ };
409
+
410
+ const check = (context, objectNode, texts, ignore) => {
411
+ for (const prop of objectNode.properties) {
412
+ const name = getKeyName(prop);
413
+ if (!name || ignore.includes(name)) continue;
414
+
415
+ if (!isUsed(name, texts)) {
416
+ context.report({ node: prop, messageId: 'unused', data: { name } });
417
+ }
418
+ }
419
+ };
package/src/plugin.js CHANGED
@@ -14,6 +14,7 @@ import requireFragmentKey from './require-fragment-key.js'
14
14
  import noStaticKeyInMap from './no-static-key-in-map.js'
15
15
  import consistentConstScoping from './consistent-const-scoping.js'
16
16
  import noVModelOnProp from './no-v-model-on-prop.js'
17
+ import noUnusedSetupReturn from './no-unused-setup-return.js'
17
18
 
18
19
  export default {
19
20
  'import-specifiers-per-line': importSpecifiersPerLineRule,
@@ -32,4 +33,5 @@ export default {
32
33
  'no-static-key-in-map': noStaticKeyInMap,
33
34
  'consistent-const-scoping': consistentConstScoping,
34
35
  'no-v-model-on-prop': noVModelOnProp,
36
+ 'no-unused-setup-return': noUnusedSetupReturn,
35
37
  }