Skip to content

Commit feaca58

Browse files
committed
refactor(language-core): remove generatePartiallyEnding function
1 parent 75211c2 commit feaca58

File tree

7 files changed

+101
-113
lines changed

7 files changed

+101
-113
lines changed

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

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function* generateComponent(
1818
&& options.scriptRanges.componentOptions.expression.start !== options.scriptRanges.componentOptions.args.start
1919
) {
2020
// use defineComponent() from user space code if it exist
21-
yield generateSfcBlockSection(
21+
yield* generateSfcBlockSection(
2222
options.script,
2323
options.scriptRanges.componentOptions.expression.start,
2424
options.scriptRanges.componentOptions.args.start,
@@ -30,21 +30,22 @@ export function* generateComponent(
3030
yield `(await import('${options.vueCompilerOptions.lib}')).defineComponent({${newLine}`;
3131
}
3232

33-
const returns: string[] = [];
33+
const returns: string[][] = [];
3434
if (ctx.bypassDefineComponent) {
3535
// fill $props
3636
if (scriptSetupRanges.defineProps) {
3737
const name = scriptSetupRanges.defineProps.name ?? `__VLS_props`;
3838
// NOTE: defineProps is inaccurate for $props
39-
returns.push(name, `{} as { $props: Partial<typeof ${name}> }`);
39+
returns.push([name]);
40+
returns.push([`{} as { $props: Partial<typeof ${name}> }`]);
4041
}
4142
// fill $emit
4243
if (scriptSetupRanges.defineEmits) {
43-
returns.push(`{} as { $emit: typeof ${scriptSetupRanges.defineEmits.name ?? `__VLS_emit`} }`);
44+
returns.push([`{} as { $emit: typeof ${scriptSetupRanges.defineEmits.name ?? `__VLS_emit`} }`]);
4445
}
4546
}
4647
if (scriptSetupRanges.defineExpose) {
47-
returns.push(`__VLS_exposed`);
48+
returns.push([`__VLS_exposed`]);
4849
}
4950
if (returns.length) {
5051
yield `setup: () => (`;
@@ -73,7 +74,7 @@ export function* generateComponent(
7374
}
7475
if (options.script && options.scriptRanges?.componentOptions?.args) {
7576
const { args } = options.scriptRanges.componentOptions;
76-
yield generateSfcBlockSection(options.script, args.start + 1, args.end - 1, codeFeatures.all);
77+
yield* generateSfcBlockSection(options.script, args.start + 1, args.end - 1, codeFeatures.all);
7778
}
7879
yield `})`;
7980
}
@@ -82,18 +83,18 @@ function* generateEmitsOption(
8283
options: ScriptCodegenOptions,
8384
scriptSetupRanges: ScriptSetupRanges,
8485
): Generator<Code> {
85-
const optionCodes: Code[] = [];
86-
const typeOptionCodes: Code[] = [];
86+
const optionCodes: Code[][] = [];
87+
const typeOptionCodes: Code[][] = [];
8788

8889
if (scriptSetupRanges.defineModel.length) {
89-
optionCodes.push(`{} as __VLS_NormalizeEmits<typeof __VLS_modelEmit>`);
90-
typeOptionCodes.push(`__VLS_ModelEmit`);
90+
optionCodes.push([`{} as __VLS_NormalizeEmits<typeof __VLS_modelEmit>`]);
91+
typeOptionCodes.push([`__VLS_ModelEmit`]);
9192
}
9293
if (scriptSetupRanges.defineEmits) {
9394
const { name, typeArg, hasUnionTypeArg } = scriptSetupRanges.defineEmits;
94-
optionCodes.push(`{} as __VLS_NormalizeEmits<typeof ${name ?? '__VLS_emit'}>`);
95+
optionCodes.push([`{} as __VLS_NormalizeEmits<typeof ${name ?? '__VLS_emit'}>`]);
9596
if (typeArg && !hasUnionTypeArg) {
96-
typeOptionCodes.push(`__VLS_Emit`);
97+
typeOptionCodes.push([`__VLS_Emit`]);
9798
}
9899
else {
99100
typeOptionCodes.length = 0;
@@ -119,42 +120,37 @@ function* generatePropsOption(
119120
scriptSetupRanges: ScriptSetupRanges,
120121
hasEmitsOption: boolean,
121122
): Generator<Code> {
122-
const getOptionCodes: (() => Code)[] = [];
123-
const typeOptionCodes: Code[] = [];
123+
const optionGenerates: Iterable<Code>[] = [];
124+
const typeOptionGenerates: Iterable<Code>[] = [];
124125

125126
if (options.templateCodegen?.inheritedAttrVars.size) {
126127
let attrsType = `__VLS_InheritedAttrs`;
127128
if (hasEmitsOption) {
128129
attrsType = `Omit<${attrsType}, keyof __VLS_EmitProps>`;
129130
}
130-
getOptionCodes.push(() => {
131-
const propsType = `__VLS_PickNotAny<${ctx.localTypes.OmitIndexSignature}<${attrsType}>, {}>`;
132-
const optionType = `${ctx.localTypes.TypePropsToOption}<${propsType}>`;
133-
return `{} as ${optionType}`;
134-
});
135-
typeOptionCodes.push(`{} as ${attrsType}`);
131+
const propsType = `__VLS_PickNotAny<${ctx.localTypes.OmitIndexSignature}<${attrsType}>, {}>`;
132+
const optionType = `${ctx.localTypes.TypePropsToOption}<${propsType}>`;
133+
optionGenerates.push([`{} as ${optionType}`]);
134+
typeOptionGenerates.push([`{} as ${attrsType}`]);
136135
}
137136
if (ctx.generatedPropsType) {
138137
if (options.vueCompilerOptions.target < 3.6) {
139-
getOptionCodes.push(() => {
140-
const propsType = `${ctx.localTypes.TypePropsToOption}<__VLS_PublicProps>`;
141-
return `{} as ` + (
142-
scriptSetupRanges.withDefaults?.arg
143-
? `${ctx.localTypes.WithDefaultsLocal}<${propsType}, typeof __VLS_defaults>`
144-
: propsType
145-
);
146-
});
138+
let propsType = `${ctx.localTypes.TypePropsToOption}<__VLS_PublicProps>`;
139+
if (scriptSetupRanges.withDefaults?.arg) {
140+
propsType = `${ctx.localTypes.WithDefaultsLocal}<${propsType}, typeof __VLS_defaults>`;
141+
}
142+
optionGenerates.push([`{} as ${propsType}`]);
147143
}
148-
typeOptionCodes.push(`{} as __VLS_PublicProps`);
144+
typeOptionGenerates.push([`{} as __VLS_PublicProps`]);
149145
}
150146
if (scriptSetupRanges.defineProps?.arg) {
151147
const { arg } = scriptSetupRanges.defineProps;
152-
getOptionCodes.push(() => generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.navigation));
153-
typeOptionCodes.length = 0;
148+
optionGenerates.push(generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.navigation));
149+
typeOptionGenerates.length = 0;
154150
}
155151

156-
const useTypeOption = options.vueCompilerOptions.target >= 3.5 && typeOptionCodes.length;
157-
const useOption = (!useTypeOption || scriptSetupRanges.withDefaults) && getOptionCodes.length;
152+
const useTypeOption = options.vueCompilerOptions.target >= 3.5 && typeOptionGenerates.length;
153+
const useOption = (!useTypeOption || scriptSetupRanges.withDefaults) && optionGenerates.length;
158154

159155
if (useTypeOption) {
160156
if (
@@ -164,12 +160,12 @@ function* generatePropsOption(
164160
yield `__defaults: __VLS_defaults,${newLine}`;
165161
}
166162
yield `__typeProps: `;
167-
yield* generateSpreadMerge(typeOptionCodes);
163+
yield* generateSpreadMerge(typeOptionGenerates);
168164
yield `,${newLine}`;
169165
}
170166
if (useOption) {
171167
yield `props: `;
172-
yield* generateSpreadMerge(getOptionCodes.map(fn => fn()));
168+
yield* generateSpreadMerge(optionGenerates);
173169
yield `,${newLine}`;
174170
}
175171
}

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

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { ScriptSetupRanges } from '../../parsers/scriptSetupRanges';
55
import type { Code, Sfc, SfcBlock, VueCompilerOptions } from '../../types';
66
import { codeFeatures } from '../codeFeatures';
77
import type { TemplateCodegenContext } from '../template/context';
8-
import { endOfLine, generatePartiallyEnding, generateSfcBlockSection, newLine } from '../utils';
8+
import { endOfLine, generateSfcBlockSection, newLine } from '../utils';
99
import { endBoundary, startBoundary } from '../utils/boundary';
1010
import { createScriptCodegenContext, type ScriptCodegenContext } from './context';
1111
import { generateScriptSetup, generateScriptSetupImports } from './scriptSetup';
@@ -55,11 +55,11 @@ function* generateScript(
5555
const { exportDefault, componentOptions } = options.scriptRanges;
5656
if (options.scriptSetup && options.scriptSetupRanges) {
5757
if (exportDefault) {
58-
yield generateSfcBlockSection(options.script, 0, exportDefault.start, codeFeatures.all);
58+
yield* generateSfcBlockSection(options.script, 0, exportDefault.start, codeFeatures.all, true);
5959
yield* generateScriptSetup(options, ctx, options.scriptSetup, options.scriptSetupRanges);
6060
}
6161
else {
62-
yield generateSfcBlockSection(options.script, 0, options.script.content.length, codeFeatures.all);
62+
yield* generateSfcBlockSection(options.script, 0, options.script.content.length, codeFeatures.all, true);
6363
yield* generateScriptSetup(options, ctx, options.scriptSetup, options.scriptSetupRanges);
6464
}
6565
}
@@ -90,20 +90,20 @@ function* generateScript(
9090
});
9191
}
9292

93-
yield generateSfcBlockSection(options.script, 0, exportDefault.start, codeFeatures.all);
94-
yield* generateConstExport(options, options.script);
93+
yield* generateSfcBlockSection(options.script, 0, exportDefault.start, codeFeatures.all, true);
94+
yield* generateConstExport(options.script);
9595
if (wrapLeft) {
9696
yield wrapLeft;
9797
}
98-
yield generateSfcBlockSection(options.script, expression.start, expression.end, codeFeatures.all);
98+
yield* generateSfcBlockSection(options.script, expression.start, expression.end, codeFeatures.all);
9999
if (wrapRight) {
100100
yield wrapRight;
101101
}
102102
yield endOfLine;
103103
}
104104
else {
105-
yield generateSfcBlockSection(options.script, 0, options.script.content.length, codeFeatures.all);
106-
yield* generateConstExport(options, options.script);
105+
yield* generateSfcBlockSection(options.script, 0, options.script.content.length, codeFeatures.all, true);
106+
yield* generateConstExport(options.script);
107107
yield `(await import('${options.vueCompilerOptions.lib}')).defineComponent({})${endOfLine}`;
108108
}
109109
}
@@ -144,16 +144,8 @@ function* generateGlobalTypesReference(
144144
}
145145

146146
export function* generateConstExport(
147-
options: ScriptCodegenOptions,
148147
block: SfcBlock,
149148
): Generator<Code> {
150-
if (options.script) {
151-
// #3632
152-
yield* generatePartiallyEnding(
153-
options.script.name,
154-
options.scriptRanges?.exportDefault?.start ?? options.script.content.length,
155-
);
156-
}
157149
yield `const `;
158150
const token = yield* startBoundary(block.name, 0, codeFeatures.doNotReportTs6133);
159151
yield `__VLS_export`;
@@ -171,14 +163,14 @@ function* generateExportDefault(options: ScriptCodegenOptions): Generator<Code>
171163

172164
if (options.script && options.scriptRanges?.exportDefault) {
173165
const { exportDefault, componentOptions } = options.scriptRanges;
174-
yield generateSfcBlockSection(
166+
yield* generateSfcBlockSection(
175167
options.script,
176168
exportDefault.start,
177169
(componentOptions ?? exportDefault).expression.start,
178170
codeFeatures.all,
179171
);
180172
yield expression;
181-
yield generateSfcBlockSection(
173+
yield* generateSfcBlockSection(
182174
options.script,
183175
(componentOptions ?? exportDefault).expression.end,
184176
options.script.content.length,

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

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { camelize } from '@vue/shared';
22
import type { ScriptSetupRanges } from '../../parsers/scriptSetupRanges';
33
import type { Code, Sfc, TextRange } from '../../types';
44
import { codeFeatures } from '../codeFeatures';
5-
import { endOfLine, generatePartiallyEnding, generateSfcBlockSection, identifierRegex, newLine } from '../utils';
5+
import { endOfLine, generateSfcBlockSection, identifierRegex, newLine } from '../utils';
66
import { endBoundary, startBoundary } from '../utils/boundary';
77
import { generateCamelized } from '../utils/camelized';
88
import { type CodeTransform, generateCodeWithTransforms, insert, replace } from '../utils/transform';
@@ -33,7 +33,7 @@ export function* generateScriptSetup(
3333
scriptSetupRanges: ScriptSetupRanges,
3434
): Generator<Code> {
3535
if (scriptSetup.generic) {
36-
yield* generateConstExport(options, scriptSetup);
36+
yield* generateConstExport(scriptSetup);
3737
yield `(`;
3838
if (typeof scriptSetup.generic === 'object') {
3939
yield `<`;
@@ -61,7 +61,7 @@ export function* generateScriptSetup(
6161
}
6262
if (scriptSetupRanges.defineProps?.arg) {
6363
yield `const __VLS_propsOption = `;
64-
yield generateSfcBlockSection(
64+
yield* generateSfcBlockSection(
6565
scriptSetup,
6666
scriptSetupRanges.defineProps.arg.start,
6767
scriptSetupRanges.defineProps.arg.end,
@@ -116,7 +116,7 @@ export function* generateScriptSetup(
116116
yield* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, 'export default');
117117
}
118118
else {
119-
yield* generateConstExport(options, scriptSetup);
119+
yield* generateConstExport(scriptSetup);
120120
yield `await (async () => {${newLine}`;
121121
yield* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, 'return');
122122
yield `})()${endOfLine}`;
@@ -174,7 +174,7 @@ function* generateSetupFunction(
174174
transforms.push(
175175
insert(callExp.start, function*() {
176176
yield `let __VLS_exposed!: `;
177-
yield generateSfcBlockSection(scriptSetup, typeArg.start, typeArg.end, codeFeatures.all);
177+
yield* generateSfcBlockSection(scriptSetup, typeArg.start, typeArg.end, codeFeatures.all);
178178
yield endOfLine;
179179
}),
180180
replace(typeArg.start, typeArg.end, function*() {
@@ -186,7 +186,7 @@ function* generateSetupFunction(
186186
transforms.push(
187187
insert(callExp.start, function*() {
188188
yield `const __VLS_exposed = `;
189-
yield generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.all);
189+
yield* generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.all);
190190
yield endOfLine;
191191
}),
192192
replace(arg.start, arg.end, function*() {
@@ -224,7 +224,7 @@ function* generateSetupFunction(
224224
transforms.push(
225225
insert(callExp.end, function*() {
226226
yield ` as Omit<__VLS_StyleModules, '$style'>[`;
227-
yield generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.withoutSemantic);
227+
yield* generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.withoutSemantic);
228228
yield `])`;
229229
}),
230230
replace(arg.start, arg.end, function*() {
@@ -264,7 +264,7 @@ function* generateSetupFunction(
264264
yield `<`;
265265
if (arg) {
266266
yield `__VLS_TemplateRefs[`;
267-
yield generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.withoutSemantic);
267+
yield* generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.withoutSemantic);
268268
yield `]`;
269269
}
270270
else {
@@ -283,7 +283,7 @@ function* generateSetupFunction(
283283
yield ` as __VLS_UseTemplateRef<`;
284284
if (arg) {
285285
yield `__VLS_TemplateRefs[`;
286-
yield generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.withoutSemantic);
286+
yield* generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.withoutSemantic);
287287
yield `]`;
288288
}
289289
else {
@@ -306,10 +306,9 @@ function* generateSetupFunction(
306306
Math.max(scriptSetupRanges.importSectionEndOffset, scriptSetupRanges.leadingCommentEndOffset),
307307
scriptSetup.content.length,
308308
transforms,
309-
(start, end) => generateSfcBlockSection(scriptSetup, start, end, codeFeatures.all),
309+
(start, end) =>
310+
generateSfcBlockSection(scriptSetup, start, end, codeFeatures.all, end === scriptSetup.content.length),
310311
);
311-
// #3632
312-
yield* generatePartiallyEnding(scriptSetup.name, scriptSetup.content.length);
313312
yield* generateMacros(options, ctx);
314313

315314
const hasSlots = !!(
@@ -325,7 +324,7 @@ function* generateSetupFunction(
325324
if (syntax) {
326325
const prefix = syntax === 'return'
327326
? [`return `]
328-
: generateConstExport(options, scriptSetup);
327+
: generateConstExport(scriptSetup);
329328
if (hasSlots) {
330329
yield `const __VLS_base = `;
331330
yield* generateComponent(options, ctx, scriptSetup, scriptSetupRanges);
@@ -369,7 +368,7 @@ function* generateDefineWithTypeTransforms(
369368
if (typeArg) {
370369
yield insert(statement.start, function*() {
371370
yield `type ${typeName} = `;
372-
yield generateSfcBlockSection(scriptSetup, typeArg.start, typeArg.end, codeFeatures.all);
371+
yield* generateSfcBlockSection(scriptSetup, typeArg.start, typeArg.end, codeFeatures.all);
373372
yield endOfLine;
374373
});
375374
yield replace(typeArg.start, typeArg.end, function*() {
@@ -385,21 +384,21 @@ function* generateDefineWithTypeTransforms(
385384
else if (typeArg) {
386385
yield replace(statement.start, typeArg.start, function*() {
387386
yield `const ${defaultName} = `;
388-
yield generateSfcBlockSection(scriptSetup, callExp.start, typeArg.start, codeFeatures.all);
387+
yield* generateSfcBlockSection(scriptSetup, callExp.start, typeArg.start, codeFeatures.all);
389388
});
390389
yield replace(typeArg.end, callExp.end, function*() {
391-
yield generateSfcBlockSection(scriptSetup, typeArg.end, callExp.end, codeFeatures.all);
390+
yield* generateSfcBlockSection(scriptSetup, typeArg.end, callExp.end, codeFeatures.all);
392391
yield endOfLine;
393-
yield generateSfcBlockSection(scriptSetup, statement.start, callExp.start, codeFeatures.all);
392+
yield* generateSfcBlockSection(scriptSetup, statement.start, callExp.start, codeFeatures.all);
394393
yield defaultName;
395394
});
396395
}
397396
else {
398397
yield replace(statement.start, callExp.end, function*() {
399398
yield `const ${defaultName} = `;
400-
yield generateSfcBlockSection(scriptSetup, callExp.start, callExp.end, codeFeatures.all);
399+
yield* generateSfcBlockSection(scriptSetup, callExp.start, callExp.end, codeFeatures.all);
401400
yield endOfLine;
402-
yield generateSfcBlockSection(scriptSetup, statement.start, callExp.start, codeFeatures.all);
401+
yield* generateSfcBlockSection(scriptSetup, statement.start, callExp.start, codeFeatures.all);
403402
yield defaultName;
404403
});
405404
}
@@ -410,7 +409,7 @@ function* generateDefineWithTypeTransforms(
410409
});
411410
yield insert(statement.end, function*() {
412411
yield endOfLine;
413-
yield generateSfcBlockSection(scriptSetup, statement.start, callExp.start, codeFeatures.all);
412+
yield* generateSfcBlockSection(scriptSetup, statement.start, callExp.start, codeFeatures.all);
414413
yield defaultName;
415414
});
416415
}
@@ -425,7 +424,7 @@ function* generatePublicProps(
425424
): Generator<Code> {
426425
if (scriptSetupRanges.defineProps?.typeArg && scriptSetupRanges.withDefaults?.arg) {
427426
yield `const __VLS_defaults = `;
428-
yield generateSfcBlockSection(
427+
yield* generateSfcBlockSection(
429428
scriptSetup,
430429
scriptSetupRanges.withDefaults.arg.start,
431430
scriptSetupRanges.withDefaults.arg.end,

0 commit comments

Comments
 (0)