Skip to content

Commit 81a7819

Browse files
authoredAug 18, 2022
fix(compiler-sfc): rewriteDefault for class with decorators (#6320)
fix #6318
1 parent c1ee6ca commit 81a7819

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed
 

‎packages/compiler-sfc/__tests__/rewriteDefault.spec.ts

+50-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,56 @@ describe('compiler sfc: rewriteDefault', () => {
190190
).toMatchInlineSnapshot(`
191191
"/*
192192
export default class Foo {}*/
193-
const script = class Bar {}"
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"
194243
`)
195244
})
196245
})

‎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) {

0 commit comments

Comments
 (0)
Please sign in to comment.