Skip to content

Commit 069a40a

Browse files
committed
wip
1 parent 54b0dec commit 069a40a

File tree

9 files changed

+36
-47
lines changed

9 files changed

+36
-47
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +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>;
31+
setupBindingNames: Set<string>;
3232
}
3333

3434
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
@@ -308,7 +308,7 @@ function* generateMacros(options: ScriptCodegenOptions): Generator<Code> {
308308
yield `// @ts-ignore${newLine}`;
309309
yield `declare const { `;
310310
for (const macro of Object.keys(options.vueCompilerOptions.macros)) {
311-
if (!options.scriptBindings.has(macro)) {
311+
if (!options.setupBindingNames.has(macro)) {
312312
yield `${macro}, `;
313313
}
314314
}

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

Lines changed: 7 additions & 7 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-
{ templateComponents, templateCodegen, styleCodegen, scriptBindings }: ScriptCodegenOptions,
153+
{ templateComponents, templateCodegen, styleCodegen, setupBindingNames }: ScriptCodegenOptions,
154154
ctx: ScriptCodegenContext,
155155
): Generator<Code> {
156-
if (!scriptBindings.size) {
156+
if (!setupBindingNames.size) {
157157
return;
158158
}
159159
ctx.generatedTypes.add(names.Bindings);
@@ -165,15 +165,15 @@ function* generateBindings(
165165
]);
166166

167167
yield `type ${names.Bindings} = __VLS_ProxyRefs<{${newLine}`;
168-
for (const varName of scriptBindings) {
169-
if (!usedVars.has(varName)) {
168+
for (const bindingName of setupBindingNames) {
169+
if (!usedVars.has(bindingName)) {
170170
continue;
171171
}
172-
const token = Symbol(varName.length);
172+
const token = Symbol(bindingName.length);
173173
yield ['', undefined, 0, { __linkedToken: token }];
174-
yield `${varName}: typeof `;
174+
yield `${bindingName}: typeof `;
175175
yield ['', undefined, 0, { __linkedToken: token }];
176-
yield varName;
176+
yield bindingName;
177177
yield endOfLine;
178178
}
179179
yield `}>${endOfLine}`;

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ export interface StyleCodegenOptions {
1111
vueCompilerOptions: VueCompilerOptions;
1212
usedCssModule: boolean;
1313
styles: Sfc['styles'];
14-
destructuredPropNames: Set<string>;
1514
templateRefNames: Set<string>;
16-
scriptBindings: Set<string>;
15+
destructuredPropNames: Set<string>;
16+
setupBindingNames: Set<string>;
1717
}
1818

1919
export { generate as generateStyle };
2020

2121
function* generate(options: StyleCodegenOptions) {
22-
const ctx = createTemplateCodegenContext(options.scriptBindings);
22+
const ctx = createTemplateCodegenContext(options.setupBindingNames);
2323
const endScope = ctx.startScope();
24+
ctx.declare(...options.destructuredPropNames);
2425
yield* generateStyleScopedClasses(options);
2526
yield* generateStyleModules(options, ctx);
2627
yield* generateCssVars(options, ctx);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ const commentDirectiveRegex = /^<!--\s*@vue-(?<name>[-\w]+)\b(?<content>[\s\S]*)
107107
* and additionally how we use that to determine whether to propagate diagnostics back upward.
108108
*/
109109
export function createTemplateCodegenContext(
110-
scriptBindings: Set<string>,
110+
setupBindingNames: Set<string>,
111111
) {
112112
let variableId = 0;
113113

@@ -307,7 +307,7 @@ export function createTemplateCodegenContext(
307307
for (const [varName, map] of all) {
308308
for (const [source, offsets] of map) {
309309
for (const offset of offsets) {
310-
if (scriptBindings.has(varName)) {
310+
if (setupBindingNames.has(varName)) {
311311
// #3409
312312
yield [
313313
varName,

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,7 @@ export function* generatePropExp(
255255
codeFeatures.withoutHighlightAndCompletion,
256256
);
257257

258-
if (
259-
options.destructuredPropNames.has(propVariableName) || ctx.scopes.some(scope => scope.has(propVariableName))
260-
) {
258+
if (ctx.scopes.some(scope => scope.has(propVariableName))) {
261259
yield* codes;
262260
}
263261
else if (options.templateRefNames.has(propVariableName)) {

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ export interface TemplateCodegenOptions {
1313
compilerOptions: ts.CompilerOptions;
1414
vueCompilerOptions: VueCompilerOptions;
1515
template: NonNullable<Sfc['template']>;
16-
scriptBindings: Set<string>;
17-
scriptSetupImportComponentNames: Set<string>;
1816
destructuredPropNames: Set<string>;
17+
setupBindingNames: Set<string>;
1918
templateRefNames: Set<string>;
19+
scriptSetupImportComponentNames: Set<string>;
2020
hasDefineSlots?: boolean;
2121
propsAssignName?: string;
2222
slotsAssignName?: string;
@@ -27,17 +27,15 @@ export interface TemplateCodegenOptions {
2727
export { generate as generateTemplate };
2828

2929
function generate(options: TemplateCodegenOptions) {
30-
const ctx = createTemplateCodegenContext(options.scriptBindings);
30+
const ctx = createTemplateCodegenContext(options.setupBindingNames);
3131
const codegen = generateWorker(options, ctx);
3232
const codes: Code[] = [];
33-
3433
for (const code of codegen) {
3534
if (typeof code === 'object') {
3635
code[3] = ctx.resolveCodeFeatures(code[3]);
3736
}
3837
codes.push(code);
3938
}
40-
4139
return {
4240
...ctx,
4341
codes,
@@ -49,6 +47,7 @@ function* generateWorker(
4947
ctx: TemplateCodegenContext,
5048
): Generator<Code> {
5149
const endScope = ctx.startScope();
50+
ctx.declare(...options.destructuredPropNames);
5251
const {
5352
slotsAssignName,
5453
propsAssignName,

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

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const isLiteralWhitelisted = /*@__PURE__*/ makeMap('true,false,null,this');
1616
export function* generateInterpolation(
1717
options: Pick<
1818
TemplateCodegenOptions | StyleCodegenOptions,
19-
'ts' | 'destructuredPropNames' | 'templateRefNames'
19+
'ts' | 'templateRefNames'
2020
>,
2121
ctx: TemplateCodegenContext,
2222
block: SfcBlock,
@@ -70,9 +70,9 @@ export function* generateInterpolation(
7070
}
7171

7272
function* forEachInterpolationSegment(
73-
options: Pick<
73+
{ ts, templateRefNames }: Pick<
7474
TemplateCodegenOptions | StyleCodegenOptions,
75-
'ts' | 'destructuredPropNames' | 'templateRefNames'
75+
'ts' | 'templateRefNames'
7676
>,
7777
ctx: TemplateCodegenContext,
7878
block: SfcBlock,
@@ -93,7 +93,7 @@ function* forEachInterpolationSegment(
9393

9494
for (
9595
const [name, offset, isShorthand] of forEachIdentifiers(
96-
options,
96+
ts,
9797
ctx,
9898
block,
9999
originalCode,
@@ -113,7 +113,7 @@ function* forEachInterpolationSegment(
113113
// fix https://github.com/vuejs/language-tools/issues/1264
114114
yield ['', offset, 'errorMappingOnly'];
115115

116-
if (options.templateRefNames.has(name)) {
116+
if (templateRefNames.has(name)) {
117117
yield `__VLS_unref(`;
118118
yield [name, offset];
119119
yield `)`;
@@ -134,29 +134,25 @@ function* forEachInterpolationSegment(
134134
}
135135

136136
function* forEachIdentifiers(
137-
options: Pick<
138-
TemplateCodegenOptions | StyleCodegenOptions,
139-
'ts' | 'destructuredPropNames'
140-
>,
137+
ts: typeof import('typescript'),
141138
ctx: TemplateCodegenContext,
142139
block: SfcBlock,
143140
originalCode: string,
144141
code: string,
145142
prefix: string,
146143
): Generator<[string, number, boolean]> {
147144
if (
148-
identifierRegex.test(originalCode) && !shouldIdentifierSkipped(options, ctx, originalCode)
145+
identifierRegex.test(originalCode) && !shouldIdentifierSkipped(ctx, originalCode)
149146
) {
150147
yield [originalCode, prefix.length, false];
151148
return;
152149
}
153150

154-
const { ts } = options;
155151
const endScope = ctx.startScope();
156152
const ast = getTypeScriptAST(ts, block, code);
157153
for (const [id, isShorthand] of forEachDeclarations(ts, ast, ast, ctx)) {
158154
const text = getNodeText(ts, id, ast);
159-
if (shouldIdentifierSkipped(options, ctx, text)) {
155+
if (shouldIdentifierSkipped(ctx, text)) {
160156
continue;
161157
}
162158
yield [text, getStartEnd(ts, id, ast).start, isShorthand];
@@ -288,10 +284,6 @@ function* forEachDeclarationsInTypeNode(
288284
}
289285

290286
function shouldIdentifierSkipped(
291-
{ destructuredPropNames }: Pick<
292-
TemplateCodegenOptions | StyleCodegenOptions,
293-
'destructuredPropNames'
294-
>,
295287
ctx: TemplateCodegenContext,
296288
text: string,
297289
) {
@@ -300,6 +292,5 @@ function shouldIdentifierSkipped(
300292
|| isGloballyAllowed(text)
301293
|| isLiteralWhitelisted(text)
302294
|| text === 'require'
303-
|| text.startsWith('__VLS_')
304-
|| destructuredPropNames.has(text);
295+
|| text.startsWith('__VLS_');
305296
}

packages/language-core/lib/plugins/vue-tsx.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ function useCodegen(
136136
return newNames;
137137
});
138138

139-
const getSetupDestructuredPropNames = computedSet(() => {
139+
const getDestructuredPropNames = computedSet(() => {
140140
const newNames = new Set(getScriptSetupRanges()?.defineProps?.destructured?.keys());
141141
const rest = getScriptSetupRanges()?.defineProps?.destructuredRest;
142142
if (rest) {
@@ -197,10 +197,10 @@ function useCodegen(
197197
compilerOptions: ctx.compilerOptions,
198198
vueCompilerOptions: getResolvedOptions(),
199199
template: sfc.template,
200-
scriptBindings: getBindingNames(),
201-
scriptSetupImportComponentNames: getSetupImportComponentNames(),
202-
destructuredPropNames: getSetupDestructuredPropNames(),
200+
destructuredPropNames: getDestructuredPropNames(),
201+
setupBindingNames: getBindingNames(),
203202
templateRefNames: getSetupTemplateRefNames(),
203+
scriptSetupImportComponentNames: getSetupImportComponentNames(),
204204
hasDefineSlots: setupHasDefineSlots(),
205205
propsAssignName: getSetupPropsAssignName(),
206206
slotsAssignName: getSetupSlotsAssignName(),
@@ -225,7 +225,7 @@ function useCodegen(
225225
vueCompilerOptions: getResolvedOptions(),
226226
script: sfc.script,
227227
scriptSetup: sfc.scriptSetup,
228-
scriptBindings: getBindingNames(),
228+
setupBindingNames: getBindingNames(),
229229
fileName,
230230
lang: getLang(),
231231
scriptRanges: getScriptRanges(),
@@ -248,9 +248,9 @@ function useCodegen(
248248
vueCompilerOptions: getResolvedOptions(),
249249
usedCssModule: usedCssModule(),
250250
styles: sfc.styles,
251-
destructuredPropNames: getSetupDestructuredPropNames(),
251+
destructuredPropNames: getDestructuredPropNames(),
252252
templateRefNames: getSetupTemplateRefNames(),
253-
scriptBindings: getBindingNames(),
253+
setupBindingNames: getBindingNames(),
254254
});
255255
const codes: Code[] = [];
256256
let ctx: TemplateCodegenContext;

0 commit comments

Comments
 (0)