Skip to content

Commit f4a9c50

Browse files
refactor(language-core): less codegen options (#5804)
1 parent 748a658 commit f4a9c50

File tree

13 files changed

+106
-123
lines changed

13 files changed

+106
-123
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const ModelEmit = '__VLS_ModelEmit';
2525
export const EmitProps = '__VLS_EmitProps';
2626
export const InternalProps = '__VLS_InternalProps';
2727
export const Emit = '__VLS_Emit';
28-
export const Bindings = '__VLS_Bindings';
28+
export const SetupExposed = '__VLS_SetupExposed';
2929
export const PublicProps = '__VLS_PublicProps';
3030
export const StyleModules = '__VLS_StyleModules';
3131

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@ export interface ScriptCodegenOptions {
2323
fileName: string;
2424
scriptRanges: ScriptRanges | undefined;
2525
scriptSetupRanges: ScriptSetupRanges | undefined;
26-
templateComponents: string[];
2726
templateStartTagOffset: number | undefined;
2827
templateCodegen: TemplateCodegenContext & { codes: Code[] } | undefined;
2928
styleCodegen: TemplateCodegenContext & { codes: Code[] } | undefined;
30-
setupBindingNames: Set<string>;
29+
setupExposed: Set<string>;
3130
}
3231

3332
export { generate as generateScript };

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ function* generateMacros(options: ScriptCodegenOptions): Generator<Code> {
303303
yield `// @ts-ignore${newLine}`;
304304
yield `declare const { `;
305305
for (const macro of Object.keys(options.vueCompilerOptions.macros)) {
306-
if (!options.setupBindingNames.has(macro)) {
306+
if (!options.setupExposed.has(macro)) {
307307
yield `${macro}, `;
308308
}
309309
}

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

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { camelize, capitalize } from '@vue/shared';
21
import * as path from 'path-browserify';
32
import type { Code } from '../../types';
43
import { codeFeatures } from '../codeFeatures';
@@ -13,7 +12,7 @@ export function* generateTemplate(
1312
ctx: ScriptCodegenContext,
1413
): Generator<Code> {
1514
yield* generateSelf(options);
16-
yield* generateBindings(options, ctx);
15+
yield* generateSetupExposed(options, ctx);
1716
yield* generateTemplateCtx(options, ctx);
1817
yield* generateTemplateComponents(options);
1918
yield* generateTemplateDirectives(options);
@@ -92,8 +91,8 @@ function* generateTemplateCtx(
9291
exps.push([`{} as ${names.InternalProps}`]);
9392
}
9493

95-
if (ctx.generatedTypes.has(names.Bindings)) {
96-
exps.push([`{} as ${names.Bindings}`]);
94+
if (ctx.generatedTypes.has(names.SetupExposed)) {
95+
exps.push([`{} as ${names.SetupExposed}`]);
9796
}
9897

9998
yield `const ${names.ctx} = `;
@@ -141,23 +140,17 @@ function* generateTemplateDirectives(options: ScriptCodegenOptions): Generator<C
141140
yield `let ${names.directives}!: __VLS_LocalDirectives & __VLS_GlobalDirectives${endOfLine}`;
142141
}
143142

144-
function* generateBindings(
145-
{ templateComponents, templateCodegen, styleCodegen, setupBindingNames }: ScriptCodegenOptions,
143+
function* generateSetupExposed(
144+
{ setupExposed }: ScriptCodegenOptions,
146145
ctx: ScriptCodegenContext,
147146
): Generator<Code> {
148-
const testNames = new Set([
149-
...templateComponents.flatMap(c => [camelize(c), capitalize(camelize(c))]),
150-
...templateCodegen?.accessExternalVariables.keys() ?? [],
151-
...styleCodegen?.accessExternalVariables.keys() ?? [],
152-
]);
153-
const exposeNames = [...setupBindingNames].filter(name => testNames.has(name));
154-
if (exposeNames.length === 0) {
147+
if (!setupExposed.size) {
155148
return;
156149
}
157-
ctx.generatedTypes.add(names.Bindings);
150+
ctx.generatedTypes.add(names.SetupExposed);
158151

159-
yield `type ${names.Bindings} = __VLS_ProxyRefs<{${newLine}`;
160-
for (const bindingName of exposeNames) {
152+
yield `type ${names.SetupExposed} = __VLS_ProxyRefs<{${newLine}`;
153+
for (const bindingName of setupExposed) {
161154
const token = Symbol(bindingName.length);
162155
yield ['', undefined, 0, { __linkedToken: token }];
163156
yield `${bindingName}: typeof `;

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ export interface StyleCodegenOptions {
1010
ts: typeof import('typescript');
1111
vueCompilerOptions: VueCompilerOptions;
1212
styles: Sfc['styles'];
13-
templateRefNames: Set<string>;
14-
directAccessNames: Set<string>;
15-
setupBindingNames: Set<string>;
13+
setupRefs: Set<string>;
14+
setupConsts: Set<string>;
1615
}
1716

1817
export { generate as generateStyle };
1918

2019
function generate(options: StyleCodegenOptions) {
21-
const ctx = createTemplateCodegenContext(options.setupBindingNames);
20+
const ctx = createTemplateCodegenContext();
2221
const codeGenerator = generateWorker(options, ctx);
2322
const codes: Code[] = [];
2423
for (const code of codeGenerator) {
@@ -35,7 +34,7 @@ function* generateWorker(
3534
ctx: TemplateCodegenContext,
3635
) {
3736
const endScope = ctx.startScope();
38-
ctx.declare(...options.directAccessNames);
37+
ctx.declare(...options.setupConsts);
3938
yield* generateStyleScopedClasses(options);
4039
yield* generateStyleModules(options, ctx);
4140
yield* generateCssVars(options, ctx);

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

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,13 @@ const commentDirectiveRegex = /^<!--\s*@vue-(?<name>[-\w]+)\b(?<content>[\s\S]*)
106106
* an error/diagnostic was encountered for a region of code covered by a `@vue-expect-error` directive,
107107
* and additionally how we use that to determine whether to propagate diagnostics back upward.
108108
*/
109-
export function createTemplateCodegenContext(
110-
setupBindingNames: Set<string>,
111-
) {
109+
export function createTemplateCodegenContext() {
112110
let variableId = 0;
113111

114112
const scopes: Set<string>[] = [];
115113
const hoistVars = new Map<string, string>();
116114
const dollarVars = new Set<string>();
117-
const accessExternalVariables = new Map<string, Map<string, Set<number>>>();
115+
const componentAccessMap = new Map<string, Map<string, Set<number>>>();
118116
const slots: {
119117
name: string;
120118
offset?: number;
@@ -157,7 +155,7 @@ export function createTemplateCodegenContext(
157155
slots,
158156
dynamicSlots,
159157
dollarVars,
160-
accessExternalVariables,
158+
componentAccessMap,
161159
blockConditions,
162160
inlayHints,
163161
inheritedAttrVars,
@@ -175,10 +173,10 @@ export function createTemplateCodegenContext(
175173
}
176174
refs.push({ typeExp, offset });
177175
},
178-
accessExternalVariable(source: string, name: string, offset?: number) {
179-
let map = accessExternalVariables.get(name);
176+
recordComponentAccess(source: string, name: string, offset?: number) {
177+
let map = componentAccessMap.get(name);
180178
if (!map) {
181-
accessExternalVariables.set(name, map = new Map());
179+
componentAccessMap.set(name, map = new Map());
182180
}
183181
let arr = map.get(source);
184182
if (!arr) {
@@ -298,7 +296,7 @@ export function createTemplateCodegenContext(
298296
};
299297

300298
function* generateAutoImport(): Generator<Code> {
301-
const all = [...accessExternalVariables.entries()];
299+
const all = [...componentAccessMap.entries()];
302300
if (!all.some(([, offsets]) => offsets.size)) {
303301
return;
304302
}
@@ -307,26 +305,7 @@ export function createTemplateCodegenContext(
307305
for (const [varName, map] of all) {
308306
for (const [source, offsets] of map) {
309307
for (const offset of offsets) {
310-
if (setupBindingNames.has(varName)) {
311-
// #3409
312-
yield [
313-
varName,
314-
source,
315-
offset,
316-
{
317-
...codeFeatures.importCompletionOnly,
318-
...codeFeatures.semanticWithoutHighlight,
319-
},
320-
];
321-
}
322-
else {
323-
yield [
324-
varName,
325-
source,
326-
offset,
327-
codeFeatures.importCompletionOnly,
328-
];
329-
}
308+
yield [varName, source, offset, codeFeatures.importCompletionOnly];
330309
yield `,`;
331310
}
332311
offsets.clear();

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ export function* generateComponent(
3131
const tagOffsets = getElementTagOffsets(node, options.template);
3232
const failGeneratedExpressions: FailGeneratedExpression[] = [];
3333
const possibleOriginalNames = getPossibleOriginalComponentNames(node.tag, true);
34-
const matchImportName = possibleOriginalNames.find(name => options.directAccessNames.has(name));
35-
const componentOriginalVar = matchImportName ?? ctx.getInternalVariable();
34+
const matchConst = possibleOriginalNames.find(name => options.setupConsts.has(name));
35+
const componentOriginalVar = matchConst ?? ctx.getInternalVariable();
3636
const componentFunctionalVar = ctx.getInternalVariable();
3737
const componentVNodeVar = ctx.getInternalVariable();
3838
const componentCtxVar = ctx.getInternalVariable();
@@ -86,7 +86,7 @@ export function* generateComponent(
8686
};
8787
}
8888

89-
if (matchImportName) {
89+
if (matchConst) {
9090
// navigation support
9191
yield `/** @type {[`;
9292
for (const tagOffset of tagOffsets) {
@@ -100,7 +100,7 @@ export function* generateComponent(
100100
];
101101
}
102102
else {
103-
const shouldCapitalize = matchImportName[0]!.toUpperCase() === matchImportName[0];
103+
const shouldCapitalize = matchConst[0]!.toUpperCase() === matchConst[0];
104104
yield* generateCamelized(
105105
shouldCapitalize ? capitalize(node.tag) : node.tag,
106106
'template',

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,9 @@ export function* generateElementDirectives(
3737
) {
3838
continue;
3939
}
40-
41-
if (!builtInDirectives.has(prop.name)) {
42-
ctx.accessExternalVariable('template', camelize('v-' + prop.name), prop.loc.start.offset);
43-
}
44-
4540
const token = yield* startBoundary('template', prop.loc.start.offset, codeFeatures.verification);
4641
yield `__VLS_asFunctionalDirective(`;
47-
yield* generateIdentifier(options, prop);
42+
yield* generateIdentifier(options, ctx, prop);
4843
yield `)(null!, { ...__VLS_directiveBindingRestFields, `;
4944
yield* generateArg(options, ctx, prop);
5045
yield* generateModifiers(options, ctx, prop);
@@ -57,6 +52,7 @@ export function* generateElementDirectives(
5752

5853
function* generateIdentifier(
5954
options: TemplateCodegenOptions,
55+
ctx: TemplateCodegenContext,
6056
prop: CompilerDOM.DirectiveNode,
6157
): Generator<Code> {
6258
const rawName = 'v-' + prop.name;
@@ -73,6 +69,9 @@ function* generateIdentifier(
7369
verification: options.vueCompilerOptions.checkUnknownDirectives && !builtInDirectives.has(prop.name),
7470
},
7571
);
72+
if (!builtInDirectives.has(prop.name)) {
73+
ctx.recordComponentAccess('template', camelize(rawName), prop.loc.start.offset);
74+
}
7675
yield endBoundary(token, startOffset + rawName.length);
7776
}
7877

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,12 @@ export function* generatePropExp(
258258
if (ctx.scopes.some(scope => scope.has(propVariableName))) {
259259
yield* codes;
260260
}
261-
else if (options.templateRefNames.has(propVariableName)) {
261+
else if (options.setupRefs.has(propVariableName)) {
262262
yield* codes;
263263
yield `.value`;
264264
}
265265
else {
266-
ctx.accessExternalVariable('template', propVariableName, exp.loc.start.offset);
266+
ctx.recordComponentAccess('template', propVariableName, exp.loc.start.offset);
267267
yield names.ctx;
268268
yield `.`;
269269
yield* codes;

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ export interface TemplateCodegenOptions {
1313
compilerOptions: ts.CompilerOptions;
1414
vueCompilerOptions: VueCompilerOptions;
1515
template: NonNullable<Sfc['template']>;
16-
directAccessNames: Set<string>;
17-
setupBindingNames: Set<string>;
18-
templateRefNames: Set<string>;
16+
setupRefs: Set<string>;
17+
setupConsts: Set<string>;
1918
hasDefineSlots?: boolean;
2019
propsAssignName?: string;
2120
slotsAssignName?: string;
@@ -26,7 +25,7 @@ export interface TemplateCodegenOptions {
2625
export { generate as generateTemplate };
2726

2827
function generate(options: TemplateCodegenOptions) {
29-
const ctx = createTemplateCodegenContext(options.setupBindingNames);
28+
const ctx = createTemplateCodegenContext();
3029
const codeGenerator = generateWorker(options, ctx);
3130
const codes: Code[] = [];
3231
for (const code of codeGenerator) {
@@ -43,7 +42,7 @@ function* generateWorker(
4342
ctx: TemplateCodegenContext,
4443
): Generator<Code> {
4544
const endScope = ctx.startScope();
46-
ctx.declare(...options.directAccessNames);
45+
ctx.declare(...options.setupConsts);
4746
const {
4847
slotsAssignName,
4948
propsAssignName,

0 commit comments

Comments
 (0)