fix export name mapping syntax

This commit is contained in:
alexlamsl 2017-06-20 23:45:35 +08:00
parent 0f80bc42d0
commit 6b7a9d33a2
4 changed files with 53 additions and 23 deletions

View File

@ -1297,16 +1297,25 @@ function OutputStream(options) {
}); });
DEFPRINT(AST_NameMapping, function(self, output) { DEFPRINT(AST_NameMapping, function(self, output) {
var is_import = output.parent() instanceof AST_Import;
var definition = self.name.definition(); var definition = self.name.definition();
var names_are_different = var names_are_different =
(definition && definition.mangled_name || self.name.name) !== (definition && definition.mangled_name || self.name.name) !==
self.foreign_name.name; self.foreign_name.name;
if (names_are_different) { if (names_are_different) {
if (is_import) {
output.print(self.foreign_name.name); output.print(self.foreign_name.name);
} else {
self.name.print(output);
}
output.space(); output.space();
output.print("as"); output.print("as");
output.space(); output.space();
if (is_import) {
self.name.print(output); self.name.print(output);
} else {
output.print(self.foreign_name.name);
}
} else { } else {
self.name.print(output); self.name.print(output);
} }

View File

@ -2333,18 +2333,22 @@ function parse($TEXT, options) {
var foreign_name; var foreign_name;
var name; var name;
if (peek().value === "as" && peek().type === "name") { if (is_import) {
foreign_name = as_symbol(foreign_type); foreign_name = as_symbol(foreign_type);
next(); // The "as" word } else {
}
name = as_symbol(type); name = as_symbol(type);
}
if (foreign_name === undefined) { if (is("name", "as")) {
foreign_name = new foreign_type({ next(); // The "as" word
name: name.name, if (is_import) {
start: name.start, name = as_symbol(type);
end: name.end, } 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({ return new AST_NameMapping({

View File

@ -36,7 +36,7 @@ issue_2038_2: {
let a = 1; let a = 1;
const c = 2; const c = 2;
var n = 3; 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 };
}
}

View File

@ -2,8 +2,7 @@ var assert = require("assert");
var uglify = require("../node"); var uglify = require("../node");
describe("Export", function() { describe("Export", function() {
it ("Should parse export directives", function() { it("Should parse export directives", function() {
var inputs = [ var inputs = [
['export * from "a.js"', ['*'], "a.js"], ['export * from "a.js"', ['*'], "a.js"],
['export {A} from "a.js"', ['A'], "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"], ['export {A, B} from "a.js"', ['A', 'B'], "a.js"],
]; ];
var test = function(code) { function test(code) {
return uglify.parse(code); return uglify.parse(code);
}; }
var extractNames = function(symbols) { function extractNames(symbols) {
var ret = []; var ret = [];
for (var i = 0; i < symbols.length; i++) { for (var i = 0; i < symbols.length; i++) {
ret.push(symbols[i].name.name) ret.push(symbols[i].foreign_name.name);
} }
return ret; return ret;
}; }
for (var i = 0; i < inputs.length; i++) { for (var i = 0; i < inputs.length; i++) {
var ast = test(inputs[i][0]); var ast = test(inputs[i][0]);
@ -34,7 +33,7 @@ describe("Export", function() {
assert(st instanceof uglify.AST_Export); assert(st instanceof uglify.AST_Export);
var actualNames = extractNames(st.exported_names); var actualNames = extractNames(st.exported_names);
assert.deepEqual(actualNames, names); assert.deepEqual(actualNames, names);
assert.equal(st.module_name.value, filename) assert.equal(st.module_name.value, filename);
} }
}) });
}); });