From d4565fed53c387854f51dc18ce3fec7b7150e8ee Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Tue, 28 Oct 2014 16:55:30 +0200 Subject: [PATCH] Loose: ES6 import, export. Removed "kind" from tests for ES6 import/export as it's left only for backward compatibility. --- acorn_loose.js | 96 +++++++++++++++++++++++++++++++++++++++++++ test/tests-harmony.js | 10 +---- 2 files changed, 98 insertions(+), 8 deletions(-) diff --git a/acorn_loose.js b/acorn_loose.js index 7585b83b28..dd1ea902c0 100644 --- a/acorn_loose.js +++ b/acorn_loose.js @@ -449,6 +449,12 @@ case tt._class: return parseObj(true, true); + case tt._import: + return parseImport(); + + case tt._export: + return parseExport(); + default: var expr = parseExpression(); if (isDummy(expr)) { @@ -963,6 +969,96 @@ return finishNode(node, "ArrowFunctionExpression"); } + function parseExport() { + var node = startNode(); + next(); + node['default'] = eat(tt._default); + node.specifiers = node.source = null; + if (node['default']) { + node.declaration = parseExpression(); + semicolon(); + } else if (token.type.keyword) { + node.declaration = parseStatement(); + } else { + node.declaration = null; + parseSpecifierList(node, "Export"); + } + return finishNode(node, "ExportDeclaration"); + } + + function parseImport() { + var node = startNode(); + next(); + if (token.type === tt.string) { + node.specifiers = []; + node.source = parseExprAtom(); + node.kind = ''; + } else { + if (token.type === tt.name && token.value !== "from") { + var elt = startNode(); + elt.id = parseIdent(); + elt.name = null; + elt['default'] = true; + finishNode(elt, "ImportSpecifier"); + eat(tt.comma); + } + parseSpecifierList(node, "Import"); + var specs = node.specifiers; + for (var i = 0; i < specs.length; i++) specs[i]['default'] = false; + if (elt) node.specifiers.unshift(elt); + } + return finishNode(node, "ImportDeclaration"); + } + + function parseSpecifierList(node, prefix) { + var elts = node.specifiers = []; + if (token.type === tt.star) { + var elt = startNode(); + next(); + if (token.type === tt.name && token.value === "as") { + next(); + elt.name = parseIdent(); + } + elts.push(finishNode(elt, prefix + "BatchSpecifier")); + } else { + var indent = curIndent, line = curLineStart, continuedLine = nextLineStart; + pushCx(); + eat(tt.braceL); + if (curLineStart > continuedLine) continuedLine = curLineStart; + while (!closes(tt.braceR, indent + (curLineStart <= continuedLine ? 1 : 0), line)) { + var elt = startNode(); + if (token.type === tt.star) { + next(); + if (token.type === tt.name && token.value === "as") { + next(); + elt.name = parseIdent(); + } + finishNode(elt, prefix + "BatchSpecifier"); + } else { + if (token.type === tt.name && token.value === "from") break; + elt.id = parseIdent(); + if (token.type === tt.name && token.value === "as") { + next(); + elt.name = parseIdent(); + } else { + elt.name = null; + } + finishNode(elt, prefix + "Specifier"); + } + elts.push(elt); + eat(tt.comma); + } + eat(tt.braceR); + popCx(); + } + if (token.type === tt.name && token.value === "from") { + next(); + node.source = parseExprAtom(); + } else { + node.source = null; + } + } + function parseExprList(close, allowEmpty) { var indent = curIndent, line = curLineStart, elts = [], continuedLine = nextLineStart; next(); // Opening bracket diff --git a/test/tests-harmony.js b/test/tests-harmony.js index 84cf2be487..00149c1ee7 100644 --- a/test/tests-harmony.js +++ b/test/tests-harmony.js @@ -5113,7 +5113,6 @@ test("import $ from \"jquery\"", { end: {line: 1, column: 8} } }], - kind: "default", source: { type: "Literal", value: "jquery", @@ -5176,7 +5175,6 @@ test("import { encrypt, decrypt } from \"crypto\"", { } } ], - kind: "named", source: { type: "Literal", value: "crypto", @@ -5228,7 +5226,6 @@ test("import { encrypt as enc } from \"crypto\"", { end: {line: 1, column: 23} } }], - kind: "named", source: { type: "Literal", value: "crypto", @@ -5333,8 +5330,7 @@ test("import crypto, { decrypt, encrypt as enc } from \"crypto\"", { }, value: "crypto", raw: "\"crypto\"" - }, - kind: "default" + } }] }, { ecmaVersion: 6, @@ -5371,7 +5367,6 @@ test("import { null as nil } from \"bar\"", { end: {line: 1, column: 20} } }], - kind: "named", source: { type: "Literal", value: "bar", @@ -5431,8 +5426,7 @@ test("import * as crypto from \"crypto\"", { }, value: "crypto", raw: "\"crypto\"" - }, - kind: "named" + } }] }, { ecmaVersion: 6,