Skip to content

Commit b84c6e2

Browse files
committed
fix: correctly test export default
fix #319
1 parent a191d3a commit b84c6e2

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/transform.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,17 @@ export function hasExportDefault(content: string) {
261261
for (const element of node.exportClause.elements) {
262262
if (element.name.escapedText === 'default') {
263263
has = true
264+
break
265+
}
266+
}
267+
} else if ('modifiers' in node && Array.isArray(node.modifiers) && node.modifiers.length > 1) {
268+
for (let i = 0, len = node.modifiers.length; i < len; ++i) {
269+
if (
270+
node.modifiers[i].kind === ts.SyntaxKind.ExportKeyword &&
271+
node.modifiers[i + 1]?.kind === ts.SyntaxKind.DefaultKeyword
272+
) {
273+
has = true
274+
break
264275
}
265276
}
266277
}

tests/transform.spec.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,14 @@ describe('transform tests', () => {
172172
expect(hasExportDefault("export { foo, sdk } from './sdk'")).toBe(false)
173173
expect(hasExportDefault("export { foo as baz, sdk } from './sdk'")).toBe(false)
174174
expect(hasExportDefault("export { default } from './sdk'")).toBe(true)
175-
expect(hasExportDefault("export { sdkas, default } from './sdk'")).toBe(true)
175+
expect(hasExportDefault("export { sdk, default } from './sdk'")).toBe(true)
176+
expect(hasExportDefault("export { sdk, default as baz } from './sdk'")).toBe(false)
177+
expect(hasExportDefault('export default class Test {}')).toBe(true)
178+
expect(hasExportDefault('export class Test {}')).toBe(false)
179+
expect(hasExportDefault('export default function test() {}')).toBe(true)
180+
expect(hasExportDefault('export function test() {}')).toBe(false)
181+
expect(hasExportDefault('const a = 1\nexport default a')).toBe(true)
182+
expect(hasExportDefault('const a = 1\nexport { a }')).toBe(false)
183+
expect(hasExportDefault('const a = 1\nexport { a as default }')).toBe(true)
176184
})
177185
})

0 commit comments

Comments
 (0)