@tcy/eslint-rules 1.12.1 → 1.13.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.
- package/README.md +64 -11
- package/coverage/clover.xml +330 -174
- 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 +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 +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 +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 +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/jsx-ref-type-match.js.html +865 -0
- 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 +359 -17
- package/coverage/lcov-report/src/no-v-model-on-prop.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 +739 -362
- package/decl/back/back.d.ts +20 -0
- package/decl/client/client.d.ts +21 -0
- package/decl/jsx-ref-type-match.d.ts +34 -0
- package/decl/no-unused-setup-return.d.ts +2 -0
- package/decl/plugin.d.ts +20 -0
- package/package.json +1 -1
- package/src/client/client.js +1 -0
- package/src/jsx-ref-type-match.js +260 -0
- package/src/no-unused-setup-return.js +120 -6
- package/src/plugin.js +2 -0
- package/tests/jsx-ref-type-match.spec.js +118 -0
- package/tests/no-unused-setup-return.spec.js +17 -1
package/decl/back/back.d.ts
CHANGED
|
@@ -98,6 +98,26 @@ declare const _default: {
|
|
|
98
98
|
JSXAttribute(node: any): void;
|
|
99
99
|
};
|
|
100
100
|
};
|
|
101
|
+
'jsx-ref-type-match': {
|
|
102
|
+
meta: {
|
|
103
|
+
type: string;
|
|
104
|
+
docs: {
|
|
105
|
+
description: string;
|
|
106
|
+
};
|
|
107
|
+
messages: {
|
|
108
|
+
untypedRef: string;
|
|
109
|
+
untypedRefUnknownTag: string;
|
|
110
|
+
unwrappedRef: string;
|
|
111
|
+
refTypeMismatch: string;
|
|
112
|
+
};
|
|
113
|
+
schema: never[];
|
|
114
|
+
};
|
|
115
|
+
create(context: any): {
|
|
116
|
+
JSXAttribute?: undefined;
|
|
117
|
+
} | {
|
|
118
|
+
JSXAttribute(node: any): void;
|
|
119
|
+
};
|
|
120
|
+
};
|
|
101
121
|
'enforce-tailwind-important': {
|
|
102
122
|
meta: {
|
|
103
123
|
type: string;
|
package/decl/client/client.d.ts
CHANGED
|
@@ -98,6 +98,26 @@ declare const _default: {
|
|
|
98
98
|
JSXAttribute(node: any): void;
|
|
99
99
|
};
|
|
100
100
|
};
|
|
101
|
+
'jsx-ref-type-match': {
|
|
102
|
+
meta: {
|
|
103
|
+
type: string;
|
|
104
|
+
docs: {
|
|
105
|
+
description: string;
|
|
106
|
+
};
|
|
107
|
+
messages: {
|
|
108
|
+
untypedRef: string;
|
|
109
|
+
untypedRefUnknownTag: string;
|
|
110
|
+
unwrappedRef: string;
|
|
111
|
+
refTypeMismatch: string;
|
|
112
|
+
};
|
|
113
|
+
schema: never[];
|
|
114
|
+
};
|
|
115
|
+
create(context: any): {
|
|
116
|
+
JSXAttribute?: undefined;
|
|
117
|
+
} | {
|
|
118
|
+
JSXAttribute(node: any): void;
|
|
119
|
+
};
|
|
120
|
+
};
|
|
101
121
|
'enforce-tailwind-important': {
|
|
102
122
|
meta: {
|
|
103
123
|
type: string;
|
|
@@ -358,6 +378,7 @@ declare const _default: {
|
|
|
358
378
|
'tcy/jsx-expression-string-literals': string;
|
|
359
379
|
'tcy/jsx-boolean-shorthand': string;
|
|
360
380
|
'tcy/jsx-ref-string-exists': string;
|
|
381
|
+
'tcy/jsx-ref-type-match': string;
|
|
361
382
|
'tcy/enforce-tailwind-important': string;
|
|
362
383
|
'tcy/v-model-name-match': string;
|
|
363
384
|
'tcy/vuejs-define-component': string;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
declare const _default: {
|
|
2
|
+
meta: {
|
|
3
|
+
type: string;
|
|
4
|
+
docs: {
|
|
5
|
+
description: string;
|
|
6
|
+
};
|
|
7
|
+
messages: {
|
|
8
|
+
untypedRef: string;
|
|
9
|
+
untypedRefUnknownTag: string;
|
|
10
|
+
unwrappedRef: string;
|
|
11
|
+
refTypeMismatch: string;
|
|
12
|
+
};
|
|
13
|
+
schema: never[];
|
|
14
|
+
};
|
|
15
|
+
create(context: any): {
|
|
16
|
+
JSXAttribute?: undefined;
|
|
17
|
+
} | {
|
|
18
|
+
JSXAttribute(node: any): void;
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
export default _default;
|
|
22
|
+
export declare function isStringRef(node: any): boolean;
|
|
23
|
+
/** `div`, `BaseInput`, `Foo.Bar`; `null` for `<ns:tag>` */
|
|
24
|
+
export declare function getTagName(opening: any): any;
|
|
25
|
+
/** JSX rule: a lowercase identifier (or one with a dash) is a native element */
|
|
26
|
+
export declare function isIntrinsicTag(tag: any): any;
|
|
27
|
+
/**
|
|
28
|
+
* Names the instance type of a component can be exported under, in its own file:
|
|
29
|
+
* `export type BaseInput = Context<typeof setup, Props>` next to `export default defineComponent(...)`.
|
|
30
|
+
*/
|
|
31
|
+
export declare function getInstanceTypeNames(exportName: any, fileName: any, tag: any): any[];
|
|
32
|
+
export declare function isAnyOrUnknown(type: any, checker: any): boolean;
|
|
33
|
+
/** `any`, `unknown`, or only `null` / `undefined` (`ref()`, `ref(null)`) */
|
|
34
|
+
export declare function isUntyped(type: any, checker: any): any;
|
|
@@ -69,3 +69,5 @@ export declare const readImported: (name: any, text: any, dir: any) => any;
|
|
|
69
69
|
/** `Component.ts`: not a composable, not the render */
|
|
70
70
|
export declare const isComponentFileName: (filename: any) => boolean;
|
|
71
71
|
export declare const getKeyName: (prop: any) => any;
|
|
72
|
+
/** A comment text marks a key as public */
|
|
73
|
+
export declare const isPublicComment: (text: any) => boolean;
|
package/decl/plugin.d.ts
CHANGED
|
@@ -95,6 +95,26 @@ declare const _default: {
|
|
|
95
95
|
JSXAttribute(node: any): void;
|
|
96
96
|
};
|
|
97
97
|
};
|
|
98
|
+
'jsx-ref-type-match': {
|
|
99
|
+
meta: {
|
|
100
|
+
type: string;
|
|
101
|
+
docs: {
|
|
102
|
+
description: string;
|
|
103
|
+
};
|
|
104
|
+
messages: {
|
|
105
|
+
untypedRef: string;
|
|
106
|
+
untypedRefUnknownTag: string;
|
|
107
|
+
unwrappedRef: string;
|
|
108
|
+
refTypeMismatch: string;
|
|
109
|
+
};
|
|
110
|
+
schema: never[];
|
|
111
|
+
};
|
|
112
|
+
create(context: any): {
|
|
113
|
+
JSXAttribute?: undefined;
|
|
114
|
+
} | {
|
|
115
|
+
JSXAttribute(node: any): void;
|
|
116
|
+
};
|
|
117
|
+
};
|
|
98
118
|
'enforce-tailwind-important': {
|
|
99
119
|
meta: {
|
|
100
120
|
type: string;
|
package/package.json
CHANGED
package/src/client/client.js
CHANGED
|
@@ -14,6 +14,7 @@ export default {
|
|
|
14
14
|
'tcy/jsx-expression-string-literals': 'error',
|
|
15
15
|
'tcy/jsx-boolean-shorthand': 'error',
|
|
16
16
|
'tcy/jsx-ref-string-exists': 'error',
|
|
17
|
+
'tcy/jsx-ref-type-match': 'error',
|
|
17
18
|
'tcy/enforce-tailwind-important': 'error',
|
|
18
19
|
'tcy/v-model-name-match': 'error',
|
|
19
20
|
'tcy/vuejs-define-component': 'error',
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
|
|
3
|
+
// The rule doesn't import `typescript`: the checker comes from the consumer's parser
|
|
4
|
+
// services, whose TS version may differ from ours. `TypeFlags` values changed between
|
|
5
|
+
// TS 5 and 6, so intrinsic types are recognized by name (`checker.typeToString`) instead.
|
|
6
|
+
// The `SymbolFlags` / `SignatureKind` values below are the same in TS 5 and 6.
|
|
7
|
+
const ANY_NAMES = new Set(['any', 'unknown']);
|
|
8
|
+
const EMPTY_NAMES = new Set(['null', 'undefined', 'void', 'never']);
|
|
9
|
+
const SYMBOL_FLAGS_INTERFACE = 64;
|
|
10
|
+
const SYMBOL_FLAGS_TYPE_ALIAS = 524288;
|
|
11
|
+
const SYMBOL_FLAGS_ALIAS = 2097152;
|
|
12
|
+
const SIGNATURE_KIND_CONSTRUCT = 1;
|
|
13
|
+
|
|
14
|
+
const MAX_TYPE_LENGTH = 80;
|
|
15
|
+
|
|
16
|
+
export default {
|
|
17
|
+
meta: {
|
|
18
|
+
type: 'problem',
|
|
19
|
+
docs: {
|
|
20
|
+
description:
|
|
21
|
+
'Ensure the variable behind a string ref (ref="name") is typed with the element or component it is bound to.',
|
|
22
|
+
},
|
|
23
|
+
messages: {
|
|
24
|
+
untypedRef:
|
|
25
|
+
'Ref "{{ref}}" is untyped ({{declared}}) but is bound to <{{tag}}>. Declare it as `ref<{{expected}}>()`.',
|
|
26
|
+
untypedRefUnknownTag:
|
|
27
|
+
'Ref "{{ref}}" is untyped ({{declared}}). Declare it with the type of what <{{tag}}> renders.',
|
|
28
|
+
unwrappedRef:
|
|
29
|
+
'Ref "{{ref}}" is bound to <{{tag}}> but its type is unwrapped by Vue (`ref<T>(initialValue)` types the value as `UnwrapRef<T>`). '
|
|
30
|
+
+ 'Declare it as `ref<{{expected}}>()` or `shallowRef<{{expected}} | null>(null)`.',
|
|
31
|
+
refTypeMismatch:
|
|
32
|
+
'Ref "{{ref}}" is typed "{{declared}}" but is bound to <{{tag}}> ({{expected}}).',
|
|
33
|
+
},
|
|
34
|
+
schema: [],
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
create(context) {
|
|
38
|
+
const services = context.sourceCode.parserServices;
|
|
39
|
+
|
|
40
|
+
// `jsx-ref-string-exists` already reports the missing type information
|
|
41
|
+
if (!services?.program) return {};
|
|
42
|
+
|
|
43
|
+
const checker = services.program.getTypeChecker();
|
|
44
|
+
const toTs = (node) => services.esTreeNodeToTSNodeMap.get(node);
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
JSXAttribute(node) {
|
|
48
|
+
if (!isStringRef(node)) return;
|
|
49
|
+
|
|
50
|
+
const refName = node.value.value;
|
|
51
|
+
const opening = node.parent;
|
|
52
|
+
const tag = getTagName(opening);
|
|
53
|
+
if (!tag) return;
|
|
54
|
+
|
|
55
|
+
const declared = getDeclaredRefType(checker, toTs, node, refName);
|
|
56
|
+
// not found: `jsx-ref-string-exists` reports it
|
|
57
|
+
if (!declared) return;
|
|
58
|
+
|
|
59
|
+
const expected = isIntrinsicTag(tag)
|
|
60
|
+
? getIntrinsicElementType(checker, services.program, tag)
|
|
61
|
+
: getComponentInstanceType(checker, toTs(opening.name), tag);
|
|
62
|
+
|
|
63
|
+
const data = {
|
|
64
|
+
ref: refName,
|
|
65
|
+
tag,
|
|
66
|
+
declared: typeToString(checker, declared),
|
|
67
|
+
expected: expected?.name,
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
if (isUntyped(declared, checker)) {
|
|
71
|
+
context.report({ node, messageId: expected ? 'untypedRef' : 'untypedRefUnknownTag', data });
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// dynamic tag (`<Component>` from a slot) or unknown component: nothing to compare
|
|
76
|
+
if (!expected) return;
|
|
77
|
+
|
|
78
|
+
const target = checker.getNonNullableType(declared);
|
|
79
|
+
if (checker.isTypeAssignableTo(expected.type, target)) return;
|
|
80
|
+
|
|
81
|
+
// `ref<T>(initialValue)` types the value as `UnwrapRef<T>`: for a component it's a
|
|
82
|
+
// structural copy of T, which T doesn't satisfy anymore, while the reverse still holds
|
|
83
|
+
if (!isIntrinsicTag(tag) && checker.isTypeAssignableTo(target, expected.type)) {
|
|
84
|
+
context.report({ node, messageId: 'unwrappedRef', data });
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
context.report({
|
|
89
|
+
node,
|
|
90
|
+
messageId: 'refTypeMismatch',
|
|
91
|
+
data: { ...data, expected: typeToString(checker, expected.type) },
|
|
92
|
+
});
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// ─── helpers ────────────────────────────────────────────────────────────────
|
|
99
|
+
|
|
100
|
+
export function isStringRef(node) {
|
|
101
|
+
return (
|
|
102
|
+
node.name?.type === 'JSXIdentifier'
|
|
103
|
+
&& node.name.name === 'ref'
|
|
104
|
+
&& node.value?.type === 'Literal'
|
|
105
|
+
&& typeof node.value.value === 'string'
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** `div`, `BaseInput`, `Foo.Bar`; `null` for `<ns:tag>` */
|
|
110
|
+
export function getTagName(opening) {
|
|
111
|
+
const name = opening?.name;
|
|
112
|
+
if (!name) return null;
|
|
113
|
+
if (name.type === 'JSXIdentifier') return name.name;
|
|
114
|
+
if (name.type === 'JSXMemberExpression') {
|
|
115
|
+
const object = getTagName({ name: name.object });
|
|
116
|
+
return object && `${object}.${name.property.name}`;
|
|
117
|
+
}
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** JSX rule: a lowercase identifier (or one with a dash) is a native element */
|
|
122
|
+
export function isIntrinsicTag(tag) {
|
|
123
|
+
return !tag.includes('.') && (/^[a-z]/.test(tag) || tag.includes('-'));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Names the instance type of a component can be exported under, in its own file:
|
|
128
|
+
* `export type BaseInput = Context<typeof setup, Props>` next to `export default defineComponent(...)`.
|
|
129
|
+
*/
|
|
130
|
+
export function getInstanceTypeNames(exportName, fileName, tag) {
|
|
131
|
+
const names = [];
|
|
132
|
+
const add = (n) => n && !names.includes(n) && names.push(n);
|
|
133
|
+
|
|
134
|
+
if (exportName && exportName !== 'default') add(exportName);
|
|
135
|
+
if (fileName) add(path.basename(fileName).replace(/\.(d\.)?[cm]?[tj]sx?$/, ''));
|
|
136
|
+
add(tag.split('.').pop());
|
|
137
|
+
|
|
138
|
+
return names;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const FUNCTION_TYPES = new Set([
|
|
142
|
+
'FunctionDeclaration',
|
|
143
|
+
'FunctionExpression',
|
|
144
|
+
'ArrowFunctionExpression',
|
|
145
|
+
]);
|
|
146
|
+
|
|
147
|
+
/** Type of `refName` on the first enclosing function's first param that has it (the render context) */
|
|
148
|
+
function getDeclaredRefType(checker, toTs, node, refName) {
|
|
149
|
+
for (let current = node.parent; current; current = current.parent) {
|
|
150
|
+
if (!FUNCTION_TYPES.has(current.type) || !current.params?.length) continue;
|
|
151
|
+
|
|
152
|
+
const tsParam = toTs(current.params[0]);
|
|
153
|
+
const paramType = checker.getTypeAtLocation(tsParam);
|
|
154
|
+
const prop = checker.getPropertyOfType(paramType, refName);
|
|
155
|
+
if (prop) return checker.getTypeOfSymbolAtLocation(prop, tsParam);
|
|
156
|
+
}
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function isAnyOrUnknown(type, checker) {
|
|
161
|
+
return ANY_NAMES.has(checker.typeToString(type));
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** `any`, `unknown`, or only `null` / `undefined` (`ref()`, `ref(null)`) */
|
|
165
|
+
export function isUntyped(type, checker) {
|
|
166
|
+
if (isAnyOrUnknown(type, checker)) return true;
|
|
167
|
+
const parts = type.isUnion?.() ? type.types : [type];
|
|
168
|
+
return parts.every((t) => EMPTY_NAMES.has(checker.typeToString(t)));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const tagMapsCache = new WeakMap();
|
|
172
|
+
|
|
173
|
+
function getTagMaps(checker, program) {
|
|
174
|
+
if (tagMapsCache.has(program)) return tagMapsCache.get(program);
|
|
175
|
+
|
|
176
|
+
const maps = {};
|
|
177
|
+
const file = program.getSourceFiles().find((f) => !f.isDeclarationFile) || program.getSourceFiles()[0];
|
|
178
|
+
if (file) {
|
|
179
|
+
for (const symbol of checker.getSymbolsInScope(file, SYMBOL_FLAGS_INTERFACE)) {
|
|
180
|
+
if (['HTMLElementTagNameMap', 'SVGElementTagNameMap', 'Element'].includes(symbol.name)) {
|
|
181
|
+
maps[symbol.name] = checker.getDeclaredTypeOfSymbol(symbol);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
tagMapsCache.set(program, maps);
|
|
187
|
+
return maps;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** `div` → HTMLDivElement, `svg` → SVGSVGElement, unknown tag → Element */
|
|
191
|
+
function getIntrinsicElementType(checker, program, tag) {
|
|
192
|
+
const maps = getTagMaps(checker, program);
|
|
193
|
+
|
|
194
|
+
for (const mapName of ['HTMLElementTagNameMap', 'SVGElementTagNameMap']) {
|
|
195
|
+
const map = maps[mapName];
|
|
196
|
+
const prop = map && checker.getPropertyOfType(map, tag);
|
|
197
|
+
if (prop) {
|
|
198
|
+
const type = checker.getTypeOfSymbol(prop);
|
|
199
|
+
return { type, name: typeToString(checker, type) };
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return maps.Element ? { type: maps.Element, name: 'Element' } : null;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function resolveAlias(checker, symbol) {
|
|
207
|
+
return symbol && (symbol.flags & SYMBOL_FLAGS_ALIAS) ? checker.getAliasedSymbol(symbol) : symbol;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Instance type of the component used as tag:
|
|
212
|
+
* 1. the type exported with the component's name from the file declaring it
|
|
213
|
+
* (`export type BaseInput = Context<typeof setup, Props>`). Checked first: `TSXComponent`
|
|
214
|
+
* has no constructor, and generic factories (`export default GGrid<Obj>()`) construct
|
|
215
|
+
* Vue's raw instance type, not the one the code uses.
|
|
216
|
+
* 2. else, for a Vue constructor (`DefineComponent`, third-party components): the type it constructs
|
|
217
|
+
* `null` when it can't be resolved (`any` tag, unknown convention).
|
|
218
|
+
*/
|
|
219
|
+
function getComponentInstanceType(checker, tsTagNode, tag) {
|
|
220
|
+
if (!tsTagNode) return null;
|
|
221
|
+
|
|
222
|
+
const tagType = checker.getTypeAtLocation(tsTagNode);
|
|
223
|
+
if (isAnyOrUnknown(tagType, checker)) return null;
|
|
224
|
+
|
|
225
|
+
const exported = getExportedInstanceType(checker, tsTagNode, tag);
|
|
226
|
+
if (exported) return exported;
|
|
227
|
+
|
|
228
|
+
const [construct] = checker.getSignaturesOfType(tagType, SIGNATURE_KIND_CONSTRUCT);
|
|
229
|
+
if (construct) {
|
|
230
|
+
const type = construct.getReturnType();
|
|
231
|
+
return { type, name: typeToString(checker, type) };
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function getExportedInstanceType(checker, tsTagNode, tag) {
|
|
238
|
+
const symbol = resolveAlias(checker, checker.getSymbolAtLocation(tsTagNode));
|
|
239
|
+
const declaration = symbol?.declarations?.[0];
|
|
240
|
+
if (!declaration) return null;
|
|
241
|
+
|
|
242
|
+
const sourceFile = declaration.getSourceFile();
|
|
243
|
+
const moduleSymbol = checker.getSymbolAtLocation(sourceFile);
|
|
244
|
+
if (!moduleSymbol) return null;
|
|
245
|
+
|
|
246
|
+
const exports = checker.getExportsOfModule(moduleSymbol);
|
|
247
|
+
for (const name of getInstanceTypeNames(symbol.name, sourceFile.fileName, tag)) {
|
|
248
|
+
const exported = resolveAlias(checker, exports.find((e) => e.name === name));
|
|
249
|
+
if (exported && (exported.flags & (SYMBOL_FLAGS_TYPE_ALIAS | SYMBOL_FLAGS_INTERFACE))) {
|
|
250
|
+
return { type: checker.getDeclaredTypeOfSymbol(exported), name };
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function typeToString(checker, type) {
|
|
258
|
+
const str = checker.typeToString(type);
|
|
259
|
+
return str.length > MAX_TYPE_LENGTH ? `${str.slice(0, MAX_TYPE_LENGTH)}…` : str;
|
|
260
|
+
}
|
|
@@ -15,6 +15,11 @@
|
|
|
15
15
|
// The check is by name, on purpose: it can miss an unused key sharing its name with something else,
|
|
16
16
|
// but never reports a used one. `ref="name"` is a use too.
|
|
17
17
|
//
|
|
18
|
+
// A key read from outside the component, through a typed ref (`cmp.value?.focus()` with
|
|
19
|
+
// `ref<VInputCheckboxBoolean>()`), is used too: with type information, the program is searched for an
|
|
20
|
+
// identifier resolving to the returned property. A key read by another app (the public API of a
|
|
21
|
+
// library) can't be found: mark it `/** @public */ focus,` (or `focus, // @public`).
|
|
22
|
+
//
|
|
18
23
|
// The directory is skipped when:
|
|
19
24
|
// - the composable is imported by a file of another directory (a component reusing it)
|
|
20
25
|
// - it has no `Component.ts` (a file calling `defineComponent`) importing the composable:
|
|
@@ -32,7 +37,8 @@ export default {
|
|
|
32
37
|
description: 'Disallow a key returned by a composable that is not used by the component (render or other compositions).',
|
|
33
38
|
},
|
|
34
39
|
messages: {
|
|
35
|
-
unused: '"{{name}}" is returned by the setup but not used by the render or by another composition of the component.'
|
|
40
|
+
unused: '"{{name}}" is returned by the setup but not used by the render or by another composition of the component. '
|
|
41
|
+
+ 'If it is public API read through a component ref, mark it `/** @public */`.',
|
|
36
42
|
},
|
|
37
43
|
schema: [{
|
|
38
44
|
type: 'object',
|
|
@@ -104,6 +110,10 @@ export default {
|
|
|
104
110
|
|
|
105
111
|
const exportedNames = new Set();
|
|
106
112
|
|
|
113
|
+
const isPublic = (prop) => hasPublicTag(context.sourceCode, prop);
|
|
114
|
+
const isReadThroughType = makeTypeUsage(context.sourceCode.parserServices);
|
|
115
|
+
const options = { ignore, isPublic, isReadThroughType };
|
|
116
|
+
|
|
107
117
|
return {
|
|
108
118
|
Program(program) {
|
|
109
119
|
for (const node of program.body) {
|
|
@@ -121,7 +131,7 @@ export default {
|
|
|
121
131
|
enter(node);
|
|
122
132
|
// `=> ({ a, b })`
|
|
123
133
|
if (node.body.type === 'ObjectExpression' && isTarget(node, exportedNames)) {
|
|
124
|
-
check(context, node.body, texts,
|
|
134
|
+
check(context, node.body, texts, options);
|
|
125
135
|
}
|
|
126
136
|
},
|
|
127
137
|
'ArrowFunctionExpression:exit': leave,
|
|
@@ -130,7 +140,7 @@ export default {
|
|
|
130
140
|
if (node.argument?.type !== 'ObjectExpression') return;
|
|
131
141
|
const fn = stack[stack.length - 1];
|
|
132
142
|
if (!fn || !isTarget(fn, exportedNames)) return;
|
|
133
|
-
check(context, node.argument, texts,
|
|
143
|
+
check(context, node.argument, texts, options);
|
|
134
144
|
},
|
|
135
145
|
};
|
|
136
146
|
},
|
|
@@ -407,13 +417,117 @@ export const getKeyName = (prop) => {
|
|
|
407
417
|
return null;
|
|
408
418
|
};
|
|
409
419
|
|
|
410
|
-
const check = (context, objectNode, texts, ignore) => {
|
|
420
|
+
const check = (context, objectNode, texts, { ignore, isPublic, isReadThroughType }) => {
|
|
411
421
|
for (const prop of objectNode.properties) {
|
|
412
422
|
const name = getKeyName(prop);
|
|
413
|
-
if (!name || ignore.includes(name)) continue;
|
|
423
|
+
if (!name || ignore.includes(name) || isPublic(prop)) continue;
|
|
414
424
|
|
|
415
|
-
|
|
425
|
+
// by name first (cheap), then through the types (a component ref in another directory)
|
|
426
|
+
if (!isUsed(name, texts) && !isReadThroughType(objectNode, prop, name)) {
|
|
416
427
|
context.report({ node: prop, messageId: 'unused', data: { name } });
|
|
417
428
|
}
|
|
418
429
|
}
|
|
419
430
|
};
|
|
431
|
+
|
|
432
|
+
// ─── @public ────────────────────────────────────────────────────────────────
|
|
433
|
+
|
|
434
|
+
const PUBLIC_TAG = /(?:^|[\s*])@public(?=[\s*]|$)/;
|
|
435
|
+
|
|
436
|
+
/** A comment text marks a key as public */
|
|
437
|
+
export const isPublicComment = (text) => PUBLIC_TAG.test(text);
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* `/** @public *\/ focus,` (comment before the key) or `focus, // @public` (comment after it,
|
|
441
|
+
* on the same line, the comma between)
|
|
442
|
+
*/
|
|
443
|
+
const hasPublicTag = (sourceCode, prop) => {
|
|
444
|
+
// `a, // @public` then `b` on the next line: the comment belongs to `a`, not to `b`
|
|
445
|
+
const before = sourceCode.getTokenBefore(prop);
|
|
446
|
+
const trailsPrevious = (c) => before?.value === ','
|
|
447
|
+
&& c.loc.start.line === before.loc.end.line
|
|
448
|
+
&& c.loc.end.line < prop.loc.start.line;
|
|
449
|
+
|
|
450
|
+
if (sourceCode.getCommentsBefore(prop).some((c) => !trailsPrevious(c) && isPublicComment(c.value))) return true;
|
|
451
|
+
|
|
452
|
+
let token = sourceCode.getTokenAfter(prop, { includeComments: true });
|
|
453
|
+
if (token?.type === 'Punctuator' && token.value === ',') {
|
|
454
|
+
token = sourceCode.getTokenAfter(token, { includeComments: true });
|
|
455
|
+
}
|
|
456
|
+
if (!token || (token.type !== 'Line' && token.type !== 'Block')) return false;
|
|
457
|
+
if (token.loc.start.line !== prop.loc.end.line || !isPublicComment(token.value)) return false;
|
|
458
|
+
|
|
459
|
+
// the comment ends the line, else it leads the next key: `h, /** @public */ i,`
|
|
460
|
+
const next = sourceCode.getTokenAfter(token, { includeComments: true });
|
|
461
|
+
return !next || next.loc.start.line > token.loc.end.line;
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
// ─── read through the types ─────────────────────────────────────────────────
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* `(objectNode, prop, name) => boolean`: an identifier of the program resolves to the returned
|
|
468
|
+
* property, e.g. `cmp.value?.focus()` with `cmp = ref<Cmp>()` and `type Cmp = Context<typeof setup>`
|
|
469
|
+
* (a mapped type keeps the declaration of the original property). Without type information, `false`.
|
|
470
|
+
*/
|
|
471
|
+
const makeTypeUsage = (services) => {
|
|
472
|
+
if (!services?.program || !services.esTreeNodeToTSNodeMap) return () => false;
|
|
473
|
+
|
|
474
|
+
const { program } = services;
|
|
475
|
+
const checker = program.getTypeChecker();
|
|
476
|
+
|
|
477
|
+
return (objectNode, prop, name) => {
|
|
478
|
+
const tsObject = services.esTreeNodeToTSNodeMap.get(objectNode);
|
|
479
|
+
const tsProp = services.esTreeNodeToTSNodeMap.get(prop);
|
|
480
|
+
if (!tsObject || !tsProp) return false;
|
|
481
|
+
|
|
482
|
+
const symbol = checker.getPropertyOfType(checker.getTypeAtLocation(tsObject), name);
|
|
483
|
+
if (!symbol) return false;
|
|
484
|
+
|
|
485
|
+
for (const id of findIdentifiers(program, name)) {
|
|
486
|
+
// the key itself
|
|
487
|
+
if (id.parent === tsProp) continue;
|
|
488
|
+
|
|
489
|
+
const used = checker.getSymbolAtLocation(id);
|
|
490
|
+
if (used && isSameProperty(checker, used, symbol, tsProp)) return true;
|
|
491
|
+
}
|
|
492
|
+
return false;
|
|
493
|
+
};
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
/** `used` is `target`, or a symbol derived from it (mapped / union / intersection type) */
|
|
497
|
+
const isSameProperty = (checker, used, target, tsProp) => {
|
|
498
|
+
if (used === target) return true;
|
|
499
|
+
const roots = [used, ...(checker.getRootSymbols?.(used) ?? [])];
|
|
500
|
+
return roots.some((s) => s === target || s.declarations?.includes(tsProp));
|
|
501
|
+
};
|
|
502
|
+
|
|
503
|
+
const identifiersCache = new WeakMap(); // program → Map<name, identifiers>
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* Identifiers named `name` in the source files of the program (not `.d.ts`, not node_modules).
|
|
507
|
+
* Matched on `escapedText`, not on `SyntaxKind`: the rule doesn't import `typescript`, and
|
|
508
|
+
* the enum values change between TS versions.
|
|
509
|
+
*/
|
|
510
|
+
const findIdentifiers = (program, name) => {
|
|
511
|
+
let byName = identifiersCache.get(program);
|
|
512
|
+
if (!byName) {
|
|
513
|
+
byName = new Map();
|
|
514
|
+
identifiersCache.set(program, byName);
|
|
515
|
+
}
|
|
516
|
+
if (byName.has(name)) return byName.get(name);
|
|
517
|
+
|
|
518
|
+
const result = [];
|
|
519
|
+
const visit = (node) => {
|
|
520
|
+
if (node.escapedText === name) result.push(node);
|
|
521
|
+
node.forEachChild(visit);
|
|
522
|
+
};
|
|
523
|
+
|
|
524
|
+
for (const sf of program.getSourceFiles()) {
|
|
525
|
+
if (sf.isDeclarationFile || sf.fileName.includes('/node_modules/')) continue;
|
|
526
|
+
// cheap filter before walking the tree
|
|
527
|
+
if (!sf.text.includes(name)) continue;
|
|
528
|
+
visit(sf);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
byName.set(name, result);
|
|
532
|
+
return result;
|
|
533
|
+
};
|
package/src/plugin.js
CHANGED
|
@@ -4,6 +4,7 @@ import exportSpecifiersPerLineRule from './export-specifiers-per-line.js';
|
|
|
4
4
|
import jsxBooleanShorthand from './jsx-boolean-shorthand.js'
|
|
5
5
|
import jsxExpressionStringLiterals from './jsx-expression-string-literals.js';
|
|
6
6
|
import jsxRefStringExists from './jsx-ref-string-exists.js';
|
|
7
|
+
import jsxRefTypeMatch from './jsx-ref-type-match.js';
|
|
7
8
|
import enforceTailwindImportant from './enforce-tailwind-important.js';
|
|
8
9
|
import vModelNameMatch from './v-model-name-match.js';
|
|
9
10
|
import decoratorTypeEnforcement from './decorator-type-enforcement.js'
|
|
@@ -23,6 +24,7 @@ export default {
|
|
|
23
24
|
'jsx-boolean-shorthand': jsxBooleanShorthand,
|
|
24
25
|
'jsx-expression-string-literals': jsxExpressionStringLiterals,
|
|
25
26
|
'jsx-ref-string-exists': jsxRefStringExists,
|
|
27
|
+
'jsx-ref-type-match': jsxRefTypeMatch,
|
|
26
28
|
'enforce-tailwind-important': enforceTailwindImportant,
|
|
27
29
|
'v-model-name-match': vModelNameMatch,
|
|
28
30
|
'decorator-type-enforcement': decoratorTypeEnforcement,
|