@tcy/eslint-rules 1.12.0 → 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 +339 -120
- 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 +34 -19
- 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 +725 -50
- 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 +761 -267
- 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 +9 -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 +235 -10
- package/src/plugin.js +2 -0
- package/tests/jsx-ref-type-match.spec.js +118 -0
- package/tests/no-unused-setup-return.spec.js +65 -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;
|
|
@@ -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,8 +62,12 @@ 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 */
|
|
63
70
|
export declare const isComponentFileName: (filename: any) => boolean;
|
|
64
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
|
+
}
|