Skip to content

Commit fb9edd2

Browse files
authored
refactor(language-core, typescript-plugin): use inline __VLS_elements to simplify code (#5255)
1 parent a8ec01f commit fb9edd2

File tree

7 files changed

+34
-46
lines changed

7 files changed

+34
-46
lines changed

packages/language-core/lib/codegen/globalTypes.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export function generateGlobalTypes({
3535
}
3636
text += `
3737
; declare global {
38-
const __VLS_intrinsicElements: __VLS_IntrinsicElements;
3938
const __VLS_directiveBindingRestFields: { instance: null, oldValue: null, modifiers: any, dir: any };
4039
const __VLS_unref: typeof import('${lib}').unref;
4140
const __VLS_placeholder: any;

packages/language-core/lib/codegen/script/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ export function* generateScript(options: ScriptCodegenOptions): Generator<Code,
127127
yield* generateComponentSelf(options, ctx, templateCodegenCtx);
128128
}
129129

130-
yield `type __VLS_IntrinsicElementsCompletion = __VLS_IntrinsicElements${endOfLine}`;
131130
yield* ctx.localTypes.generate([...ctx.localTypes.getUsedNames()]);
132131
if (options.appendGlobalTypes) {
133132
yield generateGlobalTypes(options.vueCompilerOptions);

packages/language-core/lib/codegen/script/template.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export function* generateTemplate(
2020
scriptSetupBindingNames: new Set(),
2121
});
2222
yield* generateTemplateCtx(options);
23+
yield* generateTemplateElements();
2324
yield* generateTemplateComponents(options);
2425
yield* generateTemplateDirectives(options);
2526
yield* generateTemplateBody(options, templateCodegenCtx);
@@ -54,6 +55,10 @@ function* generateTemplateCtx(options: ScriptCodegenOptions): Generator<Code> {
5455
}
5556
}
5657

58+
function* generateTemplateElements(): Generator<Code> {
59+
yield `let __VLS_elements!: __VLS_IntrinsicElements${endOfLine}`;
60+
}
61+
5762
function* generateTemplateComponents(options: ScriptCodegenOptions): Generator<Code> {
5863
const types: Code[] = [];
5964

packages/language-core/lib/codegen/template/element.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ export function* generateElement(
320320
: undefined;
321321
const failedPropExps: FailedPropExpression[] = [];
322322

323-
yield `__VLS_asFunctionalElement(__VLS_intrinsicElements`;
323+
yield `__VLS_asFunctionalElement(__VLS_elements`;
324324
yield* generatePropertyAccess(
325325
options,
326326
ctx,
@@ -329,7 +329,7 @@ export function* generateElement(
329329
ctx.codeFeatures.withoutHighlightAndCompletion
330330
);
331331
if (endTagOffset !== undefined) {
332-
yield `, __VLS_intrinsicElements`;
332+
yield `, __VLS_elements`;
333333
yield* generatePropertyAccess(
334334
options,
335335
ctx,
@@ -376,7 +376,7 @@ export function* generateElement(
376376
}
377377

378378
if (hasVBindAttrs(options, ctx, node)) {
379-
ctx.inheritedAttrVars.add(`__VLS_intrinsicElements.${node.tag}`);
379+
ctx.inheritedAttrVars.add(`__VLS_elements.${node.tag}`);
380380
}
381381

382382
collectStyleScopedClassReferences(options, ctx, node);

packages/typescript-plugin/lib/common.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { forEachElementNode, hyphenateTag, Language, VueCompilerOptions, VueVirtualCode } from '@vue/language-core';
22
import { capitalize } from '@vue/shared';
33
import type * as ts from 'typescript';
4-
import { _getComponentNames } from './requests/getComponentNames';
5-
import { _getElementNames } from './requests/getElementAttrs';
4+
import { _getComponentNames, _getElementNames } from './requests/getComponentNames';
65
import type { RequestContext } from './requests/types';
76

87
const windowsPathReg = /\\/g;

packages/typescript-plugin/lib/requests/getComponentNames.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,15 @@ export function _getComponentNames(
3131
names.push(getSelfComponentName(vueCode.fileName));
3232
return names;
3333
}
34+
35+
export function _getElementNames(
36+
ts: typeof import('typescript'),
37+
tsLs: ts.LanguageService,
38+
vueCode: VueVirtualCode
39+
) {
40+
return getVariableType(ts, tsLs, vueCode, '__VLS_elements')
41+
?.type
42+
?.getProperties()
43+
.map(c => c.name)
44+
?? [];
45+
}
Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { VueVirtualCode } from '@vue/language-core';
2-
import type * as ts from 'typescript';
32
import type { RequestContext } from './types';
3+
import { getVariableType } from './utils';
44

55
export function getElementAttrs(
66
this: RequestContext,
@@ -12,46 +12,20 @@ export function getElementAttrs(
1212
if (!(volarFile?.generated?.root instanceof VueVirtualCode)) {
1313
return;
1414
}
15-
const program = languageService.getProgram()!;
16-
17-
const tsSourceFile = program.getSourceFile(fileName);
18-
if (tsSourceFile) {
19-
const checker = program.getTypeChecker();
20-
const typeNode = tsSourceFile.statements
21-
.filter(ts.isTypeAliasDeclaration)
22-
.find(node => node.name.getText() === '__VLS_IntrinsicElementsCompletion');
23-
24-
if (typeNode) {
25-
const type = checker.getTypeFromTypeNode(typeNode.type);
26-
const el = type.getProperty(tagName);
15+
const vueCode = volarFile.generated.root;
2716

28-
if (el) {
29-
const attrs = checker.getTypeOfSymbolAtLocation(el, typeNode).getProperties();
30-
return attrs.map(c => c.name);
31-
}
32-
}
17+
const program = languageService.getProgram()!;
18+
const checker = program.getTypeChecker();
19+
const elements = getVariableType(ts, languageService, vueCode, '__VLS_elements');
20+
if (!elements) {
21+
return [];
3322
}
34-
return [];
35-
}
3623

37-
export function _getElementNames(
38-
ts: typeof import('typescript'),
39-
tsLs: ts.LanguageService,
40-
vueCode: VueVirtualCode
41-
) {
42-
const program = tsLs.getProgram()!;
43-
44-
const tsSourceFile = program.getSourceFile(vueCode.fileName);
45-
if (tsSourceFile) {
46-
const checker = program.getTypeChecker();
47-
const typeNode = tsSourceFile.statements
48-
.filter(ts.isTypeAliasDeclaration)
49-
.find(node => node.name.getText() === '__VLS_IntrinsicElementsCompletion');
50-
51-
if (typeNode) {
52-
const type = checker.getTypeFromTypeNode(typeNode.type);
53-
return type.getProperties().map(c => c.name);
54-
}
24+
const elementType = elements.type.getProperty(tagName);
25+
if (!elementType) {
26+
return [];
5527
}
56-
return [];
28+
29+
const attrs = checker.getTypeOfSymbol(elementType).getProperties();
30+
return attrs.map(c => c.name);
5731
}

0 commit comments

Comments
 (0)