Skip to content

Commit

Permalink
Handle as default in exports
Browse files Browse the repository at this point in the history
Fixes #58
  • Loading branch information
lydell committed Nov 19, 2020
1 parent 3906ec4 commit c30a09c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,10 +459,14 @@ function getSpecifierItems(tokens) {
// If the last specifier has no trailing comma we end up here. Move all
// trailing comments and whitespace from `.specifier` to `.after`, and
// comments and whitespace that don’t belong to the specifier to
// `result.after`.
// `result.after`. The last non-comment and non-whitespace token is usually
// an identifier, but in this case it’s a keyword:
//
// export { z, d as default } from "a"
case "specifier": {
const lastIdentifierIndex = findLastIndex(current.specifier, (token2) =>
isIdentifier(token2)
const lastIdentifierIndex = findLastIndex(
current.specifier,
(token2) => isIdentifier(token2) || isKeyword(token2)
);

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

function isKeyword(node) {
return node.type === "Keyword";
}

function isPunctuator(node, value) {
return node.type === "Punctuator" && node.value === value;
}
Expand Down
22 changes: 22 additions & 0 deletions test/exports.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,28 @@ const baseTests = (expect) => ({
errors: 1,
},

// Handling `as default` (issue #58).
{
code: `export { something, something as default } from './something'`,
output: (actual) => {
expect(actual).toMatchInlineSnapshot(
`export { something as default,something } from './something'`
);
},
errors: 1,
},

// Tricky `default` cases.
{
code: `export {default as default, default as def, default as fault} from "b"`,
output: (actual) => {
expect(actual).toMatchInlineSnapshot(
`export {default as def, default as default, default as fault} from "b"`
);
},
errors: 1,
},

// Test messageId, lines and columns.
{
code: input`
Expand Down

0 comments on commit c30a09c

Please # to comment.