Skip to content

Commit 7e4f0a8

Browse files
committed
fix(compiler-sfc): generate valid TS in script and script setup co-usage with TS
fix #5094
1 parent ea1fcfb commit 7e4f0a8

File tree

3 files changed

+140
-104
lines changed

3 files changed

+140
-104
lines changed

packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap

+50-29
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ exports[`SFC compile <script setup> <script> and <script setup> co-usage script
44
"import { x } from './x'
55
66
export const n = 1
7+
8+
const __default__ = {}
79
8-
export default {
10+
export default /*#__PURE__*/Object.assign(__default__, {
911
setup(__props, { expose }) {
1012
expose();
1113
@@ -14,13 +16,15 @@ export default {
1416
return { n, x }
1517
}
1618
17-
}"
19+
})"
1820
`;
1921
2022
exports[`SFC compile <script setup> <script> and <script setup> co-usage script setup first 1`] = `
21-
"import { x } from './x'
23+
"export const n = 1
24+
const __default__ = {}
25+
import { x } from './x'
2226
23-
export default {
27+
export default /*#__PURE__*/Object.assign(__default__, {
2428
setup(__props, { expose }) {
2529
expose();
2630
@@ -29,29 +33,48 @@ export default {
2933
return { n, x }
3034
}
3135
32-
}
33-
export const n = 1"
36+
})"
3437
`;
3538
3639
exports[`SFC compile <script setup> <script> and <script setup> co-usage script setup first, lang="ts", script block content export default 1`] = `
3740
"import { defineComponent as _defineComponent } from 'vue'
38-
import { x } from './x'
41+
42+
const __default__ = {
43+
name: \\"test\\"
44+
}
45+
import { x } from './x'
3946
40-
function setup(__props, { expose }) {
47+
export default /*#__PURE__*/_defineComponent({
48+
...__default__,
49+
setup(__props, { expose }) {
50+
expose();
4151
4252
x()
4353
4454
return { x }
4555
}
4656
57+
})"
58+
`;
4759
48-
const __default__ = {
49-
name: \\"test\\"
50-
}
60+
exports[`SFC compile <script setup> <script> and <script setup> co-usage script setup first, named default export 1`] = `
61+
"export const n = 1
62+
const def = {}
5163
52-
export default /*#__PURE__*/_defineComponent({
53-
...__default__,
54-
setup})"
64+
65+
const __default__ = def
66+
import { x } from './x'
67+
68+
export default /*#__PURE__*/Object.assign(__default__, {
69+
setup(__props, { expose }) {
70+
expose();
71+
72+
x()
73+
74+
return { n, def, x }
75+
}
76+
77+
})"
5578
`;
5679
5780
exports[`SFC compile <script setup> <script> and <script setup> co-usage spaces in ExportDefaultDeclaration node with many spaces and newline 1`] = `
@@ -62,16 +85,15 @@ exports[`SFC compile <script setup> <script> and <script setup> co-usage spaces
6285
some:'option'
6386
}
6487
65-
function setup(__props, { expose }) {
88+
export default /*#__PURE__*/Object.assign(__default__, {
89+
setup(__props, { expose }) {
90+
expose();
6691
6792
x()
6893
6994
return { n, x }
7095
}
7196
72-
73-
export default /*#__PURE__*/ Object.assign(__default__, {
74-
setup
7597
})"
7698
`;
7799
@@ -83,16 +105,15 @@ exports[`SFC compile <script setup> <script> and <script setup> co-usage spaces
83105
some:'option'
84106
}
85107
86-
function setup(__props, { expose }) {
108+
export default /*#__PURE__*/Object.assign(__default__, {
109+
setup(__props, { expose }) {
110+
expose();
87111
88112
x()
89113
90114
return { n, x }
91115
}
92116
93-
94-
export default /*#__PURE__*/ Object.assign(__default__, {
95-
setup
96117
})"
97118
`;
98119
@@ -980,7 +1001,12 @@ return () => {}
9801001
`;
9811002
9821003
exports[`SFC compile <script setup> should expose top level declarations 1`] = `
983-
"import { x } from './x'
1004+
"import { xx } from './x'
1005+
let aa = 1
1006+
const bb = 2
1007+
function cc() {}
1008+
class dd {}
1009+
import { x } from './x'
9841010
9851011
export default {
9861012
setup(__props, { expose }) {
@@ -994,12 +1020,7 @@ export default {
9941020
return { aa, bb, cc, dd, a, b, c, d, xx, x }
9951021
}
9961022
997-
}
998-
import { xx } from './x'
999-
let aa = 1
1000-
const bb = 2
1001-
function cc() {}
1002-
class dd {}"
1023+
}"
10031024
`;
10041025
10051026
exports[`SFC compile <script setup> with TypeScript const Enum 1`] = `

packages/compiler-sfc/__tests__/compileScript.spec.ts

+55-37
Original file line numberDiff line numberDiff line change
@@ -169,47 +169,12 @@ defineExpose({ foo: 123 })
169169
})
170170

171171
describe('<script> and <script setup> co-usage', () => {
172-
describe('spaces in ExportDefaultDeclaration node', () => {
173-
// #4371
174-
test('with many spaces and newline', () => {
175-
// #4371
176-
const { content } = compile(`
177-
<script>
178-
export const n = 1
179-
export default
180-
{
181-
some:'option'
182-
}
183-
</script>
184-
<script setup>
185-
import { x } from './x'
186-
x()
187-
</script>
188-
`)
189-
assertCode(content)
190-
})
191-
192-
test('with minimal spaces', () => {
193-
const { content } = compile(`
194-
<script>
195-
export const n = 1
196-
export default{
197-
some:'option'
198-
}
199-
</script>
200-
<script setup>
201-
import { x } from './x'
202-
x()
203-
</script>
204-
`)
205-
assertCode(content)
206-
})
207-
})
208-
209172
test('script first', () => {
210173
const { content } = compile(`
211174
<script>
212175
export const n = 1
176+
177+
export default {}
213178
</script>
214179
<script setup>
215180
import { x } from './x'
@@ -227,6 +192,22 @@ defineExpose({ foo: 123 })
227192
</script>
228193
<script>
229194
export const n = 1
195+
export default {}
196+
</script>
197+
`)
198+
assertCode(content)
199+
})
200+
201+
test('script setup first, named default export', () => {
202+
const { content } = compile(`
203+
<script setup>
204+
import { x } from './x'
205+
x()
206+
</script>
207+
<script>
208+
export const n = 1
209+
const def = {}
210+
export { def as default }
230211
</script>
231212
`)
232213
assertCode(content)
@@ -249,6 +230,43 @@ defineExpose({ foo: 123 })
249230
expect(content).toMatch(/const __default__[\S\s]*\.\.\.__default__/m)
250231
assertCode(content)
251232
})
233+
234+
describe('spaces in ExportDefaultDeclaration node', () => {
235+
// #4371
236+
test('with many spaces and newline', () => {
237+
// #4371
238+
const { content } = compile(`
239+
<script>
240+
export const n = 1
241+
export default
242+
{
243+
some:'option'
244+
}
245+
</script>
246+
<script setup>
247+
import { x } from './x'
248+
x()
249+
</script>
250+
`)
251+
assertCode(content)
252+
})
253+
254+
test('with minimal spaces', () => {
255+
const { content } = compile(`
256+
<script>
257+
export const n = 1
258+
export default{
259+
some:'option'
260+
}
261+
</script>
262+
<script setup>
263+
import { x } from './x'
264+
x()
265+
</script>
266+
`)
267+
assertCode(content)
268+
})
269+
})
252270
})
253271

254272
describe('imports', () => {

0 commit comments

Comments
 (0)