Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-sfc): make sure rewriteDefault get correct result when use decorator before export default (fix #6318) #6320

Merged
merged 1 commit into from Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 50 additions & 1 deletion packages/compiler-sfc/__tests__/rewriteDefault.spec.ts
Expand Up @@ -190,7 +190,56 @@ describe('compiler sfc: rewriteDefault', () => {
).toMatchInlineSnapshot(`
"/*
export default class Foo {}*/
const script = class Bar {}"
class Bar {}
const script = Bar"
`)
})

test('@Component\nexport default class', async () => {
expect(rewriteDefault(`@Component\nexport default class Foo {}`, 'script'))
.toMatchInlineSnapshot(`
"@Component
class Foo {}
const script = Foo"
`)
})

test('@Component\nexport default class w/ comments', async () => {
expect(
rewriteDefault(`// export default\n@Component\nexport default class Foo {}`, 'script')
).toMatchInlineSnapshot(`
"// export default
@Component
class Foo {}
const script = Foo"
`)
})

test('@Component\nexport default class w/ comments 2', async () => {
expect(
rewriteDefault(
`export default {}\n` + `// @Component\n// export default class Foo {}`,
'script'
)
).toMatchInlineSnapshot(`
"const script = {}
// @Component
// export default class Foo {}"
`)
})

test('@Component\nexport default class w/ comments 3', async () => {
expect(
rewriteDefault(
`/*\n@Component\nexport default class Foo {}*/\n` + `export default class Bar {}`,
'script'
)
).toMatchInlineSnapshot(`
"/*
@Component
export default class Foo {}*/
class Bar {}
const script = Bar"
`)
})
})
7 changes: 6 additions & 1 deletion packages/compiler-sfc/src/rewriteDefault.ts
Expand Up @@ -42,7 +42,12 @@ export function rewriteDefault(
}).program.body
ast.forEach(node => {
if (node.type === 'ExportDefaultDeclaration') {
s.overwrite(node.start!, node.declaration.start!, `const ${as} = `)
if (node.declaration.type === 'ClassDeclaration') {
s.overwrite(node.start!, node.declaration.id.start!, `class `)
linshuohao marked this conversation as resolved.
Show resolved Hide resolved
s.append(`\nconst ${as} = ${node.declaration.id.name}`)
} else {
s.overwrite(node.start!, node.declaration.start!, `const ${as} = `)
linshuohao marked this conversation as resolved.
Show resolved Hide resolved
}
}
if (node.type === 'ExportNamedDeclaration') {
for (const specifier of node.specifiers) {
Expand Down