diff --git a/lib/mozilla-ast.js b/lib/mozilla-ast.js index 275047b1..5848ecc1 100644 --- a/lib/mozilla-ast.js +++ b/lib/mozilla-ast.js @@ -659,7 +659,12 @@ local: to_moz(M.imported_name) }); } - if (M.imported_names) { + if (M.imported_names && M.imported_names[0].foreign_name.name === '*') { + specifiers.push({ + type: "ImportNamespaceSpecifier", + local: to_moz(M.imported_names[0].name) + }); + } else if (M.imported_names) { M.imported_names.forEach(function(name_mapping) { specifiers.push({ type: "ImportSpecifier", @@ -743,8 +748,10 @@ }; } var kind; + var computed = typeof M.key === "string" ? false : !(M.key instanceof AST_Symbol) || M.key instanceof AST_SymbolRef; if (M instanceof AST_ObjectKeyVal) { kind = "init"; + computed = typeof M.key !== "string"; } else if (M instanceof AST_ObjectGetter) { kind = "get"; @@ -755,7 +762,7 @@ if (parent instanceof AST_Class) { return { type: "MethodDefinition", - computed: !(M.key instanceof AST_Symbol) || M.key instanceof AST_SymbolRef, + computed: computed, kind: kind, static: M.static, key: to_moz(M.key), @@ -764,6 +771,7 @@ } return { type: "Property", + computed: computed, kind: kind, key: key, value: to_moz(M.value) @@ -819,7 +827,13 @@ }; }); - def_to_moz(AST_Symbol, function To_Moz_Identifier(M) { + def_to_moz(AST_Symbol, function To_Moz_Identifier(M, parent) { + if (M instanceof AST_SymbolMethod && parent.quote) { + return { + type: "Literal", + value: M.name + }; + } var def = M.definition(); return { type: "Identifier", diff --git a/lib/output.js b/lib/output.js index e4987179..4abe95c0 100644 --- a/lib/output.js +++ b/lib/output.js @@ -1455,7 +1455,9 @@ function OutputStream(options) { output.space(); self.module_name.print(output); } - output.semicolon(); + if (!self.exported_definition) { + output.semicolon(); + } }); function parenthesize_for_noin(node, output, noin) { diff --git a/lib/parse.js b/lib/parse.js index 313d890f..febd79db 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1159,7 +1159,9 @@ function parse($TEXT, options) { case "export": if (!is_token(peek(), "punc", "(")) { next(); - return export_(); + var node = export_(); + semicolon(); + return node; } } } diff --git a/package.json b/package.json index eb8e902d..c35fd034 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ }, "devDependencies": { "acorn": "~5.4.1", + "escodegen": "^1.9.1", "mocha": "~3.5.1", "semver": "~5.5.0" }, diff --git a/test/input/spidermonkey/input.js b/test/input/spidermonkey/input.js index 7cdc7c19..e4de54ec 100644 --- a/test/input/spidermonkey/input.js +++ b/test/input/spidermonkey/input.js @@ -67,7 +67,7 @@ function f () { console.log([10, ...[], 20, ...[30, 40], 50]["length"]); var { w: w1, ...V } = { w: 7, x: 1, y: 2 }; for (const x of y) {} -async function f1() { await x + y; } +async function f1() { await x; } ``; `x`; diff --git a/test/mocha/spidermonkey.js b/test/mocha/spidermonkey.js index 76c5b674..6f80a90e 100644 --- a/test/mocha/spidermonkey.js +++ b/test/mocha/spidermonkey.js @@ -2,6 +2,7 @@ var assert = require("assert"); var fs = require("fs"); var exec = require("child_process").exec; var acorn = require("acorn"); +var escodegen = require("escodegen"); var uglify = require("../node"); describe("spidermonkey export/import sanity test", function() { @@ -136,4 +137,35 @@ describe("spidermonkey export/import sanity test", function() { uglify_ast.print_to_string() ); }); + + it("should produce an AST compatible with escodegen", function() { + var code = fs.readFileSync("test/input/spidermonkey/input.js", "utf-8"); + var uglify_ast = uglify.parse(code); + var moz_ast = uglify_ast.to_mozilla_ast(); + assert.strictEqual( + escodegen.generate(moz_ast, { + format: { + indent: { + style: "", + }, + newline: "", + space: "", + quotes: "double" + } + }) + .replace(/;}/g, "}") + .replace(/var {/g, "var{") + .replace(/var \[/g, "var[") + .replace(/const {/g, "const{") + .replace(/const \[/g, "const[") + .replace(/get \"/g, "get\"") + .replace(/set \"/g, "set\"") + .replace(/\[object Object\].\[object Object\]/g, "new.target") // escodegen issue + .replace(/\(await x\)/, "await x") + , + uglify_ast.print_to_string({ + keep_quoted_props: true + }) + ); + }); });