[dynamic-import] Implementing import() syntax (#163)

This commit is contained in:
Jordan Gensler
2016-10-14 14:54:21 -04:00
committed by Henry Zhu
parent 4c445fd5f8
commit c63c1bc728
27 changed files with 1475 additions and 1 deletions

View File

@@ -302,6 +302,9 @@ pp.parseSubscripts = function (base, startPos, startLoc, noCalls) {
let node = this.startNodeAt(startPos, startLoc);
node.callee = base;
node.arguments = this.parseCallExpressionArguments(tt.parenR, possibleAsync);
if (node.callee.type === "Import" && node.arguments.length !== 1) {
this.raise(node.start, "import() requires exactly one argument");
}
base = this.finishNode(node, "CallExpression");
if (possibleAsync && this.shouldParseAsyncArrow()) {
@@ -387,6 +390,16 @@ pp.parseExprAtom = function (refShorthandDefaultPos) {
}
return this.finishNode(node, "Super");
case tt._import:
if (!this.hasPlugin("dynamicImport")) this.unexpected();
node = this.startNode();
this.next();
if (!this.match(tt.parenL)) {
this.unexpected();
}
return this.finishNode(node, "Import");
case tt._this:
node = this.startNode();
this.next();

View File

@@ -98,6 +98,8 @@ pp.parseStatement = function (declaration, topLevel) {
case tt.semi: return this.parseEmptyStatement(node);
case tt._export:
case tt._import:
if (this.hasPlugin("dynamicImport") && this.lookahead().type === tt.parenL) break;
if (!this.options.allowImportExportEverywhere) {
if (!topLevel) {
this.raise(this.state.start, "'import' and 'export' may only appear at the top level");