Skip to content

Commit 945a6cd

Browse files
authored
fix(language-core): do not spread exposed object (#5526)
1 parent 4df0f6a commit 945a6cd

File tree

8 files changed

+73
-90
lines changed

8 files changed

+73
-90
lines changed

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

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { ScriptSetupRanges } from '../../parsers/scriptSetupRanges';
22
import type { Code, Sfc } from '../../types';
33
import { codeFeatures } from '../codeFeatures';
44
import { endOfLine, generateSfcBlockSection, newLine } from '../utils';
5+
import { generateIntersectMerge, generateSpreadMerge } from '../utils/merge';
56
import type { ScriptCodegenContext } from './context';
67
import type { ScriptCodegenOptions } from './index';
78

@@ -29,15 +30,23 @@ export function* generateComponent(
2930
}
3031

3132
yield `setup() {${newLine}`;
32-
yield `return {${newLine}`;
33+
const returns: Code[] = [];
3334
if (ctx.bypassDefineComponent) {
35+
yield* `const __VLS_returns = {${newLine}`;
3436
yield* generateComponentSetupReturns(scriptSetupRanges);
37+
yield `}${endOfLine}`;
38+
returns.push(`typeof __VLS_returns`);
3539
}
3640
if (scriptSetupRanges.defineExpose) {
37-
yield `...__VLS_exposed,${newLine}`;
41+
returns.push(`typeof __VLS_exposed`);
42+
}
43+
if (returns.length) {
44+
yield `return {} as `;
45+
yield* generateIntersectMerge(returns);
46+
yield endOfLine;
3847
}
39-
yield `}${endOfLine}`;
4048
yield `},${newLine}`;
49+
4150
if (!ctx.bypassDefineComponent) {
4251
const emitOptionCodes = [...generateEmitsOption(options, scriptSetupRanges)];
4352
yield* emitOptionCodes;
@@ -100,10 +109,14 @@ export function* generateEmitsOption(
100109
}
101110

102111
if (options.vueCompilerOptions.target >= 3.5 && typeOptionCodes.length) {
103-
yield* generateIntersectMerge('__typeEmits', typeOptionCodes);
112+
yield `__typeEmits: {} as `;
113+
yield* generateIntersectMerge(typeOptionCodes);
114+
yield `,${newLine}`;
104115
}
105116
else if (optionCodes.length) {
106-
yield* generateSpreadMerge('emits', optionCodes);
117+
yield `emits: `;
118+
yield* generateSpreadMerge(optionCodes);
119+
yield `,${newLine}`;
107120
}
108121
}
109122

@@ -159,36 +172,13 @@ export function* generatePropsOption(
159172
) {
160173
yield `__defaults: __VLS_withDefaultsArg,${newLine}`;
161174
}
162-
yield* generateSpreadMerge('__typeProps', typeOptionCodes);
175+
yield `__typeProps: `;
176+
yield* generateSpreadMerge(typeOptionCodes);
177+
yield `,${newLine}`;
163178
}
164179
if (useOption) {
165-
yield* generateSpreadMerge('props', getOptionCodes.map(fn => fn()));
166-
}
167-
}
168-
169-
function* generateIntersectMerge(key: string, codes: Code[]): Generator<Code> {
170-
yield `${key}: {} as `;
171-
yield codes[0];
172-
for (let i = 1; i < codes.length; i++) {
173-
yield ` & `;
174-
yield codes[i];
175-
}
176-
yield `,${newLine}`;
177-
}
178-
179-
function* generateSpreadMerge(key: string, codes: Code[]): Generator<Code> {
180-
yield `${key}: `;
181-
if (codes.length === 1) {
182-
yield codes[0];
183-
}
184-
else {
185-
yield `{${newLine}`;
186-
for (const code of codes) {
187-
yield `...`;
188-
yield code;
189-
yield `,${newLine}`;
190-
}
191-
yield `}`;
180+
yield `props: `;
181+
yield* generateSpreadMerge(getOptionCodes.map(fn => fn()));
182+
yield `,${newLine}`;
192183
}
193-
yield `,${newLine}`;
194184
}

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

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { createTemplateCodegenContext, type TemplateCodegenContext } from '../te
77
import { generateInterpolation } from '../template/interpolation';
88
import { generateStyleScopedClassReferences } from '../template/styleScopedClasses';
99
import { endOfLine, newLine } from '../utils';
10+
import { generateIntersectMerge, generateSpreadMerge } from '../utils/merge';
1011
import type { ScriptCodegenContext } from './context';
1112
import type { ScriptCodegenOptions } from './index';
1213

@@ -40,19 +41,8 @@ function* generateTemplateCtx(options: ScriptCodegenOptions): Generator<Code> {
4041
}
4142

4243
yield `const __VLS_ctx = `;
43-
if (exps.length === 1) {
44-
yield exps[0];
45-
yield `${endOfLine}`;
46-
}
47-
else {
48-
yield `{${newLine}`;
49-
for (const exp of exps) {
50-
yield `...`;
51-
yield exp;
52-
yield `,${newLine}`;
53-
}
54-
yield `}${endOfLine}`;
55-
}
44+
yield* generateSpreadMerge(exps);
45+
yield endOfLine;
5646
}
5747

5848
function* generateTemplateElements(): Generator<Code> {
@@ -76,10 +66,7 @@ function* generateTemplateComponents(options: ScriptCodegenOptions): Generator<C
7666
}
7767

7868
yield `type __VLS_LocalComponents =`;
79-
for (const type of types) {
80-
yield ` & `;
81-
yield type;
82-
}
69+
yield* generateIntersectMerge(types);
8370
yield endOfLine;
8471

8572
yield `let __VLS_components!: __VLS_LocalComponents & __VLS_GlobalComponents${endOfLine}`;
@@ -102,10 +89,7 @@ export function* generateTemplateDirectives(options: ScriptCodegenOptions): Gene
10289
}
10390

10491
yield `type __VLS_LocalDirectives =`;
105-
for (const type of types) {
106-
yield ` & `;
107-
yield type;
108-
}
92+
yield* generateIntersectMerge(types);
10993
yield endOfLine;
11094

11195
yield `let __VLS_directives!: __VLS_LocalDirectives & __VLS_GlobalDirectives${endOfLine}`;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { Code } from '../../types';
2+
import { newLine } from './index';
3+
4+
export function* generateIntersectMerge(codes: Code[]): Generator<Code> {
5+
yield codes[0];
6+
for (let i = 1; i < codes.length; i++) {
7+
yield ` & `;
8+
yield codes[i];
9+
}
10+
}
11+
12+
export function* generateSpreadMerge(codes: Code[]): Generator<Code> {
13+
if (codes.length === 1) {
14+
yield codes[0];
15+
}
16+
else {
17+
yield `{${newLine}`;
18+
for (const code of codes) {
19+
yield `...`;
20+
yield code;
21+
yield `,${newLine}`;
22+
}
23+
yield `}`;
24+
}
25+
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type __VLS_PrettifyLocal<T> = {
2929
3030
exports[`vue-tsc-dts > Input: empty-component/component.vue, Output: empty-component/component.vue.d.ts 1`] = `
3131
"declare const _default: import("vue").DefineComponent2<{
32-
setup(): {};
32+
setup(): void;
3333
data(): {};
3434
props: {};
3535
computed: {};
@@ -55,7 +55,7 @@ export default _default;
5555
5656
exports[`vue-tsc-dts > Input: empty-component/custom-extension-component.cext, Output: empty-component/custom-extension-component.cext.d.ts 1`] = `
5757
"declare const _default: import("vue").DefineComponent2<{
58-
setup(): {};
58+
setup(): void;
5959
data(): {};
6060
props: {};
6161
computed: {};
@@ -141,7 +141,7 @@ type __VLS_PrettifyLocal<T> = {
141141
142142
exports[`vue-tsc-dts > Input: generic/main.vue, Output: generic/main.vue.d.ts 1`] = `
143143
"declare const _default: import("vue").DefineComponent2<{
144-
setup(): {};
144+
setup(): void;
145145
data(): {};
146146
props: {};
147147
computed: {};
@@ -243,7 +243,7 @@ export default _default;
243243
exports[`vue-tsc-dts > Input: reference-type-events/component.vue, Output: reference-type-events/component.vue.d.ts 1`] = `
244244
"import type { MyEvents } from './my-events';
245245
declare const _default: import("vue").DefineComponent2<{
246-
setup(): {};
246+
setup(): void;
247247
data(): {};
248248
props: {};
249249
computed: {};
@@ -339,7 +339,7 @@ type __VLS_ModelEmit = {
339339
'update:bar': [value: string | undefined];
340340
};
341341
declare const _default: import("vue").DefineComponent2<{
342-
setup(): {};
342+
setup(): void;
343343
data(): {};
344344
props: {};
345345
computed: {};
@@ -366,7 +366,7 @@ export default _default;
366366
exports[`vue-tsc-dts > Input: reference-type-props/component.vue, Output: reference-type-props/component.vue.d.ts 1`] = `
367367
"import { MyProps } from './my-props';
368368
declare const _default: import("vue").DefineComponent2<{
369-
setup(): {};
369+
setup(): void;
370370
data(): {};
371371
props: {};
372372
computed: {};
@@ -398,7 +398,7 @@ exports[`vue-tsc-dts > Input: reference-type-props/component-destructure.vue, Ou
398398
text: string;
399399
};
400400
declare const _default: import("vue").DefineComponent2<{
401-
setup(): {};
401+
setup(): void;
402402
data(): {};
403403
props: {};
404404
computed: {};
@@ -473,7 +473,7 @@ export default _default;
473473
474474
exports[`vue-tsc-dts > Input: reference-type-props/component-js-setup.vue, Output: reference-type-props/component-js-setup.vue.d.ts 1`] = `
475475
"declare const _default: import("vue").DefineComponent2<{
476-
setup(): {};
476+
setup(): void;
477477
data(): {};
478478
props: {
479479
foo: {
@@ -667,7 +667,7 @@ type __VLS_Slots = {} & {
667667
vbind?: (props: typeof __VLS_7) => any;
668668
};
669669
declare const __VLS_component: import("vue").DefineComponent2<{
670-
setup(): {};
670+
setup(): void;
671671
data(): {};
672672
props: {};
673673
computed: {};
@@ -713,7 +713,7 @@ type __VLS_Slots = {
713713
'no-bind': () => VNode[];
714714
};
715715
declare const __VLS_component: import("vue").DefineComponent2<{
716-
setup(): {};
716+
setup(): void;
717717
data(): {};
718718
props: {};
719719
computed: {};
@@ -762,7 +762,7 @@ type __VLS_Slots = {} & {
762762
vbind?: (props: typeof __VLS_7) => any;
763763
};
764764
declare const __VLS_component: import("vue").DefineComponent2<{
765-
setup(): {};
765+
setup(): void;
766766
data(): {};
767767
props: {};
768768
computed: {};

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ const ScriptSetupExact = defineComponent({
1616
change(..._payload: any[]) { },
1717
delete(..._payload: any[]) { },
1818
},
19-
setup() {
20-
return {};
21-
},
19+
setup() {},
2220
});
2321
// https://vuejs.org/api/sfc-script-setup.html#defineexpose
2422
const ScriptSetupExposeExact = defineComponent({
@@ -44,9 +42,7 @@ const ScriptSetupTypeOnlyExact = defineComponent({
4442
change(_id: number) { },
4543
update(_value: string) { },
4644
},
47-
setup() {
48-
return {};
49-
},
45+
setup() {},
5046
});
5147
// https://vuejs.org/api/sfc-script-setup.html#default-props-values-when-using-type-declaration
5248
const ScriptSetupDefaultPropsExact = defineComponent({
@@ -60,9 +56,7 @@ const ScriptSetupDefaultPropsExact = defineComponent({
6056
default: () => ['one', 'two']
6157
},
6258
},
63-
setup() {
64-
return {};
65-
},
59+
setup() {},
6660
});
6761
// vue 3.3 generic
6862
declare const ScriptSetupGenericExact: <T, >(

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ const ScriptSetupExact = defineComponent({
2020
"update:f": (_: string) => void
2121
"update:g": (_: string | undefined) => void
2222
},
23-
setup() {
24-
return {};
25-
},
23+
setup() {},
2624
});
2725
2826
exactType(ScriptSetup, ScriptSetupExact);

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ const ScriptSetupExact = defineComponent({
1616
change(..._payload: any[]) { },
1717
delete(..._payload: any[]) { },
1818
},
19-
setup() {
20-
return {};
21-
},
19+
setup() {},
2220
});
2321
// https://vuejs.org/api/sfc-script-setup.html#defineexpose
2422
const ScriptSetupExposeExact = defineComponent({
@@ -41,9 +39,7 @@ const ScriptSetupTypeOnlyExact = defineComponent({
4139
(e: 'change', id: number): void;
4240
(e: 'update', value: string): void;
4341
},
44-
setup() {
45-
return {};
46-
},
42+
setup() {},
4743
});
4844
// https://vuejs.org/api/sfc-script-setup.html#default-props-values-when-using-type-declaration
4945
const ScriptSetupDefaultPropsExact = defineComponent({
@@ -55,9 +51,7 @@ const ScriptSetupDefaultPropsExact = defineComponent({
5551
msg: 'hello',
5652
labels: () => ['one', 'two']
5753
},
58-
setup() {
59-
return {};
60-
},
54+
setup() {},
6155
});
6256
// vue 3.3 generic
6357
declare const ScriptSetupGenericExact: <T, >(

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ const ScriptSetupExact = defineComponent({
2020
'update:f': [f: string | undefined];
2121
'update:gG': [g: string | undefined];
2222
},
23-
setup() {
24-
return {};
25-
},
23+
setup() {},
2624
});
2725
2826
exactType(ScriptSetup, ScriptSetupExact);

0 commit comments

Comments
 (0)