From d35a9e783920fd286cdbb1574e703f199a1415f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Santos?= Date: Sun, 21 Feb 2016 17:06:09 +0000 Subject: [PATCH] Importing names from places --- lib/ast.js | 15 ++++++++++++++- lib/compress.js | 4 ++++ lib/output.js | 6 ++++++ lib/parse.js | 24 ++++++++++++++++++------ test/compress/harmony.js | 16 ++++++++++++++-- 5 files changed, 56 insertions(+), 9 deletions(-) diff --git a/lib/ast.js b/lib/ast.js index 0940647c..3457c134 100644 --- a/lib/ast.js +++ b/lib/ast.js @@ -702,10 +702,19 @@ var AST_Const = DEFNODE("Const", null, { $documentation: "A `const` statement" }, AST_Definitions); -var AST_Import = DEFNODE("Import", "module_name", { +var AST_Import = DEFNODE("Import", "imported_name module_name", { $documentation: "An `import` statement", $propdoc: { + imported_name: "[AST_SymbolImport] The name of the variable holding the module's default export.", module_name: "[AST_String] String literal describing where this module came from", + }, + _walk: function(visitor) { + return visitor._visit(this, function() { + if (this.imported_name) { + this.imported_name._walk(visitor); + } + this.module_name._walk(visitor); + }); } }); @@ -1072,6 +1081,10 @@ var AST_SymbolCatch = DEFNODE("SymbolCatch", null, { $documentation: "Symbol naming the exception in catch", }, AST_SymbolDeclaration); +var AST_SymbolImport = DEFNODE("SymbolImport", null, { + $documentation: "Symbol refering to an imported name", +}, AST_SymbolDeclaration); + var AST_Label = DEFNODE("Label", "references", { $documentation: "Symbol naming a label (declaration)", $propdoc: { diff --git a/lib/compress.js b/lib/compress.js index 52fbb74d..b1fbdf25 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1775,6 +1775,10 @@ merge(Compressor.prototype, { return self; }); + OPT(AST_Import, function(self, compressor) { + return self; + }); + OPT(AST_Function, function(self, compressor){ self = AST_Lambda.prototype.optimize.call(self, compressor); if (compressor.option("unused") && !compressor.option("keep_fnames")) { diff --git a/lib/output.js b/lib/output.js index 4dc53798..52747603 100644 --- a/lib/output.js +++ b/lib/output.js @@ -1040,6 +1040,12 @@ function OutputStream(options) { DEFPRINT(AST_Import, function(self, output) { output.print("import"); output.space(); + if (self.imported_name) { + self.imported_name.print(output); + output.space(); + output.print("from") + output.space(); + } self.module_name.print(output); output.semicolon(); }); diff --git a/lib/parse.js b/lib/parse.js index 4a549fb1..4e94f589 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1611,15 +1611,27 @@ function parse($TEXT, options) { } function import_() { + var start = prev(); + var imported_name; + if (is("name")) { + imported_name = as_symbol(AST_SymbolImport); + expect_token("name", "from"); + } + var mod_str = S.token; + if (mod_str.type !== 'string') { + unexpected(); + } + next(); return new AST_Import({ - start: prev(), + start: start, + imported_name: imported_name, module_name: new AST_String({ - start : S.token, - value : S.token.value, - quote : S.token.quote, - end : S.token, + start: mod_str, + value: mod_str.value, + quote: mod_str.quote, + end: mod_str, }), - end: next(), + end: S.token, }); } diff --git a/test/compress/harmony.js b/test/compress/harmony.js index 4cb3921c..dc1c2e4f 100644 --- a/test/compress/harmony.js +++ b/test/compress/harmony.js @@ -308,9 +308,21 @@ number_literals: { import_statement: { input: { import "mod-name"; - import "module2"; + import Foo from "bar"; + } + expect_exact: "import\"mod-name\";import Foo from\"bar\";" +} + +import_statement_mangling: { + mangle = { }; + input: { + import Foo from "foo"; + Foo(); + } + expect: { + import a from "foo"; + a(); } - expect_exact: "import\"mod-name\";import\"module2\";" } // Fabio: My patches accidentally caused a crash whenever