From 97728c4f0bb3a9fe3f135371deaa94afaad7921c Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sat, 9 May 2020 22:25:44 +0100 Subject: [PATCH] improve AST validation (#3864) --- lib/ast.js | 1 + lib/mozilla-ast.js | 2 +- test/mozilla-ast.js | 17 ++++++++++++++--- test/ufuzz/index.js | 5 ++--- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/ast.js b/lib/ast.js index 2cbb69d6..240ef160 100644 --- a/lib/ast.js +++ b/lib/ast.js @@ -784,6 +784,7 @@ var AST_Sequence = DEFNODE("Sequence", "expressions", { }); }, _validate: function() { + if (this.expressions.length < 2) throw new Error("expressions must contain multiple elements"); must_be_expressions(this, "expressions"); }, }); diff --git a/lib/mozilla-ast.js b/lib/mozilla-ast.js index 272e3e2a..1ce16921 100644 --- a/lib/mozilla-ast.js +++ b/lib/mozilla-ast.js @@ -111,7 +111,7 @@ var args = { start : my_start_token(key), end : my_end_token(M.value), - key : key.type == "Identifier" ? key.name : key.value, + key : "" + key[key.type == "Identifier" ? "name" : "value"], value : from_moz(M.value) }; if (M.kind == "init") return new AST_ObjectKeyVal(args); diff --git a/test/mozilla-ast.js b/test/mozilla-ast.js index c94ae438..5f5ad541 100644 --- a/test/mozilla-ast.js +++ b/test/mozilla-ast.js @@ -24,11 +24,22 @@ function try_beautify(code) { } } -function test(original, estree, description) { - var transformed = UglifyJS.minify(UglifyJS.AST_Node.from_mozilla_ast(estree), { +function validate(ast) { + try { + ast.walk(new UglifyJS.TreeWalker(function(node) { + node.validate(); + })); + } catch (e) { + return { error: e }; + } + return UglifyJS.minify(ast, { compress: false, - mangle: false + mangle: false, }); +} + +function test(original, estree, description) { + var transformed = validate(UglifyJS.AST_Node.from_mozilla_ast(estree)); if (transformed.error || original !== transformed.code) { console.log("//============================================================="); console.log("// !!!!!! Failed... round", round); diff --git a/test/ufuzz/index.js b/test/ufuzz/index.js index 6e02d83c..1a2163ed 100644 --- a/test/ufuzz/index.js +++ b/test/ufuzz/index.js @@ -1161,6 +1161,7 @@ for (var round = 1; round <= num_iterations; round++) { (errored ? fallback_options : minify_options).forEach(function(options) { var o = JSON.parse(options); var toplevel = sandbox.has_toplevel(o); + o.validate = true; uglify_code = UglifyJS.minify(original_code, o); original_result = orig_result[toplevel ? 1 : 0]; if (!uglify_code.error) { @@ -1181,9 +1182,7 @@ for (var round = 1; round <= num_iterations; round++) { } } else { uglify_code = uglify_code.error; - if (errored) { - ok = uglify_code.name == original_result.name; - } + ok = sandbox.same_stdout(original_result, uglify_result); } if (verbose || (verbose_interval && !(round % INTERVAL_COUNT)) || !ok) log(options); else if (errored) { -- 2.34.1