test feeding spidermonkey AST through escodegen

This commit is contained in:
Fábio Santos 2018-03-22 21:22:25 +00:00
parent 2655e84f0d
commit d310bcdf2a
6 changed files with 57 additions and 6 deletions

View File

@ -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",

View File

@ -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) {

View File

@ -1159,7 +1159,9 @@ function parse($TEXT, options) {
case "export":
if (!is_token(peek(), "punc", "(")) {
next();
return export_();
var node = export_();
semicolon();
return node;
}
}
}

View File

@ -29,6 +29,7 @@
},
"devDependencies": {
"acorn": "~5.4.1",
"escodegen": "^1.9.1",
"mocha": "~3.5.1",
"semver": "~5.5.0"
},

View File

@ -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`;

View File

@ -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
})
);
});
});