Skip to content

Commit

Permalink
fix: Preceeding export keyword & unrelated default
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbrennanfr committed Jul 28, 2023
1 parent 49b6bd4 commit e6e26fb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/compiler-sfc/src/rewriteDefault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { parse, ParserPlugin } from '@babel/parser'
import MagicString from 'magic-string'

const defaultExportRE = /((?:^|\n|;)\s*)export(\s*)default/
const namedDefaultExportRE = /((?:^|\n|;)\s*)export(.+)(?:as)?(\s*)default/s
const namedDefaultExportRE = /((?:^|\n|;)\s*)export(.+)as(\s*)default/s
const objectWithDefaultExportRE = /((?:^|\n|;)\s*)export(\s*){(.+)default/s
const exportDefaultClassRE =
/((?:^|\n|;)\s*)export\s+default\s+class\s+([\w$]+)/

Expand Down Expand Up @@ -89,7 +90,7 @@ export function rewriteDefault(
}

export function hasDefaultExport(input: string): boolean {
return defaultExportRE.test(input) || namedDefaultExportRE.test(input)
return defaultExportRE.test(input) || namedDefaultExportRE.test(input) || objectWithDefaultExportRE.test(input)
}

function specifierEnd(
Expand Down
14 changes: 14 additions & 0 deletions packages/compiler-sfc/test/rewriteDefault.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,18 @@ describe('compiler sfc: rewriteDefault', () => {
const script = Bar"
`)
})

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

0 comments on commit e6e26fb

Please sign in to comment.