Skip to content

Commit

Permalink
fix: correctly test export default
Browse files Browse the repository at this point in the history
fix #319
  • Loading branch information
qmhc committed Apr 15, 2024
1 parent a191d3a commit b84c6e2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/transform.ts
Expand Up @@ -261,6 +261,17 @@ export function hasExportDefault(content: string) {
for (const element of node.exportClause.elements) {
if (element.name.escapedText === 'default') {
has = true
break
}
}
} else if ('modifiers' in node && Array.isArray(node.modifiers) && node.modifiers.length > 1) {
for (let i = 0, len = node.modifiers.length; i < len; ++i) {
if (
node.modifiers[i].kind === ts.SyntaxKind.ExportKeyword &&
node.modifiers[i + 1]?.kind === ts.SyntaxKind.DefaultKeyword
) {
has = true
break
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion tests/transform.spec.ts
Expand Up @@ -172,6 +172,14 @@ describe('transform tests', () => {
expect(hasExportDefault("export { foo, sdk } from './sdk'")).toBe(false)
expect(hasExportDefault("export { foo as baz, sdk } from './sdk'")).toBe(false)
expect(hasExportDefault("export { default } from './sdk'")).toBe(true)
expect(hasExportDefault("export { sdkas, default } from './sdk'")).toBe(true)
expect(hasExportDefault("export { sdk, default } from './sdk'")).toBe(true)
expect(hasExportDefault("export { sdk, default as baz } from './sdk'")).toBe(false)
expect(hasExportDefault('export default class Test {}')).toBe(true)
expect(hasExportDefault('export class Test {}')).toBe(false)
expect(hasExportDefault('export default function test() {}')).toBe(true)
expect(hasExportDefault('export function test() {}')).toBe(false)
expect(hasExportDefault('const a = 1\nexport default a')).toBe(true)
expect(hasExportDefault('const a = 1\nexport { a }')).toBe(false)
expect(hasExportDefault('const a = 1\nexport { a as default }')).toBe(true)
})
})

0 comments on commit b84c6e2

Please sign in to comment.