diff --git a/acorn.js b/acorn.js index add13940b2..01d54b4870 100644 --- a/acorn.js +++ b/acorn.js @@ -2785,20 +2785,18 @@ if (this.type === tt.name) { // import defaultObj, { x, y as z } from '...' var node = this.startNode(); - node.id = this.parseIdent(); - this.checkLVal(node.id, true); - node.name = null; - node['default'] = true; - nodes.push(this.finishNode(node, "ImportSpecifier")); + node.local = this.parseIdent(); + this.checkLVal(node.local, true); + nodes.push(this.finishNode(node, "ImportDefaultSpecifier")); if (!this.eat(tt.comma)) return nodes; } if (this.type === tt.star) { var node = this.startNode(); this.next(); this.expectContextual("as"); - node.name = this.parseIdent(); - this.checkLVal(node.name, true); - nodes.push(this.finishNode(node, "ImportBatchSpecifier")); + node.local = this.parseIdent(); + this.checkLVal(node.local, true); + nodes.push(this.finishNode(node, "ImportNamespaceSpecifier")); return nodes; } this.expect(tt.braceL); @@ -2809,10 +2807,9 @@ } else first = false; var node = this.startNode(); - node.id = this.parseIdent(true); - node.name = this.eatContextual("as") ? this.parseIdent() : null; - this.checkLVal(node.name || node.id, true); - node['default'] = false; + node.imported = this.parseIdent(true); + node.local = this.eatContextual("as") ? this.parseIdent() : node.imported; + this.checkLVal(node.local, true); nodes.push(this.finishNode(node, "ImportSpecifier")); } return nodes; diff --git a/acorn_loose.js b/acorn_loose.js index 67236abbb7..6edf3b1f0c 100644 --- a/acorn_loose.js +++ b/acorn_loose.js @@ -1058,7 +1058,7 @@ node.declaration = this.parseStatement(); } else { node.declaration = null; - this.parseSpecifierList(node, "Export"); + this.parseExportSpecifierList(node); } this.semicolon(); return this.finishNode(node, "ExportDeclaration"); @@ -1074,28 +1074,56 @@ } else { if (this.tok.type === tt.name && this.tok.value !== "from") { var elt = this.startNode(); - elt.id = this.parseIdent(); - elt.name = null; - elt['default'] = true; - this.finishNode(elt, "ImportSpecifier"); + elt.local = this.parseIdent(); + this.finishNode(elt, "ImportDefaultSpecifier"); this.eat(tt.comma); } - this.parseSpecifierList(node, "Import"); - var specs = node.specifiers; - for (var i = 0; i < specs.length; i++) specs[i]['default'] = false; + this.parseImportSpecifierList(node); if (elt) node.specifiers.unshift(elt); } this.semicolon(); return this.finishNode(node, "ImportDeclaration"); }; - lp.parseSpecifierList = function(node, prefix) { + lp.parseImportSpecifierList = function(node) { + var elts = node.specifiers = []; + if (this.tok.type === tt.star) { + var elt = this.startNode(); + this.next(); + if (this.eatContextual("as")) elt.local = this.parseIdent(); + elts.push(this.finishNode(elt, "ImportNamespaceSpecifier")); + } else { + var indent = this.curIndent, line = this.curLineStart, continuedLine = this.nextLineStart; + this.pushCx(); + this.eat(tt.braceL); + if (this.curLineStart > continuedLine) continuedLine = this.curLineStart; + while (!this.closes(tt.braceR, indent + (this.curLineStart <= continuedLine ? 1 : 0), line)) { + var elt = this.startNode(); + if (this.eat(tt.star)) { + if (this.eatContextual("as")) elt.local = this.parseIdent(); + this.finishNode(elt, "ImportNamespaceSpecifier"); + } else { + if (this.isContextual("from")) break; + elt.imported = this.parseIdent(); + elt.local = this.eatContextual("as") ? this.parseIdent() : elt.imported; + this.finishNode(elt, "ImportSpecifier"); + } + elts.push(elt); + this.eat(tt.comma); + } + this.eat(tt.braceR); + this.popCx(); + } + node.source = this.eatContextual("from") ? this.parseExprAtom() : null; + }; + + lp.parseExportSpecifierList = function(node) { var elts = node.specifiers = []; if (this.tok.type === tt.star) { var elt = this.startNode(); this.next(); if (this.eatContextual("as")) elt.name = this.parseIdent(); - elts.push(this.finishNode(elt, prefix + "BatchSpecifier")); + elts.push(this.finishNode(elt, "ExportBatchSpecifier")); } else { var indent = this.curIndent, line = this.curLineStart, continuedLine = this.nextLineStart; this.pushCx(); @@ -1105,12 +1133,12 @@ var elt = this.startNode(); if (this.eat(tt.star)) { if (this.eatContextual("as")) elt.name = this.parseIdent(); - this.finishNode(elt, prefix + "BatchSpecifier"); + this.finishNode(elt, "ExportBatchSpecifier"); } else { if (this.isContextual("from")) break; elt.id = this.parseIdent(); elt.name = this.eatContextual("as") ? this.parseIdent() : null; - this.finishNode(elt, prefix + "Specifier"); + this.finishNode(elt, "ExportSpecifier"); } elts.push(elt); this.eat(tt.comma); diff --git a/test/tests-harmony.js b/test/tests-harmony.js index 8e3dd24c13..48a613207e 100644 --- a/test/tests-harmony.js +++ b/test/tests-harmony.js @@ -5253,8 +5253,8 @@ test("import $ from \"jquery\"", { body: [{ type: "ImportDeclaration", specifiers: [{ - type: "ImportSpecifier", - id: { + type: "ImportDefaultSpecifier", + local: { type: "Identifier", name: "$", loc: { @@ -5262,7 +5262,6 @@ test("import $ from \"jquery\"", { end: {line: 1, column: 8} } }, - name: null, loc: { start: {line: 1, column: 7}, end: {line: 1, column: 8} @@ -5299,7 +5298,15 @@ test("import { encrypt, decrypt } from \"crypto\"", { specifiers: [ { type: "ImportSpecifier", - id: { + imported: { + type: "Identifier", + name: "encrypt", + loc: { + start: {line: 1, column: 9}, + end: {line: 1, column: 16} + } + }, + local: { type: "Identifier", name: "encrypt", loc: { @@ -5307,7 +5314,6 @@ test("import { encrypt, decrypt } from \"crypto\"", { end: {line: 1, column: 16} } }, - name: null, loc: { start: {line: 1, column: 9}, end: {line: 1, column: 16} @@ -5315,7 +5321,15 @@ test("import { encrypt, decrypt } from \"crypto\"", { }, { type: "ImportSpecifier", - id: { + imported: { + type: "Identifier", + name: "decrypt", + loc: { + start: {line: 1, column: 18}, + end: {line: 1, column: 25} + } + }, + local: { type: "Identifier", name: "decrypt", loc: { @@ -5323,7 +5337,6 @@ test("import { encrypt, decrypt } from \"crypto\"", { end: {line: 1, column: 25} } }, - name: null, loc: { start: {line: 1, column: 18}, end: {line: 1, column: 25} @@ -5360,7 +5373,7 @@ test("import { encrypt as enc } from \"crypto\"", { type: "ImportDeclaration", specifiers: [{ type: "ImportSpecifier", - id: { + imported: { type: "Identifier", name: "encrypt", loc: { @@ -5368,7 +5381,7 @@ test("import { encrypt as enc } from \"crypto\"", { end: {line: 1, column: 16} } }, - name: { + local: { type: "Identifier", name: "enc", loc: { @@ -5419,21 +5432,19 @@ test("import crypto, { decrypt, encrypt as enc } from \"crypto\"", { }, specifiers: [ { - type: "ImportSpecifier", + type: "ImportDefaultSpecifier", loc: { start: {line: 1, column: 7}, end: {line: 1, column: 13} }, - id: { + local: { type: "Identifier", loc: { start: {line: 1, column: 7}, end: {line: 1, column: 13} }, name: "crypto" - }, - name: null, - default: true + } }, { type: "ImportSpecifier", @@ -5441,7 +5452,7 @@ test("import crypto, { decrypt, encrypt as enc } from \"crypto\"", { start: {line: 1, column: 17}, end: {line: 1, column: 24} }, - id: { + imported: { type: "Identifier", loc: { start: {line: 1, column: 17}, @@ -5449,8 +5460,14 @@ test("import crypto, { decrypt, encrypt as enc } from \"crypto\"", { }, name: "decrypt" }, - name: null, - default: false + local: { + type: "Identifier", + loc: { + start: {line: 1, column: 17}, + end: {line: 1, column: 24} + }, + name: "decrypt" + } }, { type: "ImportSpecifier", @@ -5458,7 +5475,7 @@ test("import crypto, { decrypt, encrypt as enc } from \"crypto\"", { start: {line: 1, column: 26}, end: {line: 1, column: 40} }, - id: { + imported: { type: "Identifier", loc: { start: {line: 1, column: 26}, @@ -5466,15 +5483,14 @@ test("import crypto, { decrypt, encrypt as enc } from \"crypto\"", { }, name: "encrypt" }, - name: { + local: { type: "Identifier", loc: { start: {line: 1, column: 37}, end: {line: 1, column: 40} }, name: "enc" - }, - default: false + } } ], source: { @@ -5501,7 +5517,7 @@ test("import { null as nil } from \"bar\"", { type: "ImportDeclaration", specifiers: [{ type: "ImportSpecifier", - id: { + imported: { type: "Identifier", name: "null", loc: { @@ -5509,7 +5525,7 @@ test("import { null as nil } from \"bar\"", { end: {line: 1, column: 13} } }, - name: { + local: { type: "Identifier", name: "nil", loc: { @@ -5559,12 +5575,12 @@ test("import * as crypto from \"crypto\"", { end: {line: 1, column: 32} }, specifiers: [{ - type: "ImportBatchSpecifier", + type: "ImportNamespaceSpecifier", loc: { start: {line: 1, column: 7}, end: {line: 1, column: 18} }, - name: { + local: { type: "Identifier", loc: { start: {line: 1, column: 12}, @@ -14346,17 +14362,15 @@ test("import foo, * as bar from 'baz';", { type: "ImportDeclaration", specifiers: [ { - type: "ImportSpecifier", - id: { + type: "ImportDefaultSpecifier", + local: { type: "Identifier", name: "foo" - }, - name: null, - default: true + } }, { - type: "ImportBatchSpecifier", - name: { + type: "ImportNamespaceSpecifier", + local: { type: "Identifier", name: "bar" }