run mozilla AST I/O tests against all existing compress tests

This commit is contained in:
Fábio Santos 2018-03-24 18:59:14 +00:00
parent 0150e02dc4
commit ca926600dd
6 changed files with 52 additions and 17 deletions

View File

@ -89,6 +89,7 @@
start: my_start_token(M),
end: my_end_token(M),
names: M.properties.map(from_moz),
is_array: false
});
},
AssignmentPattern: function(M) {
@ -198,11 +199,14 @@
Property: function(M) {
var key = M.key;
var args = {
start : my_start_token(key),
start : my_start_token(key || M.value),
end : my_end_token(M.value),
key : key.type == "Identifier" ? key.name : key.value,
value : from_moz(M.value)
};
if (M.computed) {
args.key = from_moz(M.key);
}
if (M.method) {
args.is_generator = M.value.generator;
args.async = M.value.async;
@ -214,14 +218,16 @@
return new AST_ConciseMethod(args);
}
if (M.kind == "init") {
if (key.type != "Identifier") {
if (key.type != "Identifier" && key.type != "Literal") {
args.key = from_moz(key);
}
return new AST_ObjectKeyVal(args);
}
if (typeof args.key === "string" || typeof args.key === "number") {
args.key = new AST_SymbolMethod({
name: args.key
});
}
args.value = new AST_Accessor(args.value);
if (M.kind == "get") return new AST_ObjectGetter(args);
if (M.kind == "set") return new AST_ObjectSetter(args);
@ -264,6 +270,9 @@
start : my_start_token(M),
end : my_end_token(M),
properties : M.properties.map(function(prop){
if (prop.type === "SpreadElement") {
return from_moz(prop);
}
prop.type = "Property";
return from_moz(prop)
})
@ -414,6 +423,7 @@
: p.type == "FunctionDeclaration" ? (p.id === M ? AST_SymbolDefun : AST_SymbolFunarg)
: p.type == "ArrowFunctionExpression" ? (p.params.indexOf(M) !== -1) ? AST_SymbolFunarg : AST_SymbolRef
: p.type == "ClassExpression" ? (p.id === M ? AST_SymbolClass : AST_SymbolRef)
: p.type == "Property" ? (p.key === M && p.computed || p.value === M ? AST_SymbolRef : AST_SymbolMethod)
: p.type == "ClassDeclaration" ? (p.id === M ? AST_SymbolDefClass : AST_SymbolRef)
: p.type == "MethodDefinition" ? (p.computed ? AST_SymbolRef : AST_SymbolMethod)
: p.type == "CatchClause" ? AST_SymbolCatch
@ -440,7 +450,7 @@
MOZ_TO_ME.ClassDeclaration =
MOZ_TO_ME.ClassExpression = function From_Moz_Class(M) {
return new AST_Class({
return new (M.type === "ClassDeclaration" ? AST_DefClass : AST_ClassExpression)({
start : my_start_token(M),
end : my_end_token(M),
name : from_moz(M.id),
@ -471,6 +481,7 @@
map("CatchClause", AST_Catch, "param>argname, body%body");
map("ThisExpression", AST_This);
map("SuperExpression", AST_Super);
map("BinaryExpression", AST_Binary, "operator=operator, left>left, right>right");
map("LogicalExpression", AST_Binary, "operator=operator, left>left, right>right");
map("AssignmentExpression", AST_Assign, "operator=operator, left>left, right>right");
@ -648,7 +659,7 @@
}
return {
type: M.is_default ? "ExportDefaultDeclaration" : "ExportNamedDeclaration",
declaration: to_moz(M.is_default ? M.exported_value : M.exported_definition)
declaration: to_moz(M.exported_value || M.exported_definition)
};
});
@ -708,7 +719,7 @@
});
def_to_moz(AST_Binary, function To_Moz_BinaryExpression(M) {
if (to_moz_in_destructuring()) {
if (M.operator == "=" && to_moz_in_destructuring()) {
return {
type: "AssignmentPattern",
left: to_moz(M.left),
@ -742,6 +753,12 @@
type: "Identifier",
value: M.key
};
if (typeof M.key === "number") {
key = {
type: "Literal",
value: Number(M.key)
};
}
if (typeof M.key === "string") {
key = {
type: "Identifier",
@ -749,10 +766,11 @@
};
}
var kind;
var computed = typeof M.key === "string" ? false : !(M.key instanceof AST_Symbol) || M.key instanceof AST_SymbolRef;
var string_or_num = typeof M.key === "string" || typeof M.key === "number";
var computed = string_or_num ? false : !(M.key instanceof AST_Symbol) || M.key instanceof AST_SymbolRef;
if (M instanceof AST_ObjectKeyVal) {
kind = "init";
computed = typeof M.key !== "string";
computed = !string_or_num;
} else
if (M instanceof AST_ObjectGetter) {
kind = "get";

View File

@ -1455,7 +1455,13 @@ function OutputStream(options) {
output.space();
self.module_name.print(output);
}
if (!self.exported_definition) {
if (self.exported_value
&& !(self.exported_value instanceof AST_Defun ||
self.exported_value instanceof AST_Function ||
self.exported_value instanceof AST_Class)
|| self.module_name
|| self.exported_names
) {
output.semicolon();
}
});

View File

@ -1160,7 +1160,7 @@ function parse($TEXT, options) {
if (!is_token(peek(), "punc", "(")) {
next();
var node = export_();
if (can_insert_semicolon()) semicolon();
if (is("punc", ";")) semicolon();
return node;
}
}
@ -2712,7 +2712,8 @@ function parse($TEXT, options) {
next();
args.push(new AST_Expansion({
start: prev(),
expression: expression(false)
expression: expression(false),
end: prev()
}));
} else {
args.push(expression(false));

View File

@ -277,7 +277,7 @@ export_default_anonymous_function: {
foo();
}
}
expect_exact: "export default function(){foo()};"
expect_exact: "export default function(){foo()}"
}
export_default_arrow: {
@ -394,7 +394,7 @@ export_default_anonymous_class: {
}
};
}
expect_exact: "export default class{constructor(){foo()}};"
expect_exact: "export default class{constructor(){foo()}}"
}
export_default_anonymous_function_not_call: {

View File

@ -254,7 +254,7 @@ export_default_anon_function: {
console.log(1 + 2);
}
}
expect_exact: "export default function(){console.log(3)};"
expect_exact: "export default function(){console.log(3)}"
}
export_default_anon_class: {
@ -266,7 +266,7 @@ export_default_anon_class: {
foo() { console.log(1 + 2); }
}
}
expect_exact: "export default class{foo(){console.log(3)}};"
expect_exact: "export default class{foo(){console.log(3)}}"
}
export_module_statement: {

View File

@ -110,6 +110,16 @@ function run_compress_tests() {
});
return false;
}
var ast = input.to_mozilla_ast();
var ast_as_string = U.AST_Node.from_mozilla_ast(ast).print_to_string();
var input_string = input.print_to_string();
if (input_string !== ast_as_string) {
log("!!! Mozilla AST I/O corrupted input\n---INPUT---\n{input}\n---OUTPUT---\n{output}\n\n", {
input: input_string,
output: ast_as_string,
});
return false;
}
var options = U.defaults(test.options, {
warnings: false
});