From 2f93058c6e1aa5778e17d490811991902415b0f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C5=A0pan=C4=9Bl?= Date: Fri, 31 Mar 2017 11:52:56 +0200 Subject: [PATCH] More variants of import added (#1738) - `import * from "x.js"` - `import * as Name from "x.js"` --- lib/output.js | 24 ++++++++++------- lib/parse.js | 57 ++++++++++++++++++++-------------------- test/compress/harmony.js | 10 ++++++- 3 files changed, 51 insertions(+), 40 deletions(-) diff --git a/lib/output.js b/lib/output.js index 04be0f65..8c323060 100644 --- a/lib/output.js +++ b/lib/output.js @@ -1242,17 +1242,21 @@ function OutputStream(options) { output.space(); } if (self.imported_names) { - output.print("{"); - self.imported_names.forEach(function(name_import, i) { - output.space(); - name_import.print(output); - if (i < self.imported_names.length - 1) { - output.print(","); + if (self.imported_names.length === 1 && self.imported_names[0].foreign_name.name === "*") { + self.imported_names[0].print(output); + } else { + output.print("{"); + self.imported_names.forEach(function (name_import, i) { output.space(); - } - }); - output.space(); - output.print("}"); + name_import.print(output); + if (i < self.imported_names.length - 1) { + output.print(","); + output.space(); + } + }); + output.space(); + output.print("}"); + } } if (self.imported_name || self.imported_names) { output.space(); diff --git a/lib/parse.js b/lib/parse.js index 477b17bf..7b7b5aae 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -2176,17 +2176,7 @@ function parse($TEXT, options) { next(); } - if (is("punc", "{")) { - next(); - imported_names = []; - while (!is("punc", "}")) { - imported_names.push(import_name()); - if (is("punc", ",")) { - next(); - } - } - next(); - } + imported_names = import_names(true); if (imported_names || imported_name) { expect_token("name", "from"); @@ -2237,16 +2227,14 @@ function parse($TEXT, options) { }) } - function import_nameAsterisk() { + function import_nameAsterisk(name) { var start = S.token; var foreign_name; - var name; - next(); var end = prev(); - name = new AST_SymbolImport({ + name = name || new AST_SymbolImport({ name: '*', start: start, end: end, @@ -2266,6 +2254,30 @@ function parse($TEXT, options) { }) } + function import_names(allow_as) { + var names; + if (is("punc", "{")) { + next(); + names = []; + while (!is("punc", "}")) { + names.push(import_name()); + if (is("punc", ",")) { + next(); + } + } + next(); + } else if (is("operator", "*")) { + var name; + next(); + if (allow_as && is("name", "as")) { + next(); // The "as" word + name = as_symbol(AST_SymbolImportForeign); + } + names = [import_nameAsterisk(name)]; + } + return names; + } + function export_() { var start = S.token; var is_default; @@ -2278,20 +2290,7 @@ function parse($TEXT, options) { next(); } - if (is("punc", "{")) { - next(); - exported_names = []; - while (!is("punc", "}")) { - exported_names.push(import_name()); - if (is("punc", ",")) { - next(); - } - } - next(); - } else if (is("operator", "*")) { - var st = prev(); - exported_names = [import_nameAsterisk()]; - } + exported_names = import_names(false); if (exported_names) { if (is("name", "from")) { diff --git a/test/compress/harmony.js b/test/compress/harmony.js index fa29b18e..0fecf548 100644 --- a/test/compress/harmony.js +++ b/test/compress/harmony.js @@ -184,7 +184,15 @@ import_statement: { import Bar, { Foo } from 'lel'; import { Bar as kex, Baz as food } from 'lel'; } - expect_exact: "import\"mod-name\";import Foo from\"bar\";import{Bar,Baz}from\"lel\";import Bar,{Foo}from\"lel\";import{Bar as kex,Baz as food}from\"lel\";" + expect_exact: 'import"mod-name";import Foo from"bar";import{Bar,Baz}from"lel";import Bar,{Foo}from"lel";import{Bar as kex,Baz as food}from"lel";' +} + +import_all_statement: { + input: { + import * from 'lel'; + import * as Lel from 'lel'; + } + expect_exact: 'import*from"lel";import*as Lel from"lel";' } export_statement: {