From a4922ce5d26f1a4f939e9ed3c7737894f9ea38b3 Mon Sep 17 00:00:00 2001 From: Naveen Jain Date: Thu, 1 Feb 2018 19:37:20 +0530 Subject: [PATCH] [BugFix] Compile export default from --- packages/babylon/src/parser/statement.js | 43 ++++++++++++++----- .../invalid-export-default/options.json | 2 +- .../_no-plugin/export-default/options.json | 2 +- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/packages/babylon/src/parser/statement.js b/packages/babylon/src/parser/statement.js index 39f112595de5..62ef5a1107b1 100644 --- a/packages/babylon/src/parser/statement.js +++ b/packages/babylon/src/parser/statement.js @@ -1310,21 +1310,44 @@ export default class StatementParser extends ExpressionParser { this.parseExportStar(node); if (node.type === "ExportAllDeclaration") return node; } else if (this.isExportDefaultSpecifier()) { - this.expectPlugin("exportDefaultFrom"); const specifier = this.startNode(); specifier.exported = this.parseIdentifier(true); + let isDefaultfrom = false; + if (specifier.exported.name === "default" && this.isContextual("from")) { + isDefaultfrom = true; + const lookaheadState = this.lookahead(); + if ( + lookaheadState.value === "=" || + lookaheadState.type === tt.semi || + lookaheadState.type === tt.eof || + lookaheadState.type === tt.braceR || + lineBreak.test( + this.input.slice(lookaheadState.lastTokEnd, lookaheadState.start), + ) + ) { + node.declaration = this.parseMaybeAssign(); + this.checkExport(node, true, true); + return this.finishNode(node, "ExportDefaultDeclaration"); + } + } + this.expectPlugin("exportDefaultFrom"); const specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")]; node.specifiers = specifiers; - if (this.match(tt.comma) && this.lookahead().type === tt.star) { - this.expect(tt.comma); - const specifier = this.startNode(); - this.expect(tt.star); - this.expectContextual("as"); - specifier.exported = this.parseIdentifier(); - specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier")); - } else { - this.parseExportSpecifiersMaybe(node); + if (!isDefaultfrom) { + if (this.match(tt.comma) && this.lookahead().type === tt.star) { + this.expect(tt.comma); + const specifier = this.startNode(); + this.expect(tt.star); + this.expectContextual("as"); + specifier.exported = this.parseIdentifier(); + specifiers.push( + this.finishNode(specifier, "ExportNamespaceSpecifier"), + ); + } else { + this.parseExportSpecifiersMaybe(node); + } } + this.parseExportFrom(node, true); } else if (this.eat(tt._default)) { // export default ... diff --git a/packages/babylon/test/fixtures/esprima/es2015-export-declaration/invalid-export-default/options.json b/packages/babylon/test/fixtures/esprima/es2015-export-declaration/invalid-export-default/options.json index 689ff73f73f7..8ba464d69650 100644 --- a/packages/babylon/test/fixtures/esprima/es2015-export-declaration/invalid-export-default/options.json +++ b/packages/babylon/test/fixtures/esprima/es2015-export-declaration/invalid-export-default/options.json @@ -1,3 +1,3 @@ { - "throws": "This experimental syntax requires enabling the parser plugin: 'exportDefaultFrom' (1:7)" + "throws": "This experimental syntax requires enabling the parser plugin: 'exportDefaultFrom' (1:15)" } diff --git a/packages/babylon/test/fixtures/experimental/_no-plugin/export-default/options.json b/packages/babylon/test/fixtures/experimental/_no-plugin/export-default/options.json index e306a4f34cbf..98c70928d7ab 100644 --- a/packages/babylon/test/fixtures/experimental/_no-plugin/export-default/options.json +++ b/packages/babylon/test/fixtures/experimental/_no-plugin/export-default/options.json @@ -1,4 +1,4 @@ { - "throws": "This experimental syntax requires enabling the parser plugin: 'exportDefaultFrom' (1:7)", + "throws": "This experimental syntax requires enabling the parser plugin: 'exportDefaultFrom' (1:9)", "plugins": [] }