Skip to content

Commit b7a9bbb

Browse files
authored
refactor(language-core): decouple the usage of code features with template context (#5564)
1 parent aa3531f commit b7a9bbb

File tree

15 files changed

+109
-115
lines changed

15 files changed

+109
-115
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ export interface ScriptCodegenOptions {
2626
templateRefNames: Set<string>;
2727
}
2828

29-
export function* generateScript(options: ScriptCodegenOptions): Generator<Code, ScriptCodegenContext> {
30-
const ctx = createScriptCodegenContext(options);
31-
29+
export function* generateScript(
30+
options: ScriptCodegenOptions,
31+
ctx: ScriptCodegenContext = createScriptCodegenContext(options),
32+
): Generator<Code> {
3233
yield* generateGlobalTypesPath(options);
3334

3435
if (options.sfc.script?.src) {
@@ -148,8 +149,6 @@ export function* generateScript(options: ScriptCodegenOptions): Generator<Code,
148149
if (options.sfc.scriptSetup) {
149150
yield ['', 'scriptSetup', options.sfc.scriptSetup.content.length, codeFeatures.verification];
150151
}
151-
152-
return ctx;
153152
}
154153

155154
function* generateGlobalTypesPath(

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,6 @@ export function createTemplateCodegenContext(
191191
get currentInfo() {
192192
return stack[stack.length - 1];
193193
},
194-
codeFeatures: new Proxy(codeFeatures, {
195-
get(target, key: keyof typeof codeFeatures) {
196-
const data = target[key];
197-
return resolveCodeFeatures(data);
198-
},
199-
}),
200194
resolveCodeFeatures,
201195
inlineTsAsts: templateAst && templateInlineTsAsts.get(templateAst),
202196
inVFor: false,

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export function* generateComponent(
9090
componentOriginalVar,
9191
'template',
9292
tagOffset,
93-
ctx.codeFeatures.withoutHighlightAndCompletion,
93+
codeFeatures.withoutHighlightAndCompletion,
9494
];
9595
}
9696
else {
@@ -99,7 +99,7 @@ export function* generateComponent(
9999
shouldCapitalize ? capitalize(node.tag) : node.tag,
100100
'template',
101101
tagOffset,
102-
ctx.codeFeatures.withoutHighlightAndCompletion,
102+
codeFeatures.withoutHighlightAndCompletion,
103103
);
104104
}
105105
yield `, `;
@@ -112,7 +112,7 @@ export function* generateComponent(
112112
options,
113113
ctx,
114114
'template',
115-
ctx.codeFeatures.all,
115+
codeFeatures.all,
116116
dynamicTagInfo.tag,
117117
dynamicTagInfo.offsets[0],
118118
`(`,
@@ -124,7 +124,7 @@ export function* generateComponent(
124124
options,
125125
ctx,
126126
'template',
127-
ctx.codeFeatures.withoutCompletion,
127+
codeFeatures.withoutCompletion,
128128
dynamicTagInfo.tag,
129129
dynamicTagInfo.offsets[1],
130130
`(`,
@@ -152,12 +152,12 @@ export function* generateComponent(
152152
yield* generateCanonicalComponentName(
153153
node.tag,
154154
tagOffsets[0],
155-
ctx.resolveCodeFeatures({
155+
{
156156
...codeFeatures.semanticWithoutHighlight,
157157
...options.vueCompilerOptions.checkUnknownComponents
158158
? codeFeatures.verification
159159
: codeFeatures.doNotReportTs2339AndTs2551,
160-
}),
160+
},
161161
);
162162
yield `${endOfLine}`;
163163

@@ -212,7 +212,7 @@ export function* generateComponent(
212212
yield* wrapWith(
213213
node.loc.start.offset,
214214
node.loc.end.offset,
215-
ctx.codeFeatures.doNotReportTs6133,
215+
codeFeatures.doNotReportTs6133,
216216
componentVNodeVar,
217217
);
218218
yield ` = ${componentFunctionalVar}`;
@@ -221,7 +221,7 @@ export function* generateComponent(
221221
yield* wrapWith(
222222
tagOffsets[0],
223223
tagOffsets[0] + node.tag.length,
224-
ctx.codeFeatures.verification,
224+
codeFeatures.verification,
225225
`{${newLine}`,
226226
...generateElementProps(
227227
options,
@@ -328,7 +328,7 @@ export function* generateElement(
328328
yield* wrapWith(
329329
startTagOffset,
330330
startTagOffset + node.tag.length,
331-
ctx.codeFeatures.verification,
331+
codeFeatures.verification,
332332
`{${newLine}`,
333333
...generateElementProps(
334334
options,
@@ -380,7 +380,7 @@ function* generateFailedPropExps(
380380
options,
381381
ctx,
382382
'template',
383-
ctx.codeFeatures.all,
383+
codeFeatures.all,
384384
failedExp.node.loc.source,
385385
failedExp.node.loc.start.offset,
386386
failedExp.prefix,
@@ -436,13 +436,13 @@ function* generateComponentGeneric(
436436
yield* wrapWith(
437437
offset,
438438
offset + content.length,
439-
ctx.codeFeatures.verification,
439+
codeFeatures.verification,
440440
`<`,
441441
[
442442
content,
443443
'template',
444444
offset,
445-
ctx.codeFeatures.all,
445+
codeFeatures.all,
446446
],
447447
`>`,
448448
);
@@ -469,7 +469,7 @@ function* generateElementReference(
469469
ctx,
470470
content,
471471
startOffset,
472-
ctx.codeFeatures.navigation,
472+
codeFeatures.navigation,
473473
);
474474
yield `} */${endOfLine}`;
475475

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ export function* generateElementDirectives(
4141
yield* wrapWith(
4242
prop.loc.start.offset,
4343
prop.loc.end.offset,
44-
ctx.codeFeatures.verification,
44+
codeFeatures.verification,
4545
`__VLS_asFunctionalDirective(`,
46-
...generateIdentifier(options, ctx, prop),
46+
...generateIdentifier(options, prop),
4747
`)(null!, { ...__VLS_directiveBindingRestFields, `,
4848
...generateArg(options, ctx, prop),
4949
...generateModifiers(options, ctx, prop),
@@ -56,23 +56,22 @@ export function* generateElementDirectives(
5656

5757
function* generateIdentifier(
5858
options: TemplateCodegenOptions,
59-
ctx: TemplateCodegenContext,
6059
prop: CompilerDOM.DirectiveNode,
6160
): Generator<Code> {
6261
const rawName = 'v-' + prop.name;
6362
yield* wrapWith(
6463
prop.loc.start.offset,
6564
prop.loc.start.offset + rawName.length,
66-
ctx.codeFeatures.verification,
65+
codeFeatures.verification,
6766
`__VLS_directives.`,
6867
...generateCamelized(
6968
rawName,
7069
'template',
7170
prop.loc.start.offset,
72-
ctx.resolveCodeFeatures({
71+
{
7372
...codeFeatures.withoutHighlightAndCompletion,
7473
verification: options.vueCompilerOptions.checkUnknownDirectives && !builtInDirectives.has(prop.name),
75-
}),
74+
},
7675
),
7776
);
7877
}
@@ -92,23 +91,23 @@ function* generateArg(
9291
yield* wrapWith(
9392
startOffset,
9493
startOffset + arg.content.length,
95-
ctx.codeFeatures.verification,
94+
codeFeatures.verification,
9695
`arg`,
9796
);
9897
yield `: `;
9998
if (arg.isStatic) {
10099
yield* generateStringLiteralKey(
101100
arg.content,
102101
startOffset,
103-
ctx.codeFeatures.all,
102+
codeFeatures.all,
104103
);
105104
}
106105
else {
107106
yield* generateInterpolation(
108107
options,
109108
ctx,
110109
'template',
111-
ctx.codeFeatures.all,
110+
codeFeatures.all,
112111
arg.content,
113112
startOffset,
114113
`(`,
@@ -135,7 +134,7 @@ export function* generateModifiers(
135134
yield* wrapWith(
136135
startOffset,
137136
endOffset,
138-
ctx.codeFeatures.verification,
137+
codeFeatures.verification,
139138
propertyName,
140139
);
141140
yield `: { `;
@@ -145,7 +144,7 @@ export function* generateModifiers(
145144
ctx,
146145
mod.content,
147146
mod.loc.start.offset,
148-
ctx.codeFeatures.withoutHighlightAndNavigation,
147+
codeFeatures.withoutHighlightAndNavigation,
149148
);
150149
yield `: true, `;
151150
}
@@ -165,7 +164,7 @@ function* generateValue(
165164
yield* wrapWith(
166165
exp.loc.start.offset,
167166
exp.loc.end.offset,
168-
ctx.codeFeatures.verification,
167+
codeFeatures.verification,
169168
`value`,
170169
);
171170
yield `: `;

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ export function* generateElementEvents(
6262
yield `const ${ctx.getInternalVariable()}: __VLS_NormalizeComponentEvent<typeof ${propsVar}, typeof ${emitsVar}, '${propName}', '${emitName}', '${camelizedEmitName}'> = (${newLine}`;
6363
if (prop.name === 'on') {
6464
yield `{ `;
65-
yield* generateEventArg(options, ctx, source, start!, emitPrefix.slice(0, -1), ctx.codeFeatures.navigation);
65+
yield* generateEventArg(options, source, start!, emitPrefix.slice(0, -1), codeFeatures.navigation);
6666
yield `: {} as any } as typeof ${emitsVar},${newLine}`;
6767
}
6868
yield `{ `;
6969
if (prop.name === 'on') {
70-
yield* generateEventArg(options, ctx, source, start!, propPrefix.slice(0, -1));
70+
yield* generateEventArg(options, source, start!, propPrefix.slice(0, -1));
7171
yield `: `;
7272
yield* generateEventExpression(options, ctx, prop);
7373
}
@@ -82,19 +82,18 @@ export function* generateElementEvents(
8282

8383
export function* generateEventArg(
8484
options: TemplateCodegenOptions,
85-
ctx: TemplateCodegenContext,
8685
name: string,
8786
start: number,
8887
directive = 'on',
8988
features?: VueCodeInformation,
9089
): Generator<Code> {
91-
features ??= ctx.resolveCodeFeatures({
90+
features ??= {
9291
...codeFeatures.semanticWithoutHighlight,
9392
...codeFeatures.navigationWithoutRename,
9493
...options.vueCompilerOptions.checkUnknownEvents
9594
? codeFeatures.verification
9695
: codeFeatures.doNotReportTs2353AndTs2561,
97-
});
96+
};
9897

9998
if (directive.length) {
10099
name = capitalize(name);
@@ -159,7 +158,7 @@ export function* generateEventExpression(
159158
].join('\n\n'),
160159
});
161160
}
162-
return ctx.codeFeatures.all;
161+
return codeFeatures.all;
163162
},
164163
prop.exp.content,
165164
prop.exp.loc.start.offset,
@@ -192,7 +191,7 @@ export function* generateModelEventExpression(
192191
options,
193192
ctx,
194193
'template',
195-
ctx.codeFeatures.verification,
194+
codeFeatures.verification,
196195
prop.exp.content,
197196
prop.exp.loc.start.offset,
198197
);

0 commit comments

Comments
 (0)