From 33d50820435b771f62c9cec4ece4b44e5d59d572 Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Sun, 23 Nov 2014 14:06:13 +0200 Subject: [PATCH] Parse `import Thing ...` in the same way as `import {default as Thing} ...`. Adjusts with Reflect.parse output and simplifies handling of "default" case in external tools by providing regular named ImportSpecifier. --- acorn.js | 10 +++++----- acorn_loose.js | 7 ++++--- test/tests-harmony.js | 29 ++++++++++++++++++++++------- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/acorn.js b/acorn.js index daa502ae16..3055cc9aa0 100644 --- a/acorn.js +++ b/acorn.js @@ -2541,7 +2541,6 @@ if (tokType === _string) { node.specifiers = []; node.source = parseExprAtom(); - node.kind = ""; } else { node.specifiers = parseImportSpecifiers(); if (tokType !== _name || tokVal !== "from") unexpected(); @@ -2559,10 +2558,11 @@ if (tokType === _name) { // import defaultObj, { x, y as z } from '...' var node = startNode(); - node.id = parseIdent(); - checkLVal(node.id, true); - node.name = null; - node['default'] = true; + node.id = startNode(); + node.name = parseIdent(); + checkLVal(node.name, true); + node.id.name = "default"; + finishNode(node.id, "Identifier"); nodes.push(finishNode(node, "ImportSpecifier")); if (!eat(_comma)) return nodes; } diff --git a/acorn_loose.js b/acorn_loose.js index 649aec6241..4be08176f7 100644 --- a/acorn_loose.js +++ b/acorn_loose.js @@ -1016,9 +1016,10 @@ } else { if (token.type === tt.name && token.value !== "from") { var elt = startNode(); - elt.id = parseIdent(); - elt.name = null; - elt['default'] = true; + elt.id = startNode(); + elt.name = parseIdent(); + elt.id.name = "default"; + finishNode(elt.id, "Identifier"); finishNode(elt, "ImportSpecifier"); eat(tt.comma); } diff --git a/test/tests-harmony.js b/test/tests-harmony.js index 15e3b98545..bf11776e4a 100644 --- a/test/tests-harmony.js +++ b/test/tests-harmony.js @@ -5153,6 +5153,14 @@ test("import $ from \"jquery\"", { specifiers: [{ type: "ImportSpecifier", id: { + type: "Identifier", + name: "default", + loc: { + start: {line: 1, column: 7}, + end: {line: 1, column: 8} + } + }, + name: { type: "Identifier", name: "$", loc: { @@ -5160,7 +5168,6 @@ test("import $ from \"jquery\"", { end: {line: 1, column: 8} } }, - name: null, loc: { start: {line: 1, column: 7}, end: {line: 1, column: 8} @@ -5328,10 +5335,16 @@ test("import crypto, { decrypt, encrypt as enc } from \"crypto\"", { start: {line: 1, column: 7}, end: {line: 1, column: 13} }, - name: "crypto" + name: "default" }, - name: null, - default: true + name: { + type: "Identifier", + loc: { + start: {line: 1, column: 7}, + end: {line: 1, column: 13} + }, + name: "crypto" + } }, { type: "ImportSpecifier", @@ -14171,10 +14184,12 @@ test("import foo, * as bar from 'baz';", { type: "ImportSpecifier", id: { type: "Identifier", - name: "foo" + name: "default" }, - name: null, - default: true + name: { + type: "Identifier", + name: "foo" + } }, { type: "ImportBatchSpecifier",