From ca926600dd66188f867e6097c662e64771f566a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Santos?= Date: Sat, 24 Mar 2018 18:59:14 +0000 Subject: [PATCH] run mozilla AST I/O tests against all existing compress tests --- lib/mozilla-ast.js | 38 ++++++++++++++++++++++++++++---------- lib/output.js | 8 +++++++- lib/parse.js | 5 +++-- test/compress/export.js | 4 ++-- test/compress/harmony.js | 4 ++-- test/run-tests.js | 10 ++++++++++ 6 files changed, 52 insertions(+), 17 deletions(-) diff --git a/lib/mozilla-ast.js b/lib/mozilla-ast.js index 3ad26132..1676ee37 100644 --- a/lib/mozilla-ast.js +++ b/lib/mozilla-ast.js @@ -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); } - args.key = new AST_SymbolMethod({ - name: args.key - }); + 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"; diff --git a/lib/output.js b/lib/output.js index 4abe95c0..9093665e 100644 --- a/lib/output.js +++ b/lib/output.js @@ -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(); } }); diff --git a/lib/parse.js b/lib/parse.js index 414a47ac..c44088c8 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -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)); diff --git a/test/compress/export.js b/test/compress/export.js index 6fcc46b0..66eed356 100644 --- a/test/compress/export.js +++ b/test/compress/export.js @@ -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: { diff --git a/test/compress/harmony.js b/test/compress/harmony.js index 93025177..c7f105c0 100644 --- a/test/compress/harmony.js +++ b/test/compress/harmony.js @@ -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: { diff --git a/test/run-tests.js b/test/run-tests.js index deb1f954..05994d99 100755 --- a/test/run-tests.js +++ b/test/run-tests.js @@ -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 });