Skip to content

Commit

Permalink
fix corner case with spread syntax (#5572)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Jul 22, 2022
1 parent d67daa8 commit 56e9454
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 44 deletions.
12 changes: 2 additions & 10 deletions lib/parse.js
Expand Up @@ -552,16 +552,8 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {

function handle_dot() {
next();
var ch = peek();
if (ch == ".") {
var op = ".";
do {
op += ".";
next();
} while (peek() == ".");
return token("operator", op);
}
return is_digit(ch.charCodeAt(0)) ? read_num(".") : token("punc", ".");
if (looking_at("..")) return token("operator", "." + next() + next());
return is_digit(peek().charCodeAt(0)) ? read_num(".") : token("punc", ".");
}

function read_word() {
Expand Down
9 changes: 9 additions & 0 deletions test/compress/spreads.js
@@ -1,3 +1,12 @@
decimal: {
input: {
console.log({... 0.42});
}
expect_exact: "console.log({....42});"
expect_stdout: "{}"
node_version: ">=8.3.0"
}

collapse_vars_1: {
options = {
collapse_vars: true,
Expand Down
103 changes: 69 additions & 34 deletions test/mozilla-ast.js
Expand Up @@ -61,12 +61,12 @@ function test(input, to_moz, description, skip_on_error, beautified) {
var ast = UglifyJS.AST_Node.from_mozilla_ast(to_moz(input));
} catch (e) {
if (skip_on_error) return true;
console.log("//=============================================================");
console.log("//", description, "failed... round", round);
console.log(e);
console.log("// original code");
if (beautified === true) console.log("// (beautified)");
console.log(input.code);
console.error("//=============================================================");
console.error("//", description, "failed... round", round);
console.error(e);
console.error("// original code");
if (beautified === true) console.error("// (beautified)");
console.error(input.code);
return false;
}
var transformed = validate(ast);
Expand All @@ -78,34 +78,34 @@ function test(input, to_moz, description, skip_on_error, beautified) {
if (!test(beautified, to_moz, description, skip_on_error, true)) return false;
}
}
console.log("//=============================================================");
console.log("// !!!!!! Failed... round", round);
console.log("// original code");
console.error("//=============================================================");
console.error("// !!!!!! Failed... round", round);
console.error("// original code");
if (beautified.error) {
console.log("// !!! beautify failed !!!");
console.log(beautified.error.stack);
console.error("// !!! beautify failed !!!");
console.error(beautified.error.stack);
} else if (beautified === true) {
console.log("// (beautified)");
console.error("// (beautified)");
}
console.log(input.raw);
console.log();
console.log();
console.log("//-------------------------------------------------------------");
console.log("//", description);
console.error(input.raw);
console.error();
console.error();
console.error("//-------------------------------------------------------------");
console.error("//", description);
if (transformed.error) {
console.log(transformed.error.stack);
console.error(transformed.error.stack);
} else {
beautified = beautify(transformed.ast);
if (beautified.error) {
console.log("// !!! beautify failed !!!");
console.log(beautified.error.stack);
console.log(transformed.code);
console.error("// !!! beautify failed !!!");
console.error(beautified.error.stack);
console.error(transformed.code);
} else {
console.log("// (beautified)");
console.log(beautified.code);
console.error("// (beautified)");
console.error(beautified.code);
}
}
console.log("!!!!!! Failed... round", round);
console.error("!!!!!! Failed... round", round);
return false;
}
return true;
Expand All @@ -119,10 +119,26 @@ for (var round = 1; round <= num_iterations; round++) {
var code = ufuzz.createTopLevelCode();
minify_options.forEach(function(options) {
var ok = true;
var input = UglifyJS.minify(options ? function(options) {
options.module = ufuzz.module;
return UglifyJS.minify(code, options).code;
}(JSON.parse(options)) : code, {
var minified;
if (options) {
var o = JSON.parse(options);
o.module = ufuzz.module;
minified = UglifyJS.minify(code, o);
if (minified.error) {
console.log("//=============================================================");
console.log("// minify() failed... round", round);
console.log("// original code");
console.log(code);
console.log();
console.log();
console.log("//-------------------------------------------------------------");
console.log("minify(options):");
console.log(JSON.stringify(o, null, 2));
return;
}
minified = minified.code;
}
var input = UglifyJS.minify(minified || code, {
compress: false,
mangle: false,
module: ufuzz.module,
Expand All @@ -133,11 +149,27 @@ for (var round = 1; round <= num_iterations; round++) {
input.raw = options ? input.code : code;
if (input.error) {
ok = false;
console.log("//=============================================================");
console.log("// minify() failed... round", round);
console.log(input.error);
console.log("// original code");
console.log(code);
console.error("//=============================================================");
console.error("// parse() failed... round", round);
console.error("// original code");
console.error(code);
console.error();
console.error();
if (options) {
console.error("//-------------------------------------------------------------");
console.error("// minified code");
console.error(minified);
console.error();
console.error();
console.error("//-------------------------------------------------------------");
console.error("minify(options):");
console.error(JSON.stringify(o, null, 2));
console.error();
console.error();
}
console.error("//-------------------------------------------------------------");
console.error("// parse() error");
console.error(input.error);
}
if (ok) ok = test(input, function(input) {
return input.ast.to_mozilla_ast();
Expand All @@ -149,7 +181,10 @@ for (var round = 1; round <= num_iterations; round++) {
sourceType: "module",
});
}, "acorn.parse()", !ufuzz.verbose);
if (!ok) process.exit(1);
if (!ok && isFinite(num_iterations)) {
console.log();
process.exit(1);
}
});
}
console.log();

0 comments on commit 56e9454

Please sign in to comment.