Skip to content

Commit 882abc0

Browse files
committed
refactor(language-core): generate setup returns on demand
1 parent 769c133 commit 882abc0

File tree

8 files changed

+40
-58
lines changed

8 files changed

+40
-58
lines changed

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ScriptSetupRanges } from '../../parsers/scriptSetupRanges';
22
import type { Code, Sfc } from '../../types';
33
import { codeFeatures } from '../codeFeatures';
4-
import { endOfLine, generateSfcBlockSection, newLine } from '../utils';
4+
import { generateSfcBlockSection, newLine } from '../utils';
55
import { generateIntersectMerge, generateSpreadMerge } from '../utils/merge';
66
import type { ScriptCodegenContext } from './context';
77
import type { ScriptCodegenOptions } from './index';
@@ -29,23 +29,18 @@ export function* generateComponent(
2929
yield `(await import('${options.vueCompilerOptions.lib}')).defineComponent({${newLine}`;
3030
}
3131

32-
yield `setup() {${newLine}`;
3332
const returns: Code[] = [];
3433
if (ctx.bypassDefineComponent) {
35-
yield* `const __VLS_returns = {${newLine}`;
36-
yield* generateComponentSetupReturns(scriptSetupRanges);
37-
yield `}${endOfLine}`;
38-
returns.push(`typeof __VLS_returns`);
34+
returns.push(...generateComponentSetupReturns(scriptSetupRanges));
3935
}
4036
if (scriptSetupRanges.defineExpose) {
41-
returns.push(`typeof __VLS_exposed`);
37+
returns.push(`__VLS_exposed`);
4238
}
4339
if (returns.length) {
44-
yield `return {} as `;
45-
yield* generateIntersectMerge(returns);
46-
yield endOfLine;
40+
yield `setup: () => (`;
41+
yield* generateSpreadMerge(returns);
42+
yield `),${newLine}`;
4743
}
48-
yield `},${newLine}`;
4944

5045
if (!ctx.bypassDefineComponent) {
5146
const emitOptionCodes = [...generateEmitsOption(options, scriptSetupRanges)];
@@ -76,13 +71,14 @@ export function* generateComponent(
7671
export function* generateComponentSetupReturns(scriptSetupRanges: ScriptSetupRanges): Generator<Code> {
7772
// fill $props
7873
if (scriptSetupRanges.defineProps) {
74+
const name = scriptSetupRanges.defineProps.name ?? `__VLS_props`;
7975
// NOTE: defineProps is inaccurate for $props
80-
yield `$props: __VLS_makeOptional(${scriptSetupRanges.defineProps.name ?? `__VLS_props`}),${newLine}`;
81-
yield `...${scriptSetupRanges.defineProps.name ?? `__VLS_props`},${newLine}`;
76+
yield name;
77+
yield `{} as { $props: Partial<typeof ${name}> }`;
8278
}
8379
// fill $emit
8480
if (scriptSetupRanges.defineEmits) {
85-
yield `$emit: ${scriptSetupRanges.defineEmits.name ?? '__VLS_emit'},${newLine}`;
81+
yield `{} as { $emit: typeof ${scriptSetupRanges.defineEmits.name ?? `__VLS_emit`} }`;
8682
}
8783
}
8884

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ export function* generateComponentSelf(
1515
): Generator<Code> {
1616
if (options.sfc.scriptSetup && options.scriptSetupRanges) {
1717
yield `const __VLS_self = (await import('${options.vueCompilerOptions.lib}')).defineComponent({${newLine}`;
18-
yield `setup() {${newLine}`;
19-
yield `return {${newLine}`;
18+
yield `setup: () => ({${newLine}`;
2019
if (ctx.bypassDefineComponent) {
21-
yield* generateComponentSetupReturns(options.scriptSetupRanges);
20+
for (const code of generateComponentSetupReturns(options.scriptSetupRanges)) {
21+
yield `...${code},${newLine}`;
22+
}
2223
}
2324
// bindings
2425
const templateUsageVars = new Set([
@@ -36,8 +37,7 @@ export function* generateComponentSelf(
3637
yield ['', undefined, 0, { __linkedToken: token }];
3738
yield `${varName},${newLine}`;
3839
}
39-
yield `}${endOfLine}`; // return {
40-
yield `},${newLine}`; // setup() {
40+
yield `}),${newLine}`;
4141
if (options.sfc.scriptSetup && options.scriptSetupRanges && !ctx.bypassDefineComponent) {
4242
const emitOptionCodes = [...generateEmitsOption(options, options.scriptSetupRanges)];
4343
yield* emitOptionCodes;

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

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { generateStyleScopedClasses } from '../style/scopedClasses';
55
import { createTemplateCodegenContext, type TemplateCodegenContext } from '../template/context';
66
import { generateInterpolation } from '../template/interpolation';
77
import { generateStyleScopedClassReferences } from '../template/styleScopedClasses';
8-
import { endOfLine, newLine } from '../utils';
9-
import { generateIntersectMerge, generateSpreadMerge } from '../utils/merge';
8+
import { endOfLine, generateSfcBlockSection, newLine } from '../utils';
9+
import { generateSpreadMerge } from '../utils/merge';
1010
import type { ScriptCodegenContext } from './context';
1111
import type { ScriptCodegenOptions } from './index';
1212

@@ -49,48 +49,42 @@ function* generateTemplateElements(): Generator<Code> {
4949
}
5050

5151
function* generateTemplateComponents(options: ScriptCodegenOptions): Generator<Code> {
52-
const types: Code[] = [`typeof __VLS_ctx`];
52+
const types: string[] = [`typeof __VLS_ctx`];
5353

5454
if (options.sfc.script && options.scriptRanges?.exportDefault?.componentsOption) {
5555
const { componentsOption } = options.scriptRanges.exportDefault;
5656
yield `const __VLS_componentsOption = `;
57-
yield [
58-
options.sfc.script.content.slice(componentsOption.start, componentsOption.end),
59-
'script',
57+
yield generateSfcBlockSection(
58+
options.sfc.script,
6059
componentsOption.start,
60+
componentsOption.end,
6161
codeFeatures.navigation,
62-
];
62+
);
6363
yield endOfLine;
6464
types.push(`typeof __VLS_componentsOption`);
6565
}
6666

67-
yield `type __VLS_LocalComponents =`;
68-
yield* generateIntersectMerge(types);
69-
yield endOfLine;
70-
67+
yield `type __VLS_LocalComponents = ${types.join(` & `)}${endOfLine}`;
7168
yield `let __VLS_components!: __VLS_LocalComponents & __VLS_GlobalComponents${endOfLine}`;
7269
}
7370

7471
export function* generateTemplateDirectives(options: ScriptCodegenOptions): Generator<Code> {
75-
const types: Code[] = [`typeof __VLS_ctx`];
72+
const types: string[] = [`typeof __VLS_ctx`];
7673

7774
if (options.sfc.script && options.scriptRanges?.exportDefault?.directivesOption) {
7875
const { directivesOption } = options.scriptRanges.exportDefault;
7976
yield `const __VLS_directivesOption = `;
80-
yield [
81-
options.sfc.script.content.slice(directivesOption.start, directivesOption.end),
82-
'script',
77+
yield generateSfcBlockSection(
78+
options.sfc.script,
8379
directivesOption.start,
80+
directivesOption.end,
8481
codeFeatures.navigation,
85-
];
82+
);
8683
yield endOfLine;
8784
types.push(`__VLS_ResolveDirectives<typeof __VLS_directivesOption>`);
8885
}
8986

90-
yield `type __VLS_LocalDirectives =`;
91-
yield* generateIntersectMerge(types);
92-
yield endOfLine;
93-
87+
yield `type __VLS_LocalDirectives = ${types.join(` & `)}${endOfLine}`;
9488
yield `let __VLS_directives!: __VLS_LocalDirectives & __VLS_GlobalDirectives${endOfLine}`;
9589
}
9690

packages/tsc/tests/__snapshots__/dts.spec.ts.snap

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`vue-tsc-dts > Input: empty-component/component.vue, Output: empty-component/component.vue.d.ts 1`] = `
44
"declare const _default: import("vue").DefineComponent2<{
5-
setup(): void;
5+
setup(): {};
66
data(): {};
77
props: {};
88
computed: {};
@@ -28,7 +28,7 @@ export default _default;
2828

2929
exports[`vue-tsc-dts > Input: empty-component/custom-extension-component.cext, Output: empty-component/custom-extension-component.cext.d.ts 1`] = `
3030
"declare const _default: import("vue").DefineComponent2<{
31-
setup(): void;
31+
setup(): {};
3232
data(): {};
3333
props: {};
3434
computed: {};
@@ -114,7 +114,7 @@ type __VLS_PrettifyLocal<T> = {
114114
115115
exports[`vue-tsc-dts > Input: generic/main.vue, Output: generic/main.vue.d.ts 1`] = `
116116
"declare const _default: import("vue").DefineComponent2<{
117-
setup(): void;
117+
setup(): {};
118118
data(): {};
119119
props: {};
120120
computed: {};
@@ -216,7 +216,7 @@ export default _default;
216216
exports[`vue-tsc-dts > Input: reference-type-events/component.vue, Output: reference-type-events/component.vue.d.ts 1`] = `
217217
"import type { MyEvents } from './my-events';
218218
declare const _default: import("vue").DefineComponent2<{
219-
setup(): void;
219+
setup(): {};
220220
data(): {};
221221
props: {};
222222
computed: {};
@@ -312,7 +312,7 @@ type __VLS_ModelEmit = {
312312
'update:bar': [value: string | undefined];
313313
};
314314
declare const _default: import("vue").DefineComponent2<{
315-
setup(): void;
315+
setup(): {};
316316
data(): {};
317317
props: {};
318318
computed: {};
@@ -339,7 +339,7 @@ export default _default;
339339
exports[`vue-tsc-dts > Input: reference-type-props/component.vue, Output: reference-type-props/component.vue.d.ts 1`] = `
340340
"import { MyProps } from './my-props';
341341
declare const _default: import("vue").DefineComponent2<{
342-
setup(): void;
342+
setup(): {};
343343
data(): {};
344344
props: {};
345345
computed: {};
@@ -371,7 +371,7 @@ exports[`vue-tsc-dts > Input: reference-type-props/component-destructure.vue, Ou
371371
text: string;
372372
};
373373
declare const _default: import("vue").DefineComponent2<{
374-
setup(): void;
374+
setup(): {};
375375
data(): {};
376376
props: {};
377377
computed: {};
@@ -446,7 +446,7 @@ export default _default;
446446
447447
exports[`vue-tsc-dts > Input: reference-type-props/component-js-setup.vue, Output: reference-type-props/component-js-setup.vue.d.ts 1`] = `
448448
"declare const _default: import("vue").DefineComponent2<{
449-
setup(): void;
449+
setup(): {};
450450
data(): {};
451451
props: {
452452
foo: {
@@ -640,7 +640,7 @@ type __VLS_Slots = {} & {
640640
vbind?: (props: typeof __VLS_7) => any;
641641
};
642642
declare const __VLS_component: import("vue").DefineComponent2<{
643-
setup(): void;
643+
setup(): {};
644644
data(): {};
645645
props: {};
646646
computed: {};
@@ -686,7 +686,7 @@ type __VLS_Slots = {
686686
'no-bind': () => VNode[];
687687
};
688688
declare const __VLS_component: import("vue").DefineComponent2<{
689-
setup(): void;
689+
setup(): {};
690690
data(): {};
691691
props: {};
692692
computed: {};
@@ -735,7 +735,7 @@ type __VLS_Slots = {} & {
735735
vbind?: (props: typeof __VLS_7) => any;
736736
};
737737
declare const __VLS_component: import("vue").DefineComponent2<{
738-
setup(): void;
738+
setup(): {};
739739
data(): {};
740740
props: {};
741741
computed: {};

test-workspace/tsc/passedFixtures/vue3.4/components/main.vue

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ const ScriptSetupExact = defineComponent({
1616
change(..._payload: any[]) { },
1717
delete(..._payload: any[]) { },
1818
},
19-
setup() {},
2019
});
2120
// https://vuejs.org/api/sfc-script-setup.html#defineexpose
2221
const ScriptSetupExposeExact = defineComponent({
@@ -42,7 +41,6 @@ const ScriptSetupTypeOnlyExact = defineComponent({
4241
change(_id: number) { },
4342
update(_value: string) { },
4443
},
45-
setup() {},
4644
});
4745
// https://vuejs.org/api/sfc-script-setup.html#default-props-values-when-using-type-declaration
4846
const ScriptSetupDefaultPropsExact = defineComponent({
@@ -56,7 +54,6 @@ const ScriptSetupDefaultPropsExact = defineComponent({
5654
default: () => ['one', 'two']
5755
},
5856
},
59-
setup() {},
6057
});
6158
// vue 3.3 generic
6259
declare const ScriptSetupGenericExact: <T, >(

test-workspace/tsc/passedFixtures/vue3.4/defineModel/main.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const ScriptSetupExact = defineComponent({
2020
"update:f": (_: string) => void
2121
"update:g": (_: string | undefined) => void
2222
},
23-
setup() {},
2423
});
2524
2625
exactType(ScriptSetup, ScriptSetupExact);

test-workspace/tsc/passedFixtures/vue3/components/main.vue

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ const ScriptSetupExact = defineComponent({
1616
change(..._payload: any[]) { },
1717
delete(..._payload: any[]) { },
1818
},
19-
setup() {},
2019
});
2120
// https://vuejs.org/api/sfc-script-setup.html#defineexpose
2221
const ScriptSetupExposeExact = defineComponent({
@@ -39,7 +38,6 @@ const ScriptSetupTypeOnlyExact = defineComponent({
3938
(e: 'change', id: number): void;
4039
(e: 'update', value: string): void;
4140
},
42-
setup() {},
4341
});
4442
// https://vuejs.org/api/sfc-script-setup.html#default-props-values-when-using-type-declaration
4543
const ScriptSetupDefaultPropsExact = defineComponent({
@@ -51,7 +49,6 @@ const ScriptSetupDefaultPropsExact = defineComponent({
5149
msg: 'hello',
5250
labels: () => ['one', 'two']
5351
},
54-
setup() {},
5552
});
5653
// vue 3.3 generic
5754
declare const ScriptSetupGenericExact: <T, >(

test-workspace/tsc/passedFixtures/vue3/defineModel/main.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const ScriptSetupExact = defineComponent({
2020
'update:f': [f: string | undefined];
2121
'update:gG': [g: string | undefined];
2222
},
23-
setup() {},
2423
});
2524
2625
exactType(ScriptSetup, ScriptSetupExact);

0 commit comments

Comments
 (0)