tweak test & warnings (#5123)

closes #5116
closes #5117
closes #5122
This commit is contained in:
Alex Lam S.L
2021-09-06 18:33:23 +01:00
committed by GitHub
parent c3aef23614
commit 4b88dfb8d9
2 changed files with 40 additions and 38 deletions

View File

@@ -3716,12 +3716,8 @@ merge(Compressor.prototype, {
} }
function extract_declarations_from_unreachable_code(compressor, stat, target) { function extract_declarations_from_unreachable_code(compressor, stat, target) {
if (!(stat instanceof AST_DefClass
|| stat instanceof AST_Definitions
|| stat instanceof AST_LambdaDefinition)) {
AST_Node.warn("Dropping unreachable code [{file}:{line},{col}]", stat.start);
}
var block; var block;
var dropped = false;
stat.walk(new TreeWalker(function(node, descend) { stat.walk(new TreeWalker(function(node, descend) {
if (node instanceof AST_DefClass) { if (node instanceof AST_DefClass) {
node.extends = null; node.extends = null;
@@ -3750,16 +3746,17 @@ merge(Compressor.prototype, {
block = []; block = [];
descend(); descend();
if (block.required) { if (block.required) {
target.push(make_node(AST_BlockStatement, stat, { target.push(make_node(AST_BlockStatement, stat, { body: block }));
body: block
}));
} else if (block.length) { } else if (block.length) {
[].push.apply(target, block); [].push.apply(target, block);
} }
block = save; block = save;
return true; return true;
} }
if (!(node instanceof AST_LoopControl)) dropped = true;
})); }));
if (dropped) AST_Node.warn("Dropping unreachable code [{file}:{line},{col}]", stat.start);
function push(node) { function push(node) {
if (block) { if (block) {
block.push(node); block.push(node);
@@ -6709,7 +6706,7 @@ merge(Compressor.prototype, {
if (tail.length == 0) break; if (tail.length == 0) break;
if (tail.length == duplicated) { if (tail.length == duplicated) {
[].unshift.apply(side_effects, tail.map(function(def) { [].unshift.apply(side_effects, tail.map(function(def) {
AST_Node.warn("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", template(def.name)); AST_Node.info("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", template(def.name));
var sym = def.name.definition(); var sym = def.name.definition();
var ref = make_node(AST_SymbolRef, def.name, def.name); var ref = make_node(AST_SymbolRef, def.name, def.name);
sym.references.push(ref); sym.references.push(ref);

View File

@@ -282,35 +282,40 @@ describe("test/reduce.js", function() {
}); });
if (result.error) throw result.error; if (result.error) throw result.error;
assert.deepEqual(result.warnings, []); assert.deepEqual(result.warnings, []);
assert.strictEqual(result.code.replace(/function \(/g, "function("), (semver.satisfies(process.version, "<=0.10") ? [ if (semver.satisfies(process.version, "<=0.10")) {
"// Can't reproduce test failure", assert.strictEqual(result.code, [
"// minify options: {", "// Can't reproduce test failure",
'// "compress": false,', "// minify options: {",
'// "mangle": false,', '// "compress": false,',
'// "output": {', '// "mangle": false,',
'// "beautify": true', '// "output": {',
"// }", '// "beautify": true',
"// }", "// }",
] : [ "// }",
[ ].join("\n"));
"try{", } else {
"null[function(){}]", var message = result.code.split(/\n/, 3)[1].slice("// output: ".length);
"}catch(e){", assert.strictEqual(result.code, [
"console.log(e)", [
"}", "try{",
].join(""), "null[function(){}]",
"// output: TypeError: Cannot read property 'function(){}' of null", "}catch(e){",
"// ", "console.log(e)",
"// minify: TypeError: Cannot read property 'function() {}' of null", "}",
"// ", ].join(""),
"// options: {", "// output: " + message,
'// "compress": false,', "// ",
'// "mangle": false,', "// minify: " + message.replace("(){}", "() {}"),
'// "output": {', "// ",
'// "beautify": true', "// options: {",
"// }", '// "compress": false,',
"// }", '// "mangle": false,',
]).join("\n")); '// "output": {',
'// "beautify": true',
"// }",
"// }",
].join("\n"));
}
}); });
it("Should maintain block-scope for const/let", function() { it("Should maintain block-scope for const/let", function() {
if (semver.satisfies(process.version, "<4")) return; if (semver.satisfies(process.version, "<4")) return;