Skip to content

Commit 305dec6

Browse files
committed
refactor(language-core): reduce the dependency of script codegen on template codegen context
1 parent 493af70 commit 305dec6

File tree

8 files changed

+244
-245
lines changed

8 files changed

+244
-245
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ export function* generateComponent(
6363
if (
6464
options.vueCompilerOptions.target >= 3.5
6565
&& options.vueCompilerOptions.inferComponentDollarRefs
66-
&& options.templateCodegen?.templateRefs.size
66+
&& options.templateCodegen?.generatedTypes.has(names.TemplateRefs)
6767
) {
6868
yield `__typeRefs: {} as ${names.TemplateRefs},${newLine}`;
6969
}
7070
if (
7171
options.vueCompilerOptions.target >= 3.5
7272
&& options.vueCompilerOptions.inferComponentDollarEl
73-
&& options.templateCodegen?.singleRootElTypes.size
73+
&& options.templateCodegen?.generatedTypes.has(names.RootEl)
7474
) {
7575
yield `__typeEl: {} as ${names.RootEl},${newLine}`;
7676
}
@@ -125,7 +125,7 @@ function* generatePropsOption(
125125
const optionGenerates: Iterable<Code>[] = [];
126126
const typeOptionGenerates: Iterable<Code>[] = [];
127127

128-
if (options.templateCodegen?.inheritedAttrVars.size) {
128+
if (options.templateCodegen?.generatedTypes.has(names.InheritedAttrs)) {
129129
const attrsType = hasEmitsOption
130130
? `Omit<${names.InheritedAttrs}, keyof ${names.EmitProps}>`
131131
: names.InheritedAttrs;
@@ -134,7 +134,7 @@ function* generatePropsOption(
134134
optionGenerates.push([`{} as ${optionType}`]);
135135
typeOptionGenerates.push([`{} as ${attrsType}`]);
136136
}
137-
if (ctx.generatedPropsType) {
137+
if (ctx.generatedTypes.has(names.PublicProps)) {
138138
if (options.vueCompilerOptions.target < 3.6) {
139139
let propsType = `${ctx.localTypes.TypePropsToOption}<${names.PublicProps}>`;
140140
if (scriptSetupRanges.withDefaults?.arg) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function createScriptCodegenContext(options: ScriptCodegenOptions) {
99
const inlayHints: InlayHintInfo[] = [];
1010

1111
return {
12-
generatedPropsType: false,
12+
generatedTypes: new Set<string>(),
1313
bindingNames: new Set([
1414
...options.scriptRanges?.bindings.map(
1515
({ range }) => options.script!.content.slice(range.start, range.end),

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export function* generateGeneric(
5454
const propTypes: string[] = [];
5555
const { vueCompilerOptions } = options;
5656

57-
if (ctx.generatedPropsType) {
57+
if (ctx.generatedTypes.has(names.PublicProps)) {
5858
propTypes.push(names.PublicProps);
5959
}
6060
if (scriptSetupRanges.defineProps?.arg) {
@@ -75,7 +75,7 @@ export function* generateGeneric(
7575
if (scriptSetupRanges.defineEmits || scriptSetupRanges.defineModel.length) {
7676
propTypes.push(names.EmitProps);
7777
}
78-
if (options.templateCodegen?.inheritedAttrVars.size) {
78+
if (options.templateCodegen?.generatedTypes.has(names.InheritedAttrs)) {
7979
propTypes.push(names.InheritedAttrs);
8080
}
8181

@@ -283,19 +283,12 @@ export function* generateSetupFunction(
283283
generateSfcBlockSection(scriptSetup, start, end, codeFeatures.all, end === scriptSetup.content.length),
284284
);
285285
yield* generateMacros(options, ctx);
286-
287-
const hasSlots = !!(
288-
scriptSetupRanges.defineSlots
289-
|| options.templateCodegen?.slots.length
290-
|| options.templateCodegen?.dynamicSlots.length
291-
);
292-
293286
yield* generateModels(scriptSetup, scriptSetupRanges);
294-
yield* generatePublicProps(options, ctx, scriptSetup, scriptSetupRanges, hasSlots);
287+
yield* generatePublicProps(options, ctx, scriptSetup, scriptSetupRanges);
295288
yield* body;
296289

297290
if (output) {
298-
if (hasSlots) {
291+
if (hasSlotsType(options)) {
299292
yield `const __VLS_base = `;
300293
yield* generateComponent(options, ctx, scriptSetup, scriptSetupRanges);
301294
yield endOfLine;
@@ -390,7 +383,6 @@ function* generatePublicProps(
390383
ctx: ScriptCodegenContext,
391384
scriptSetup: NonNullable<Sfc['scriptSetup']>,
392385
scriptSetupRanges: ScriptSetupRanges,
393-
hasSlots: boolean,
394386
): Generator<Code> {
395387
if (scriptSetupRanges.defineProps?.typeArg && scriptSetupRanges.withDefaults?.arg) {
396388
yield `const ${names.defaults} = `;
@@ -404,7 +396,7 @@ function* generatePublicProps(
404396
}
405397

406398
const propTypes: string[] = [];
407-
if (options.vueCompilerOptions.jsxSlots && hasSlots) {
399+
if (options.vueCompilerOptions.jsxSlots && hasSlotsType(options)) {
408400
propTypes.push(`${ctx.localTypes.PropsChildren}<${names.Slots}>`);
409401
}
410402
if (scriptSetupRanges.defineProps?.typeArg) {
@@ -414,11 +406,18 @@ function* generatePublicProps(
414406
propTypes.push(names.ModelProps);
415407
}
416408
if (propTypes.length) {
417-
ctx.generatedPropsType = true;
418409
yield `type ${names.PublicProps} = ${propTypes.join(` & `)}${endOfLine}`;
410+
ctx.generatedTypes.add(names.PublicProps);
419411
}
420412
}
421413

414+
function hasSlotsType(options: ScriptCodegenOptions): boolean {
415+
return !!(
416+
options.scriptSetupRanges?.defineSlots
417+
|| options.templateCodegen?.generatedTypes.has(names.Slots)
418+
);
419+
}
420+
422421
function* generateModels(
423422
scriptSetup: NonNullable<Sfc['scriptSetup']>,
424423
scriptSetupRanges: ScriptSetupRanges,

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

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,68 +35,60 @@ export function* generateTemplate(
3535
if (options.templateCodegen) {
3636
yield* options.templateCodegen.codes;
3737
}
38-
else {
39-
if (!options.scriptSetupRanges?.defineSlots) {
40-
yield `type ${names.Slots} = {}${endOfLine}`;
41-
}
42-
yield `type ${names.InheritedAttrs} = {}${endOfLine}`;
43-
yield `type ${names.TemplateRefs} = {}${endOfLine}`;
44-
yield `type ${names.RootEl} = any${endOfLine}`;
45-
}
4638
}
4739

48-
function* generateSelf(options: ScriptCodegenOptions): Generator<Code> {
49-
if (options.script && options.scriptRanges?.componentOptions) {
50-
yield `const ${names.self} = (await import('${options.vueCompilerOptions.lib}')).defineComponent(`;
51-
const { args } = options.scriptRanges.componentOptions;
52-
yield* generateSfcBlockSection(options.script, args.start, args.end, codeFeatures.all);
40+
function* generateSelf({ script, scriptRanges, vueCompilerOptions, fileName }: ScriptCodegenOptions): Generator<Code> {
41+
if (script && scriptRanges?.componentOptions) {
42+
yield `const ${names.self} = (await import('${vueCompilerOptions.lib}')).defineComponent(`;
43+
const { args } = scriptRanges.componentOptions;
44+
yield* generateSfcBlockSection(script, args.start, args.end, codeFeatures.all);
5345
yield `)${endOfLine}`;
5446
}
55-
else if (options.script && options.scriptRanges?.exportDefault) {
47+
else if (script && scriptRanges?.exportDefault) {
5648
yield `const ${names.self} = `;
57-
const { expression } = options.scriptRanges.exportDefault;
58-
yield* generateSfcBlockSection(options.script, expression.start, expression.end, codeFeatures.all);
49+
const { expression } = scriptRanges.exportDefault;
50+
yield* generateSfcBlockSection(script, expression.start, expression.end, codeFeatures.all);
5951
yield endOfLine;
6052
}
61-
else if (options.script?.src) {
62-
yield `let ${names.self}!: typeof import('./${path.basename(options.fileName)}').default${endOfLine}`;
53+
else if (script?.src) {
54+
yield `let ${names.self}!: typeof import('./${path.basename(fileName)}').default${endOfLine}`;
6355
}
6456
}
6557

6658
function* generateTemplateCtx(
67-
options: ScriptCodegenOptions,
59+
{ vueCompilerOptions, script, scriptRanges, styles, scriptSetupRanges, fileName }: ScriptCodegenOptions,
6860
ctx: ScriptCodegenContext,
6961
): Generator<Code> {
7062
const exps: Iterable<Code>[] = [];
7163
const emitTypes: string[] = [];
7264
const propTypes: string[] = [];
7365

74-
if (options.vueCompilerOptions.petiteVueExtensions.some(ext => options.fileName.endsWith(ext))) {
66+
if (vueCompilerOptions.petiteVueExtensions.some(ext => fileName.endsWith(ext))) {
7567
exps.push([`globalThis`]);
7668
}
77-
if (options.script?.src || options.scriptRanges?.exportDefault) {
69+
if (script?.src || scriptRanges?.exportDefault) {
7870
exps.push([`{} as InstanceType<__VLS_PickNotAny<typeof ${names.self}, new () => {}>>`]);
7971
}
8072
else {
81-
exps.push([`{} as import('${options.vueCompilerOptions.lib}').ComponentPublicInstance`]);
73+
exps.push([`{} as import('${vueCompilerOptions.lib}').ComponentPublicInstance`]);
8274
}
83-
if (options.styles.some(style => style.module)) {
75+
if (styles.some(style => style.module)) {
8476
exps.push([`{} as __VLS_StyleModules`]);
8577
}
8678

87-
if (options.scriptSetupRanges?.defineEmits) {
88-
const { defineEmits } = options.scriptSetupRanges;
79+
if (scriptSetupRanges?.defineEmits) {
80+
const { defineEmits } = scriptSetupRanges;
8981
emitTypes.push(`typeof ${defineEmits.name ?? names.emit}`);
9082
}
91-
if (options.scriptSetupRanges?.defineModel.length) {
83+
if (scriptSetupRanges?.defineModel.length) {
9284
emitTypes.push(`typeof ${names.modelEmit}`);
9385
}
9486
if (emitTypes.length) {
9587
yield `type ${names.EmitProps} = __VLS_EmitsToProps<__VLS_NormalizeEmits<${emitTypes.join(` & `)}>>${endOfLine}`;
9688
exps.push([`{} as { $emit: ${emitTypes.join(` & `)} }`]);
9789
}
9890

99-
const { defineProps, withDefaults } = options.scriptSetupRanges ?? {};
91+
const { defineProps, withDefaults } = scriptSetupRanges ?? {};
10092
const props = defineProps?.arg
10193
? `typeof ${defineProps.name ?? names.props}`
10294
: defineProps?.typeArg
@@ -107,7 +99,7 @@ function* generateTemplateCtx(
10799
if (props) {
108100
propTypes.push(props);
109101
}
110-
if (options.scriptSetupRanges?.defineModel.length) {
102+
if (scriptSetupRanges?.defineModel.length) {
111103
propTypes.push(names.ModelProps);
112104
}
113105
if (emitTypes.length) {
@@ -119,7 +111,7 @@ function* generateTemplateCtx(
119111
exps.push([`{} as ${names.InternalProps}`]);
120112
}
121113

122-
if (options.scriptSetupRanges && ctx.bindingNames.size) {
114+
if (scriptSetupRanges && ctx.bindingNames.size) {
123115
exps.push([`{} as ${names.Bindings}`]);
124116
}
125117

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ export function createTemplateCodegenContext(
155155
const commentBuffer: CompilerDOM.CommentNode[] = [];
156156

157157
return {
158+
generatedTypes: new Set<string>(),
158159
get currentInfo() {
159160
return stack[stack.length - 1]!;
160161
},

0 commit comments

Comments
 (0)