Skip to content

Commit

Permalink
fix: properly parse export default from
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Jun 4, 2020
1 parent 84f6abc commit 575e277
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 3 deletions.
19 changes: 16 additions & 3 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -1857,10 +1857,23 @@ export default class StatementParser extends ExpressionParser {
}
const next = this.nextTokenStart();
return (
const hasFrom = this.isUnparsedContextual(next, "from");
if (
this.input.charCodeAt(next) === charCodes.comma ||
this.isUnparsedContextual(next, "from")
);
(this.match(tt.name) && hasFrom)
) {
return true;
}
// lookahead again when `export default from` is seen
if (this.match(tt._default) && hasFrom) {
const nextAfterFrom = this.input.charCodeAt(
this.nextTokenStartSince(next + 4),
);
return (
nextAfterFrom === charCodes.quotationMark ||
nextAfterFrom === charCodes.apostrophe
);
}
}
parseExportFrom(node: N.ExportNamedDeclaration, expect?: boolean): void {
Expand Down
15 changes: 15 additions & 0 deletions packages/babel-parser/src/plugins/placeholders.js
Expand Up @@ -251,6 +251,21 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return super.parseExport(node);
}

isExportDefaultSpecifier(): boolean {
if (this.match(tt._default)) {
const next = this.nextTokenStart();
if (this.isUnparsedContextual(next, "from")) {
if (
this.input.substr(this.nextTokenStartSince(next + 4), 2) ===
tt.placeholder.label
) {
return true;
}
}
}
return super.isExportDefaultSpecifier();
}

maybeParseExportDefaultSpecifier(node: N.Node): boolean {
if (node.specifiers && node.specifiers.length > 0) {
// "export %%NAME%%" has already been parsed by #parseExport.
Expand Down
@@ -0,0 +1 @@
export default from (bar);
@@ -0,0 +1,33 @@
{
"type": "File",
"start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":26}},
"program": {
"type": "Program",
"start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":26}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExportDefaultDeclaration",
"start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":26}},
"declaration": {
"type": "CallExpression",
"start":15,"end":25,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":25}},
"callee": {
"type": "Identifier",
"start":15,"end":19,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":19},"identifierName":"from"},
"name": "from"
},
"arguments": [
{
"type": "Identifier",
"start":21,"end":24,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":24},"identifierName":"bar"},
"name": "bar"
}
]
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
export default from ?? 42;
@@ -0,0 +1,36 @@
{
"type": "File",
"start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":26}},
"program": {
"type": "Program",
"start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":26}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExportDefaultDeclaration",
"start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":26}},
"declaration": {
"type": "LogicalExpression",
"start":15,"end":25,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":25}},
"left": {
"type": "Identifier",
"start":15,"end":19,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":19},"identifierName":"from"},
"name": "from"
},
"operator": "??",
"right": {
"type": "NumericLiteral",
"start":23,"end":25,"loc":{"start":{"line":1,"column":23},"end":{"line":1,"column":25}},
"extra": {
"rawValue": 42,
"raw": "42"
},
"value": 42
}
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
export default from;
@@ -0,0 +1,22 @@
{
"type": "File",
"start":0,"end":20,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}},
"program": {
"type": "Program",
"start":0,"end":20,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExportDefaultDeclaration",
"start":0,"end":20,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}},
"declaration": {
"type": "Identifier",
"start":15,"end":19,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":19},"identifierName":"from"},
"name": "from"
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
export default from;
@@ -0,0 +1,4 @@
{
"plugins": ["exportDefaultFrom"],
"sourceType": "module"
}
@@ -0,0 +1,22 @@
{
"type": "File",
"start":0,"end":20,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}},
"program": {
"type": "Program",
"start":0,"end":20,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExportDefaultDeclaration",
"start":0,"end":20,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}},
"declaration": {
"type": "Identifier",
"start":15,"end":19,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":19},"identifierName":"from"},
"name": "from"
}
}
],
"directives": []
}
}

0 comments on commit 575e277

Please sign in to comment.