Skip to content

Commit c30a09c

Browse files
committed
Handle as default in exports
Fixes #58
1 parent 3906ec4 commit c30a09c

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/shared.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,10 +459,14 @@ function getSpecifierItems(tokens) {
459459
// If the last specifier has no trailing comma we end up here. Move all
460460
// trailing comments and whitespace from `.specifier` to `.after`, and
461461
// comments and whitespace that don’t belong to the specifier to
462-
// `result.after`.
462+
// `result.after`. The last non-comment and non-whitespace token is usually
463+
// an identifier, but in this case it’s a keyword:
464+
//
465+
// export { z, d as default } from "a"
463466
case "specifier": {
464-
const lastIdentifierIndex = findLastIndex(current.specifier, (token2) =>
465-
isIdentifier(token2)
467+
const lastIdentifierIndex = findLastIndex(
468+
current.specifier,
469+
(token2) => isIdentifier(token2) || isKeyword(token2)
466470
);
467471

468472
const specifier = current.specifier.slice(0, lastIdentifierIndex + 1);
@@ -767,6 +771,10 @@ function isIdentifier(node) {
767771
return node.type === "Identifier";
768772
}
769773

774+
function isKeyword(node) {
775+
return node.type === "Keyword";
776+
}
777+
770778
function isPunctuator(node, value) {
771779
return node.type === "Punctuator" && node.value === value;
772780
}

test/exports.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,28 @@ const baseTests = (expect) => ({
401401
errors: 1,
402402
},
403403

404+
// Handling `as default` (issue #58).
405+
{
406+
code: `export { something, something as default } from './something'`,
407+
output: (actual) => {
408+
expect(actual).toMatchInlineSnapshot(
409+
`export { something as default,something } from './something'`
410+
);
411+
},
412+
errors: 1,
413+
},
414+
415+
// Tricky `default` cases.
416+
{
417+
code: `export {default as default, default as def, default as fault} from "b"`,
418+
output: (actual) => {
419+
expect(actual).toMatchInlineSnapshot(
420+
`export {default as def, default as default, default as fault} from "b"`
421+
);
422+
},
423+
errors: 1,
424+
},
425+
404426
// Test messageId, lines and columns.
405427
{
406428
code: input`

0 commit comments

Comments
 (0)