From 6b7a9d33a2df3bd0181eaff39353d254c5400f7b Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Tue, 20 Jun 2017 23:45:35 +0800 Subject: [PATCH] fix `export` name mapping syntax --- lib/output.js | 13 +++++++++++-- lib/parse.js | 24 ++++++++++++++---------- test/compress/export.js | 20 +++++++++++++++++++- test/mocha/export.js | 19 +++++++++---------- 4 files changed, 53 insertions(+), 23 deletions(-) diff --git a/lib/output.js b/lib/output.js index 3c04e3ea..9a13374d 100644 --- a/lib/output.js +++ b/lib/output.js @@ -1297,16 +1297,25 @@ function OutputStream(options) { }); DEFPRINT(AST_NameMapping, function(self, output) { + var is_import = output.parent() instanceof AST_Import; var definition = self.name.definition(); var names_are_different = (definition && definition.mangled_name || self.name.name) !== self.foreign_name.name; if (names_are_different) { - output.print(self.foreign_name.name); + if (is_import) { + output.print(self.foreign_name.name); + } else { + self.name.print(output); + } output.space(); output.print("as"); output.space(); - self.name.print(output); + if (is_import) { + self.name.print(output); + } else { + output.print(self.foreign_name.name); + } } else { self.name.print(output); } diff --git a/lib/parse.js b/lib/parse.js index 84696890..27ecba59 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -2333,18 +2333,22 @@ function parse($TEXT, options) { var foreign_name; var name; - if (peek().value === "as" && peek().type === "name") { + if (is_import) { foreign_name = as_symbol(foreign_type); - next(); // The "as" word + } else { + name = as_symbol(type); } - name = as_symbol(type); - - if (foreign_name === undefined) { - foreign_name = new foreign_type({ - name: name.name, - start: name.start, - end: name.end, - }); + if (is("name", "as")) { + next(); // The "as" word + if (is_import) { + name = as_symbol(type); + } else { + foreign_name = as_symbol(foreign_type); + } + } else if (is_import) { + name = new type(foreign_name); + } else { + foreign_name = new foreign_type(name); } return new AST_NameMapping({ diff --git a/test/compress/export.js b/test/compress/export.js index a773aad6..6315cdb1 100644 --- a/test/compress/export.js +++ b/test/compress/export.js @@ -36,7 +36,7 @@ issue_2038_2: { let a = 1; const c = 2; var n = 3; - export { LET as a, CONST as c, VAR as n }; + export { a as LET, c as CONST, n as VAR }; } } @@ -67,3 +67,21 @@ issue_2124: { } } } + +issue_2126: { + mangle = { + toplevel: true, + } + input: { + import { foo as bar, cat as dog } from "stuff"; + console.log(bar, dog); + export { bar as qux }; + export { dog }; + } + expect: { + import { foo as o, cat as f } from "stuff"; + console.log(o, f); + export { o as qux }; + export { f as dog }; + } +} diff --git a/test/mocha/export.js b/test/mocha/export.js index 906aa68c..e9bb8503 100644 --- a/test/mocha/export.js +++ b/test/mocha/export.js @@ -2,8 +2,7 @@ var assert = require("assert"); var uglify = require("../node"); describe("Export", function() { - it ("Should parse export directives", function() { - + it("Should parse export directives", function() { var inputs = [ ['export * from "a.js"', ['*'], "a.js"], ['export {A} from "a.js"', ['A'], "a.js"], @@ -12,17 +11,17 @@ describe("Export", function() { ['export {A, B} from "a.js"', ['A', 'B'], "a.js"], ]; - var test = function(code) { + function test(code) { return uglify.parse(code); - }; + } - var extractNames = function(symbols) { + function extractNames(symbols) { var ret = []; for (var i = 0; i < symbols.length; i++) { - ret.push(symbols[i].name.name) + ret.push(symbols[i].foreign_name.name); } return ret; - }; + } for (var i = 0; i < inputs.length; i++) { var ast = test(inputs[i][0]); @@ -34,7 +33,7 @@ describe("Export", function() { assert(st instanceof uglify.AST_Export); var actualNames = extractNames(st.exported_names); assert.deepEqual(actualNames, names); - assert.equal(st.module_name.value, filename) + assert.equal(st.module_name.value, filename); } - }) -}); \ No newline at end of file + }); +});