Skip to content

Commit a364a28

Browse files
committed
wip
1 parent fe372e8 commit a364a28

File tree

12 files changed

+67
-62
lines changed

12 files changed

+67
-62
lines changed

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@ export function createScriptCodegenContext(options: ScriptCodegenOptions) {
1010

1111
return {
1212
generatedTypes: new Set<string>(),
13-
bindingNames: new Set([
14-
...options.scriptRanges?.bindings.map(
15-
({ range }) => options.script!.content.slice(range.start, range.end),
16-
) ?? [],
17-
...options.scriptSetupRanges?.bindings.map(
18-
({ range }) => options.scriptSetup!.content.slice(range.start, range.end),
19-
) ?? [],
20-
]),
2113
localTypes,
2214
inlayHints,
2315
};

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export interface ScriptCodegenOptions {
2828
templateStartTagOffset: number | undefined;
2929
templateCodegen: TemplateCodegenContext & { codes: Code[] } | undefined;
3030
styleCodegen: TemplateCodegenContext & { codes: Code[] } | undefined;
31+
scriptBindings: Set<string>;
3132
}
3233

3334
export { generate as generateScript };

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ export function* generateSetupFunction(
282282
(start, end) =>
283283
generateSfcBlockSection(scriptSetup, start, end, codeFeatures.all, end === scriptSetup.content.length),
284284
);
285-
yield* generateMacros(options, ctx);
285+
yield* generateMacros(options);
286286
yield* generateModels(scriptSetup, scriptSetupRanges);
287287
yield* generatePublicProps(options, ctx, scriptSetup, scriptSetupRanges);
288288
yield* body;
@@ -303,15 +303,12 @@ export function* generateSetupFunction(
303303
}
304304
}
305305

306-
function* generateMacros(
307-
options: ScriptCodegenOptions,
308-
ctx: ScriptCodegenContext,
309-
): Generator<Code> {
306+
function* generateMacros(options: ScriptCodegenOptions): Generator<Code> {
310307
if (options.vueCompilerOptions.target >= 3.3) {
311308
yield `// @ts-ignore${newLine}`;
312309
yield `declare const { `;
313310
for (const macro of Object.keys(options.vueCompilerOptions.macros)) {
314-
if (!ctx.bindingNames.has(macro)) {
311+
if (!options.scriptBindings.has(macro)) {
315312
yield `${macro}, `;
316313
}
317314
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ function* generateTemplateDirectives(options: ScriptCodegenOptions): Generator<C
150150
}
151151

152152
function* generateBindings(
153-
{ scriptSetup, templateComponents, templateCodegen, styleCodegen }: ScriptCodegenOptions,
153+
{ templateComponents, templateCodegen, styleCodegen, scriptBindings }: ScriptCodegenOptions,
154154
ctx: ScriptCodegenContext,
155155
): Generator<Code> {
156-
if (!(scriptSetup && ctx.bindingNames.size)) {
156+
if (!scriptBindings.size) {
157157
return;
158158
}
159159
ctx.generatedTypes.add(names.Bindings);
@@ -165,7 +165,7 @@ function* generateBindings(
165165
]);
166166

167167
yield `type ${names.Bindings} = __VLS_ProxyRefs<{${newLine}`;
168-
for (const varName of ctx.bindingNames) {
168+
for (const varName of scriptBindings) {
169169
if (!usedVars.has(varName)) {
170170
continue;
171171
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@ export interface StyleCodegenOptions {
1313
styles: Sfc['styles'];
1414
destructuredPropNames: Set<string>;
1515
templateRefNames: Set<string>;
16+
scriptBindings: Set<string>;
1617
}
1718

1819
export { generate as generateStyle };
1920

2021
function* generate(options: StyleCodegenOptions) {
21-
const ctx = createTemplateCodegenContext({
22-
scriptSetupBindingNames: new Set(),
23-
});
22+
const ctx = createTemplateCodegenContext(options.scriptBindings);
23+
const endScope = ctx.startScope();
2424
yield* generateStyleScopedClasses(options);
2525
yield* generateStyleModules(options, ctx);
2626
yield* generateCssVars(options, ctx);
27+
yield* endScope();
2728
return ctx;
2829
}
2930

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

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { codeFeatures } from '../codeFeatures';
44
import type { InlayHintInfo } from '../inlayHints';
55
import { endOfLine, newLine } from '../utils';
66
import { endBoundary, startBoundary } from '../utils/boundary';
7-
import type { TemplateCodegenOptions } from './index';
87

98
export type TemplateCodegenContext = ReturnType<typeof createTemplateCodegenContext>;
109

@@ -108,14 +107,14 @@ const commentDirectiveRegex = /^<!--\s*@vue-(?<name>[-\w]+)\b(?<content>[\s\S]*)
108107
* and additionally how we use that to determine whether to propagate diagnostics back upward.
109108
*/
110109
export function createTemplateCodegenContext(
111-
options: Pick<TemplateCodegenOptions, 'scriptSetupBindingNames'>,
110+
scriptBindings: Set<string>,
112111
) {
113112
let variableId = 0;
114113

115-
const scopes = [new Set<string>()];
114+
const scopes: Set<string>[] = [];
116115
const hoistVars = new Map<string, string>();
117116
const dollarVars = new Set<string>();
118-
const accessExternalVariables = new Map<string, Set<number>>();
117+
const accessExternalVariables = new Map<string, Map<string, Set<number>>>();
119118
const slots: {
120119
name: string;
121120
offset?: number;
@@ -176,10 +175,14 @@ export function createTemplateCodegenContext(
176175
}
177176
refs.push({ typeExp, offset });
178177
},
179-
accessExternalVariable(name: string, offset?: number) {
180-
let arr = accessExternalVariables.get(name);
178+
accessExternalVariable(source: string, name: string, offset?: number) {
179+
let map = accessExternalVariables.get(name);
180+
if (!map) {
181+
accessExternalVariables.set(name, map = new Map());
182+
}
183+
let arr = map.get(source);
181184
if (!arr) {
182-
accessExternalVariables.set(name, arr = new Set());
185+
map.set(source, arr = new Set());
183186
}
184187
if (offset !== undefined) {
185188
arr.add(offset);
@@ -301,31 +304,33 @@ export function createTemplateCodegenContext(
301304
}
302305
yield `// @ts-ignore${newLine}`; // #2304
303306
yield `[`;
304-
for (const [varName, offsets] of all) {
305-
for (const offset of offsets) {
306-
if (options.scriptSetupBindingNames.has(varName)) {
307-
// #3409
308-
yield [
309-
varName,
310-
'template',
311-
offset,
312-
{
313-
...codeFeatures.additionalCompletion,
314-
...codeFeatures.semanticWithoutHighlight,
315-
},
316-
];
317-
}
318-
else {
319-
yield [
320-
varName,
321-
'template',
322-
offset,
323-
codeFeatures.additionalCompletion,
324-
];
307+
for (const [varName, map] of all) {
308+
for (const [source, offsets] of map) {
309+
for (const offset of offsets) {
310+
if (scriptBindings.has(varName)) {
311+
// #3409
312+
yield [
313+
varName,
314+
source,
315+
offset,
316+
{
317+
...codeFeatures.additionalCompletion,
318+
...codeFeatures.semanticWithoutHighlight,
319+
},
320+
];
321+
}
322+
else {
323+
yield [
324+
varName,
325+
source,
326+
offset,
327+
codeFeatures.additionalCompletion,
328+
];
329+
}
330+
yield `,`;
325331
}
326-
yield `,`;
332+
offsets.clear();
327333
}
328-
offsets.clear();
329334
}
330335
yield `]${endOfLine}`;
331336
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ function* generateElementReference(
565565
yield `} */${endOfLine}`;
566566

567567
if (identifierRegex.test(name) && !options.templateRefNames.has(name)) {
568-
ctx.accessExternalVariable(name, offset);
568+
ctx.accessExternalVariable('template', name, offset);
569569
}
570570

571571
return { name, offset };

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function* generateElementDirectives(
3939
}
4040

4141
if (!builtInDirectives.has(prop.name)) {
42-
ctx.accessExternalVariable(camelize('v-' + prop.name), prop.loc.start.offset);
42+
ctx.accessExternalVariable('template', camelize('v-' + prop.name), prop.loc.start.offset);
4343
}
4444

4545
const token = yield* startBoundary('template', prop.loc.start.offset, codeFeatures.verification);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ export function* generatePropExp(
266266
yield `)`;
267267
}
268268
else {
269-
ctx.accessExternalVariable(propVariableName, exp.loc.start.offset);
269+
ctx.accessExternalVariable('template', propVariableName, exp.loc.start.offset);
270270
yield names.ctx;
271271
yield `.`;
272272
yield* codes;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface TemplateCodegenOptions {
1313
compilerOptions: ts.CompilerOptions;
1414
vueCompilerOptions: VueCompilerOptions;
1515
template: NonNullable<Sfc['template']>;
16-
scriptSetupBindingNames: Set<string>;
16+
scriptBindings: Set<string>;
1717
scriptSetupImportComponentNames: Set<string>;
1818
destructuredPropNames: Set<string>;
1919
templateRefNames: Set<string>;
@@ -27,7 +27,7 @@ export interface TemplateCodegenOptions {
2727
export { generate as generateTemplate };
2828

2929
function generate(options: TemplateCodegenOptions) {
30-
const ctx = createTemplateCodegenContext(options);
30+
const ctx = createTemplateCodegenContext(options.scriptBindings);
3131
const codegen = generateWorker(options, ctx);
3232
const codes: Code[] = [];
3333

0 commit comments

Comments
 (0)