Skip to content

Commit 5221d4d

Browse files
authored
fix(compiler-sfc): rewriteDefault for class with decorators (#12747)
1 parent 89a6b5e commit 5221d4d

File tree

2 files changed

+251
-1
lines changed

2 files changed

+251
-1
lines changed

packages/compiler-sfc/src/rewriteDefault.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ export function rewriteDefault(
4242
}).program.body
4343
ast.forEach(node => {
4444
if (node.type === 'ExportDefaultDeclaration') {
45-
s.overwrite(node.start!, node.declaration.start!, `const ${as} = `)
45+
if (node.declaration.type === 'ClassDeclaration') {
46+
s.overwrite(node.start!, node.declaration.id.start!, `class `)
47+
s.append(`\nconst ${as} = ${node.declaration.id.name}`)
48+
} else {
49+
s.overwrite(node.start!, node.declaration.start!, `const ${as} = `)
50+
}
4651
}
4752
if (node.type === 'ExportNamedDeclaration') {
4853
for (const specifier of node.specifiers) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
import { rewriteDefault } from '../src'
2+
3+
describe('compiler sfc: rewriteDefault', () => {
4+
test('without export default', () => {
5+
expect(rewriteDefault(`export a = {}`, 'script')).toMatchInlineSnapshot(`
6+
"export a = {}
7+
const script = {}"
8+
`)
9+
})
10+
11+
test('rewrite export default', () => {
12+
expect(
13+
rewriteDefault(`export default {}`, 'script')
14+
).toMatchInlineSnapshot(`"const script = {}"`)
15+
})
16+
17+
test('rewrite export named default', () => {
18+
expect(
19+
rewriteDefault(
20+
`const a = 1 \n export { a as b, a as default, a as c}`,
21+
'script'
22+
)
23+
).toMatchInlineSnapshot(`
24+
"const a = 1
25+
export { a as b, a as c}
26+
const script = a"
27+
`)
28+
29+
expect(
30+
rewriteDefault(
31+
`const a = 1 \n export { a as b, a as default , a as c}`,
32+
'script'
33+
)
34+
).toMatchInlineSnapshot(`
35+
"const a = 1
36+
export { a as b, a as c}
37+
const script = a"
38+
`)
39+
})
40+
41+
test('w/ comments', async () => {
42+
expect(rewriteDefault(`// export default\nexport default {}`, 'script'))
43+
.toMatchInlineSnapshot(`
44+
"// export default
45+
const script = {}"
46+
`)
47+
})
48+
49+
test('export named default multiline', () => {
50+
expect(
51+
rewriteDefault(`let App = {}\n export {\nApp as default\n}`, '_sfc_main')
52+
).toMatchInlineSnapshot(`
53+
"let App = {}
54+
export {
55+
56+
}
57+
const _sfc_main = App"
58+
`)
59+
})
60+
61+
test('export named default multiline /w comments', () => {
62+
expect(
63+
rewriteDefault(
64+
`const a = 1 \n export {\n a as b,\n a as default,\n a as c}\n` +
65+
`// export { myFunction as default }`,
66+
'script'
67+
)
68+
).toMatchInlineSnapshot(`
69+
"const a = 1
70+
export {
71+
a as b,
72+
73+
a as c}
74+
// export { myFunction as default }
75+
const script = a"
76+
`)
77+
78+
expect(
79+
rewriteDefault(
80+
`const a = 1 \n export {\n a as b,\n a as default ,\n a as c}\n` +
81+
`// export { myFunction as default }`,
82+
'script'
83+
)
84+
).toMatchInlineSnapshot(`
85+
"const a = 1
86+
export {
87+
a as b,
88+
89+
a as c}
90+
// export { myFunction as default }
91+
const script = a"
92+
`)
93+
})
94+
95+
test(`export { default } from '...'`, async () => {
96+
expect(
97+
rewriteDefault(`export { default, foo } from './index.js'`, 'script')
98+
).toMatchInlineSnapshot(`
99+
"import { default as __VUE_DEFAULT__ } from './index.js'
100+
export { foo } from './index.js'
101+
const script = __VUE_DEFAULT__"
102+
`)
103+
104+
expect(
105+
rewriteDefault(`export { default , foo } from './index.js'`, 'script')
106+
).toMatchInlineSnapshot(`
107+
"import { default as __VUE_DEFAULT__ } from './index.js'
108+
export { foo } from './index.js'
109+
const script = __VUE_DEFAULT__"
110+
`)
111+
112+
expect(
113+
rewriteDefault(`export { foo, default } from './index.js'`, 'script')
114+
).toMatchInlineSnapshot(`
115+
"import { default as __VUE_DEFAULT__ } from './index.js'
116+
export { foo, } from './index.js'
117+
const script = __VUE_DEFAULT__"
118+
`)
119+
120+
expect(
121+
rewriteDefault(
122+
`export { foo as default, bar } from './index.js'`,
123+
'script'
124+
)
125+
).toMatchInlineSnapshot(`
126+
"import { foo } from './index.js'
127+
export { bar } from './index.js'
128+
const script = foo"
129+
`)
130+
131+
expect(
132+
rewriteDefault(
133+
`export { foo as default , bar } from './index.js'`,
134+
'script'
135+
)
136+
).toMatchInlineSnapshot(`
137+
"import { foo } from './index.js'
138+
export { bar } from './index.js'
139+
const script = foo"
140+
`)
141+
142+
expect(
143+
rewriteDefault(
144+
`export { bar, foo as default } from './index.js'`,
145+
'script'
146+
)
147+
).toMatchInlineSnapshot(`
148+
"import { foo } from './index.js'
149+
export { bar, } from './index.js'
150+
const script = foo"
151+
`)
152+
})
153+
154+
test('export default class', async () => {
155+
expect(rewriteDefault(`export default class Foo {}`, 'script'))
156+
.toMatchInlineSnapshot(`
157+
"class Foo {}
158+
const script = Foo"
159+
`)
160+
})
161+
162+
test('export default class w/ comments', async () => {
163+
expect(
164+
rewriteDefault(`// export default\nexport default class Foo {}`, 'script')
165+
).toMatchInlineSnapshot(`
166+
"// export default
167+
class Foo {}
168+
const script = Foo"
169+
`)
170+
})
171+
172+
test('export default class w/ comments 2', async () => {
173+
expect(
174+
rewriteDefault(
175+
`export default {}\n` + `// export default class Foo {}`,
176+
'script'
177+
)
178+
).toMatchInlineSnapshot(`
179+
"const script = {}
180+
// export default class Foo {}"
181+
`)
182+
})
183+
184+
test('export default class w/ comments 3', async () => {
185+
expect(
186+
rewriteDefault(
187+
`/*\nexport default class Foo {}*/\n` + `export default class Bar {}`,
188+
'script'
189+
)
190+
).toMatchInlineSnapshot(`
191+
"/*
192+
export default class Foo {}*/
193+
class Bar {}
194+
const script = Bar"
195+
`)
196+
})
197+
198+
test('@Component\nexport default class', async () => {
199+
expect(rewriteDefault(`@Component\nexport default class Foo {}`, 'script'))
200+
.toMatchInlineSnapshot(`
201+
"@Component
202+
class Foo {}
203+
const script = Foo"
204+
`)
205+
})
206+
207+
test('@Component\nexport default class w/ comments', async () => {
208+
expect(
209+
rewriteDefault(`// export default\n@Component\nexport default class Foo {}`, 'script')
210+
).toMatchInlineSnapshot(`
211+
"// export default
212+
@Component
213+
class Foo {}
214+
const script = Foo"
215+
`)
216+
})
217+
218+
test('@Component\nexport default class w/ comments 2', async () => {
219+
expect(
220+
rewriteDefault(
221+
`export default {}\n` + `// @Component\n// export default class Foo {}`,
222+
'script'
223+
)
224+
).toMatchInlineSnapshot(`
225+
"const script = {}
226+
// @Component
227+
// export default class Foo {}"
228+
`)
229+
})
230+
231+
test('@Component\nexport default class w/ comments 3', async () => {
232+
expect(
233+
rewriteDefault(
234+
`/*\n@Component\nexport default class Foo {}*/\n` + `export default class Bar {}`,
235+
'script'
236+
)
237+
).toMatchInlineSnapshot(`
238+
"/*
239+
@Component
240+
export default class Foo {}*/
241+
class Bar {}
242+
const script = Bar"
243+
`)
244+
})
245+
})

0 commit comments

Comments
 (0)