improve warnings (#4247)

closes #4244
This commit is contained in:
Alex Lam S.L
2020-10-27 09:39:33 +00:00
committed by GitHub
parent 607f87c5cd
commit 79e5c3f564
9 changed files with 64 additions and 63 deletions

View File

@@ -276,7 +276,9 @@ function convert_ast(fn) {
function run() { function run() {
var content = options.sourceMap && options.sourceMap.content; var content = options.sourceMap && options.sourceMap.content;
if (content && content != "inline") { if (content && content != "inline") {
UglifyJS.AST_Node.info("Using input source map: " + content); UglifyJS.AST_Node.info("Using input source map: {content}", {
content : content,
});
options.sourceMap.content = read_file(content, content); options.sourceMap.content = read_file(content, content);
} }
try { try {

View File

@@ -137,17 +137,17 @@ var AST_Node = DEFNODE("Node", "start end", {
}, null); }, null);
(AST_Node.log_function = function(fn, verbose) { (AST_Node.log_function = function(fn, verbose) {
var printed = Object.create(null); if (!fn) {
if (fn) {
AST_Node.info = verbose ? function(text, props) {
log("INFO: " + string_template(text, props));
} : noop;
AST_Node.warn = function(text, props) {
log("WARN: " + string_template(text, props));
};
} else {
AST_Node.info = AST_Node.warn = noop; AST_Node.info = AST_Node.warn = noop;
return;
} }
var printed = Object.create(null);
AST_Node.info = verbose ? function(text, props) {
log("INFO: " + string_template(text, props));
} : noop;
AST_Node.warn = function(text, props) {
log("WARN: " + string_template(text, props));
};
function log(msg) { function log(msg) {
if (printed[msg]) return; if (printed[msg]) return;

View File

@@ -192,7 +192,11 @@ merge(Compressor.prototype, {
node.walk(new TreeWalker(function() { node.walk(new TreeWalker(function() {
count++; count++;
})); }));
AST_Node.info("pass " + pass + ": last_count: " + min_count + ", count: " + count); AST_Node.info("pass {pass}: last_count: {min_count}, count: {count}", {
pass: pass,
min_count: min_count,
count: count,
});
if (count < min_count) { if (count < min_count) {
min_count = count; min_count = count;
stopping = false; stopping = false;
@@ -1340,11 +1344,11 @@ merge(Compressor.prototype, {
replaced++; replaced++;
} }
CHANGED = abort = true; CHANGED = abort = true;
AST_Node.info("Collapsing {name} [{file}:{line},{col}]", { AST_Node.info("Collapsing {node} [{file}:{line},{col}]", {
name: node.print_to_string(), node: node,
file: node.start.file, file: node.start.file,
line: node.start.line, line: node.start.line,
col: node.start.col col: node.start.col,
}); });
if (candidate instanceof AST_UnaryPostfix) { if (candidate instanceof AST_UnaryPostfix) {
if (lhs instanceof AST_SymbolRef) lhs.definition().fixed = false; if (lhs instanceof AST_SymbolRef) lhs.definition().fixed = false;
@@ -2799,14 +2803,15 @@ 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_Defun)) { if (!(stat instanceof AST_Definitions || stat instanceof AST_Defun)) {
AST_Node.warn("Dropping unreachable code [{file}:{line},{col}]", stat.start); AST_Node.warn("Dropping unreachable code [{file}:{line},{col}]", stat.start);
} }
var block; var block;
stat.walk(new TreeWalker(function(node, descend) { stat.walk(new TreeWalker(function(node, descend) {
if (node instanceof AST_Definitions) { if (node instanceof AST_Definitions) {
AST_Node.warn("Declarations in unreachable code! [{file}:{line},{col}]", node.start); if (node.remove_initializers(compressor)) {
node.remove_initializers(compressor); AST_Node.warn("Dropping initialization in unreachable code [{file}:{line},{col}]", node.start);
}
push(node); push(node);
return true; return true;
} }
@@ -3273,7 +3278,12 @@ merge(Compressor.prototype, {
} }
function warn(node) { function warn(node) {
AST_Node.warn("global_defs " + node.print_to_string() + " redefined [{file}:{line},{col}]", node.start); AST_Node.warn("global_defs {node} redefined [{file}:{line},{col}]", {
node: node,
file: node.start.file,
line: node.start.line,
col: node.start.col,
});
} }
AST_Toplevel.DEFMETHOD("resolve_defines", function(compressor) { AST_Toplevel.DEFMETHOD("resolve_defines", function(compressor) {
@@ -3878,10 +3888,10 @@ merge(Compressor.prototype, {
return val[key].apply(val, args); return val[key].apply(val, args);
} catch (ex) { } catch (ex) {
AST_Node.warn("Error evaluating {code} [{file}:{line},{col}]", { AST_Node.warn("Error evaluating {code} [{file}:{line},{col}]", {
code: this.print_to_string(), code: this,
file: this.start.file, file: this.start.file,
line: this.start.line, line: this.start.line,
col: this.start.col col: this.start.col,
}); });
} finally { } finally {
if (val instanceof RegExp) val.lastIndex = 0; if (val instanceof RegExp) val.lastIndex = 0;
@@ -4992,7 +5002,7 @@ merge(Compressor.prototype, {
var old_def, var_defs = var_defs_by_id.get(sym.id); var old_def, var_defs = var_defs_by_id.get(sym.id);
if (!def.value) { if (!def.value) {
if (var_defs.length > 1) { if (var_defs.length > 1) {
AST_Node.warn("Dropping duplicated declaration of variable {name} [{file}:{line},{col}]", template(def.name)); AST_Node.info("Dropping declaration of variable {name} [{file}:{line},{col}]", template(def.name));
remove(var_defs, def); remove(var_defs, def);
sym.eliminated++; sym.eliminated++;
} else { } else {
@@ -5226,7 +5236,7 @@ merge(Compressor.prototype, {
name: sym.name, name: sym.name,
file: sym.start.file, file: sym.start.file,
line: sym.start.line, line: sym.start.line,
col : sym.start.col col : sym.start.col,
}; };
} }
@@ -6662,19 +6672,21 @@ merge(Compressor.prototype, {
this.definitions.forEach(function(def) { this.definitions.forEach(function(def) {
def.value = make_node(AST_Undefined, def).optimize(compressor); def.value = make_node(AST_Undefined, def).optimize(compressor);
}); });
return true;
}); });
AST_Let.DEFMETHOD("remove_initializers", function() { function remove_initializers() {
var CHANGED = false;
this.definitions.forEach(function(def) { this.definitions.forEach(function(def) {
if (!def.value) return;
def.value = null; def.value = null;
CHANGED = true;
}); });
}); return CHANGED;
}
AST_Var.DEFMETHOD("remove_initializers", function() { AST_Let.DEFMETHOD("remove_initializers", remove_initializers);
this.definitions.forEach(function(def) { AST_Var.DEFMETHOD("remove_initializers", remove_initializers);
def.value = null;
});
});
AST_Definitions.DEFMETHOD("to_assignments", function(compressor) { AST_Definitions.DEFMETHOD("to_assignments", function(compressor) {
var reduce_vars = compressor.option("reduce_vars"); var reduce_vars = compressor.option("reduce_vars");
@@ -6827,7 +6839,7 @@ merge(Compressor.prototype, {
length: length, length: length,
file: self.start.file, file: self.start.file,
line: self.start.line, line: self.start.line,
col: self.start.col col: self.start.col,
}); });
break; break;
} }
@@ -6884,10 +6896,10 @@ merge(Compressor.prototype, {
})); }));
} catch (ex) { } catch (ex) {
AST_Node.warn("Error converting {expr} [{file}:{line},{col}]", { AST_Node.warn("Error converting {expr} [{file}:{line},{col}]", {
expr: self.print_to_string(), expr: self,
file: self.start.file, file: self.start.file,
line: self.start.line, line: self.start.line,
col: self.start.col col: self.start.col,
}); });
} }
} }
@@ -9302,7 +9314,7 @@ merge(Compressor.prototype, {
prop: self.property, prop: self.property,
file: self.start.file, file: self.start.file,
line: self.start.line, line: self.start.line,
col: self.start.col col: self.start.col,
}); });
} }
var parent = compressor.parent(); var parent = compressor.parent();

View File

@@ -33,7 +33,9 @@ function read_source_map(name, toplevel) {
return to_ascii(match[2]); return to_ascii(match[2]);
} }
} }
AST_Node.warn("inline source map not found: " + name); AST_Node.warn("inline source map not found: {name}", {
name: name,
});
} }
function parse_source_map(content) { function parse_source_map(content) {
@@ -258,6 +260,7 @@ function minify(files, options) {
} catch (ex) { } catch (ex) {
return { error: ex }; return { error: ex };
} finally { } finally {
AST_Node.log_function();
AST_Node.disable_validation(); AST_Node.disable_validation();
} }
} }

View File

@@ -143,8 +143,9 @@ function push_uniq(array, el) {
} }
function string_template(text, props) { function string_template(text, props) {
return text.replace(/\{(.+?)\}/g, function(str, p) { return text.replace(/\{([^}]+)\}/g, function(str, p) {
return props && props[p]; var value = props[p];
return value instanceof AST_Node ? value.print_to_string() : value;
}); });
} }

View File

@@ -315,6 +315,7 @@ function test_case(test) {
if (test.mangle.properties) U.mangle_properties(output, test.mangle.properties); if (test.mangle.properties) U.mangle_properties(output, test.mangle.properties);
} }
var output_code = make_code(output, output_options); var output_code = make_code(output, output_options);
U.AST_Node.log_function();
if (expect != output_code) { if (expect != output_code) {
log([ log([
"!!! failed", "!!! failed",

View File

@@ -83,13 +83,11 @@ ifs_3_should_warn: {
"WARN: Condition left of && always false [test/compress/conditionals.js:3,12]", "WARN: Condition left of && always false [test/compress/conditionals.js:3,12]",
"WARN: Condition always false [test/compress/conditionals.js:3,12]", "WARN: Condition always false [test/compress/conditionals.js:3,12]",
"WARN: Dropping unreachable code [test/compress/conditionals.js:3,34]", "WARN: Dropping unreachable code [test/compress/conditionals.js:3,34]",
"WARN: Declarations in unreachable code! [test/compress/conditionals.js:4,12]",
"WARN: + in boolean context always true [test/compress/conditionals.js:10,19]", "WARN: + in boolean context always true [test/compress/conditionals.js:10,19]",
"WARN: Boolean || always true [test/compress/conditionals.js:10,12]", "WARN: Boolean || always true [test/compress/conditionals.js:10,12]",
"WARN: Condition left of || always true [test/compress/conditionals.js:10,12]", "WARN: Condition left of || always true [test/compress/conditionals.js:10,12]",
"WARN: Condition always true [test/compress/conditionals.js:10,12]", "WARN: Condition always true [test/compress/conditionals.js:10,12]",
"WARN: Dropping unreachable code [test/compress/conditionals.js:12,15]", "WARN: Dropping unreachable code [test/compress/conditionals.js:12,15]",
"WARN: Declarations in unreachable code! [test/compress/conditionals.js:13,12]",
"WARN: Dropping side-effect-free statement [test/compress/conditionals.js:3,12]", "WARN: Dropping side-effect-free statement [test/compress/conditionals.js:3,12]",
"WARN: Dropping side-effect-free statement [test/compress/conditionals.js:10,12]", "WARN: Dropping side-effect-free statement [test/compress/conditionals.js:10,12]",
] ]

View File

@@ -61,8 +61,6 @@ dead_code_2_should_warn: {
expect_stdout: true expect_stdout: true
expect_warnings: [ expect_warnings: [
"WARN: Dropping unreachable code [test/compress/dead-code.js:8,12]", "WARN: Dropping unreachable code [test/compress/dead-code.js:8,12]",
"WARN: Declarations in unreachable code! [test/compress/dead-code.js:10,16]",
"WARN: Dropping unreachable code [test/compress/dead-code.js:10,16]",
] ]
node_version: "<=4" node_version: "<=4"
} }
@@ -103,11 +101,9 @@ dead_code_constant_boolean_should_warn_more: {
"WARN: + in boolean context always true [test/compress/dead-code.js:1,33]", "WARN: + in boolean context always true [test/compress/dead-code.js:1,33]",
"WARN: Boolean || always true [test/compress/dead-code.js:1,16]", "WARN: Boolean || always true [test/compress/dead-code.js:1,16]",
"WARN: Dropping unreachable code [test/compress/dead-code.js:1,45]", "WARN: Dropping unreachable code [test/compress/dead-code.js:1,45]",
"WARN: Declarations in unreachable code! [test/compress/dead-code.js:3,12]",
"WARN: Boolean expression always true [test/compress/dead-code.js:6,47]", "WARN: Boolean expression always true [test/compress/dead-code.js:6,47]",
"WARN: Boolean && always false [test/compress/dead-code.js:6,28]", "WARN: Boolean && always false [test/compress/dead-code.js:6,28]",
"WARN: Dropping unreachable code [test/compress/dead-code.js:6,63]", "WARN: Dropping unreachable code [test/compress/dead-code.js:6,63]",
"WARN: Declarations in unreachable code! [test/compress/dead-code.js:9,12]",
"WARN: Dropping side-effect-free statement [test/compress/dead-code.js:1,15]", "WARN: Dropping side-effect-free statement [test/compress/dead-code.js:1,15]",
"WARN: Dropping side-effect-free statement [test/compress/dead-code.js:6,28]", "WARN: Dropping side-effect-free statement [test/compress/dead-code.js:6,28]",
] ]

View File

@@ -39,7 +39,7 @@ non_hoisted_function_after_return: {
"WARN: Dropping unreachable code [test/compress/issue-1034.js:4,16]", "WARN: Dropping unreachable code [test/compress/issue-1034.js:4,16]",
"WARN: Dropping unreachable code [test/compress/issue-1034.js:7,16]", "WARN: Dropping unreachable code [test/compress/issue-1034.js:7,16]",
"WARN: Dropping unreachable code [test/compress/issue-1034.js:10,12]", "WARN: Dropping unreachable code [test/compress/issue-1034.js:10,12]",
"WARN: Dropping unused function UnusedFunction [test/compress/issue-1034.js:11,21]" "WARN: Dropping unused function UnusedFunction [test/compress/issue-1034.js:11,21]",
] ]
} }
@@ -84,15 +84,12 @@ non_hoisted_function_after_return_2a: {
} }
} }
expect_warnings: [ expect_warnings: [
"WARN: Dropping unreachable code [test/compress/issue-1034.js:4,16]", "WARN: Dropping initialization in unreachable code [test/compress/issue-1034.js:4,16]",
"WARN: Declarations in unreachable code! [test/compress/issue-1034.js:4,16]", "WARN: Dropping initialization in unreachable code [test/compress/issue-1034.js:7,16]",
"WARN: Dropping unreachable code [test/compress/issue-1034.js:7,16]",
"WARN: Declarations in unreachable code! [test/compress/issue-1034.js:7,16]",
"WARN: Dropping unused variable a [test/compress/issue-1034.js:4,20]", "WARN: Dropping unused variable a [test/compress/issue-1034.js:4,20]",
"WARN: Dropping unused function nope [test/compress/issue-1034.js:11,21]", "WARN: Dropping unused function nope [test/compress/issue-1034.js:11,21]",
"INFO: pass 0: last_count: Infinity, count: 35", "INFO: pass 0: last_count: Infinity, count: 35",
"WARN: Dropping unreachable code [test/compress/issue-1034.js:9,12]", "WARN: Dropping initialization in unreachable code [test/compress/issue-1034.js:9,12]",
"WARN: Declarations in unreachable code! [test/compress/issue-1034.js:9,12]",
"WARN: Dropping unreachable code [test/compress/issue-1034.js:12,12]", "WARN: Dropping unreachable code [test/compress/issue-1034.js:12,12]",
"INFO: Dropping unused variable b [test/compress/issue-1034.js:7,20]", "INFO: Dropping unused variable b [test/compress/issue-1034.js:7,20]",
"INFO: Dropping unused variable c [test/compress/issue-1034.js:9,16]", "INFO: Dropping unused variable c [test/compress/issue-1034.js:9,16]",
@@ -138,10 +135,7 @@ non_hoisted_function_after_return_2b: {
} }
} }
expect_warnings: [ expect_warnings: [
"WARN: Dropping unreachable code [test/compress/issue-1034.js:6,16]", "WARN: Dropping initialization in unreachable code [test/compress/issue-1034.js:8,12]",
"WARN: Declarations in unreachable code! [test/compress/issue-1034.js:6,16]",
"WARN: Dropping unreachable code [test/compress/issue-1034.js:8,12]",
"WARN: Declarations in unreachable code! [test/compress/issue-1034.js:8,12]",
"WARN: Dropping unreachable code [test/compress/issue-1034.js:12,12]", "WARN: Dropping unreachable code [test/compress/issue-1034.js:12,12]",
] ]
} }
@@ -242,15 +236,12 @@ non_hoisted_function_after_return_2a_strict: {
} }
expect_stdout: "5 6" expect_stdout: "5 6"
expect_warnings: [ expect_warnings: [
"WARN: Dropping unreachable code [test/compress/issue-1034.js:5,16]", "WARN: Dropping initialization in unreachable code [test/compress/issue-1034.js:5,16]",
"WARN: Declarations in unreachable code! [test/compress/issue-1034.js:5,16]", "WARN: Dropping initialization in unreachable code [test/compress/issue-1034.js:8,16]",
"WARN: Dropping unreachable code [test/compress/issue-1034.js:8,16]",
"WARN: Declarations in unreachable code! [test/compress/issue-1034.js:8,16]",
"WARN: Dropping unused variable a [test/compress/issue-1034.js:5,20]", "WARN: Dropping unused variable a [test/compress/issue-1034.js:5,20]",
"WARN: Dropping unused function nope [test/compress/issue-1034.js:12,21]", "WARN: Dropping unused function nope [test/compress/issue-1034.js:12,21]",
"INFO: pass 0: last_count: Infinity, count: 46", "INFO: pass 0: last_count: Infinity, count: 46",
"WARN: Dropping unreachable code [test/compress/issue-1034.js:10,12]", "WARN: Dropping initialization in unreachable code [test/compress/issue-1034.js:10,12]",
"WARN: Declarations in unreachable code! [test/compress/issue-1034.js:10,12]",
"WARN: Dropping unreachable code [test/compress/issue-1034.js:13,12]", "WARN: Dropping unreachable code [test/compress/issue-1034.js:13,12]",
"INFO: Dropping unused variable b [test/compress/issue-1034.js:8,20]", "INFO: Dropping unused variable b [test/compress/issue-1034.js:8,20]",
"INFO: Dropping unused variable c [test/compress/issue-1034.js:10,16]", "INFO: Dropping unused variable c [test/compress/issue-1034.js:10,16]",
@@ -301,10 +292,7 @@ non_hoisted_function_after_return_2b_strict: {
} }
expect_stdout: "5 6" expect_stdout: "5 6"
expect_warnings: [ expect_warnings: [
"WARN: Dropping unreachable code [test/compress/issue-1034.js:7,16]", "WARN: Dropping initialization in unreachable code [test/compress/issue-1034.js:9,12]",
"WARN: Declarations in unreachable code! [test/compress/issue-1034.js:7,16]",
"WARN: Dropping unreachable code [test/compress/issue-1034.js:9,12]",
"WARN: Declarations in unreachable code! [test/compress/issue-1034.js:9,12]",
"WARN: Dropping unreachable code [test/compress/issue-1034.js:13,12]", "WARN: Dropping unreachable code [test/compress/issue-1034.js:13,12]",
] ]
} }