minor clean-ups (#5604)
This commit is contained in:
parent
a9d9af53e9
commit
387c69b194
|
|
@ -128,12 +128,7 @@ var AST_Node = DEFNODE("Node", "start end", {
|
||||||
var marker = {};
|
var marker = {};
|
||||||
this.walk(new TreeWalker(function(node) {
|
this.walk(new TreeWalker(function(node) {
|
||||||
if (node.validate_visited === marker) {
|
if (node.validate_visited === marker) {
|
||||||
throw new Error(string_template("cannot reuse {type} from [{file}:{line},{col}]", {
|
throw new Error(string_template("cannot reuse AST_{TYPE} from [{start}]", node));
|
||||||
type: "AST_" + node.TYPE,
|
|
||||||
file: node.start.file,
|
|
||||||
line: node.start.line,
|
|
||||||
col: node.start.col,
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
node.validate_visited = marker;
|
node.validate_visited = marker;
|
||||||
}));
|
}));
|
||||||
|
|
|
||||||
180
lib/compress.js
180
lib/compress.js
|
|
@ -2057,12 +2057,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
&& node instanceof AST_Assign && node.operator != "=" && node.left.equals(lhs)) {
|
&& node instanceof AST_Assign && node.operator != "=" && node.left.equals(lhs)) {
|
||||||
replaced++;
|
replaced++;
|
||||||
changed = true;
|
changed = true;
|
||||||
AST_Node.info("Cascading {node} [{file}:{line},{col}]", {
|
AST_Node.info("Cascading {this} [{start}]", node);
|
||||||
node: node,
|
|
||||||
file: node.start.file,
|
|
||||||
line: node.start.line,
|
|
||||||
col: node.start.col,
|
|
||||||
});
|
|
||||||
can_replace = false;
|
can_replace = false;
|
||||||
lvalues = get_lvalues(lhs);
|
lvalues = get_lvalues(lhs);
|
||||||
node.right.transform(scanner);
|
node.right.transform(scanner);
|
||||||
|
|
@ -2119,12 +2114,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
}
|
}
|
||||||
replaced++;
|
replaced++;
|
||||||
changed = abort = true;
|
changed = abort = true;
|
||||||
AST_Node.info("Collapsing {node} [{file}:{line},{col}]", {
|
AST_Node.info("Collapsing {this} [{start}]", node);
|
||||||
node: node,
|
|
||||||
file: node.start.file,
|
|
||||||
line: node.start.line,
|
|
||||||
col: node.start.col,
|
|
||||||
});
|
|
||||||
if (candidate.TYPE == "Binary") {
|
if (candidate.TYPE == "Binary") {
|
||||||
update_symbols(candidate, node);
|
update_symbols(candidate, node);
|
||||||
return make_node(AST_Assign, candidate, {
|
return make_node(AST_Assign, candidate, {
|
||||||
|
|
@ -2252,12 +2242,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
if (node instanceof AST_SymbolRef && node.definition() === def) {
|
if (node instanceof AST_SymbolRef && node.definition() === def) {
|
||||||
if (is_lhs(node, multi_replacer.parent())) return node;
|
if (is_lhs(node, multi_replacer.parent())) return node;
|
||||||
if (!--replaced) abort = true;
|
if (!--replaced) abort = true;
|
||||||
AST_Node.info("Replacing {node} [{file}:{line},{col}]", {
|
AST_Node.info("Replacing {this} [{start}]", node);
|
||||||
node: node,
|
|
||||||
file: node.start.file,
|
|
||||||
line: node.start.line,
|
|
||||||
col: node.start.col,
|
|
||||||
});
|
|
||||||
var ref = rvalue.clone();
|
var ref = rvalue.clone();
|
||||||
ref.scope = node.scope;
|
ref.scope = node.scope;
|
||||||
ref.reference();
|
ref.reference();
|
||||||
|
|
@ -4282,7 +4267,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
if (node instanceof AST_Definitions) {
|
if (node instanceof AST_Definitions) {
|
||||||
var defns = [];
|
var defns = [];
|
||||||
if (node.remove_initializers(compressor, defns)) {
|
if (node.remove_initializers(compressor, defns)) {
|
||||||
AST_Node.warn("Dropping initialization in unreachable code [{file}:{line},{col}]", node.start);
|
AST_Node.warn("Dropping initialization in unreachable code [{start}]", node);
|
||||||
}
|
}
|
||||||
if (defns.length > 0) {
|
if (defns.length > 0) {
|
||||||
node.definitions = defns;
|
node.definitions = defns;
|
||||||
|
|
@ -4309,7 +4294,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
}
|
}
|
||||||
if (!(node instanceof AST_LoopControl)) dropped = true;
|
if (!(node instanceof AST_LoopControl)) dropped = true;
|
||||||
}));
|
}));
|
||||||
if (dropped) AST_Node.warn("Dropping unreachable code [{file}:{line},{col}]", stat.start);
|
if (dropped) AST_Node.warn("Dropping unreachable code [{start}]", stat);
|
||||||
|
|
||||||
function push(node) {
|
function push(node) {
|
||||||
if (block) {
|
if (block) {
|
||||||
|
|
@ -4785,12 +4770,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function warn(node) {
|
function warn(node) {
|
||||||
AST_Node.warn("global_defs {node} redefined [{file}:{line},{col}]", {
|
AST_Node.warn("global_defs {this} redefined [{start}]", node);
|
||||||
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) {
|
||||||
|
|
@ -5042,7 +5022,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
def(AST_Statement, function() {
|
def(AST_Statement, function() {
|
||||||
throw new Error(string_template("Cannot evaluate a statement [{file}:{line},{col}]", this.start));
|
throw new Error(string_template("Cannot evaluate a statement [{start}]", this));
|
||||||
});
|
});
|
||||||
def(AST_Accessor, return_this);
|
def(AST_Accessor, return_this);
|
||||||
def(AST_BigInt, return_this);
|
def(AST_BigInt, return_this);
|
||||||
|
|
@ -5468,12 +5448,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
try {
|
try {
|
||||||
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 {this} [{start}]", this);
|
||||||
code: this,
|
|
||||||
file: this.start.file,
|
|
||||||
line: this.start.line,
|
|
||||||
col: this.start.col,
|
|
||||||
});
|
|
||||||
} finally {
|
} finally {
|
||||||
if (val instanceof RegExp) val.lastIndex = 0;
|
if (val instanceof RegExp) val.lastIndex = 0;
|
||||||
}
|
}
|
||||||
|
|
@ -7291,7 +7266,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
if (value && ((index = indexOf_assign(sym, def)) < 0 || self_assign(value.tail_node()))) {
|
if (value && ((index = indexOf_assign(sym, def)) < 0 || self_assign(value.tail_node()))) {
|
||||||
def = def.clone();
|
def = def.clone();
|
||||||
value = value.drop_side_effect_free(compressor);
|
value = value.drop_side_effect_free(compressor);
|
||||||
if (value) AST_Node.warn("Side effects in definition of variable {name} [{file}:{line},{col}]", template(def.name));
|
if (value) AST_Node.warn("Side effects in definition of variable {name} [{start}]", def.name);
|
||||||
if (node instanceof AST_Const) {
|
if (node instanceof AST_Const) {
|
||||||
def.value = value || make_node(AST_Number, def, { value: 0 });
|
def.value = value || make_node(AST_Number, def, { value: 0 });
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -7306,7 +7281,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
if (parent instanceof AST_ExportDeclaration) {
|
if (parent instanceof AST_ExportDeclaration) {
|
||||||
flush();
|
flush();
|
||||||
} else if (drop_sym && var_defs[sym.id] > 1) {
|
} else if (drop_sym && var_defs[sym.id] > 1) {
|
||||||
AST_Node.info("Dropping declaration of variable {name} [{file}:{line},{col}]", template(def.name));
|
AST_Node.info("Dropping declaration of variable {name} [{start}]", def.name);
|
||||||
var_defs[sym.id]--;
|
var_defs[sym.id]--;
|
||||||
sym.eliminated++;
|
sym.eliminated++;
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -7324,7 +7299,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
&& assigned_once(fn, sym.references)
|
&& assigned_once(fn, sym.references)
|
||||||
&& can_declare_defun(fn)
|
&& can_declare_defun(fn)
|
||||||
&& (old_def = rename_def(fn, def.name.name)) !== false) {
|
&& (old_def = rename_def(fn, def.name.name)) !== false) {
|
||||||
AST_Node.warn("Declaring {name} as function [{file}:{line},{col}]", template(def.name));
|
AST_Node.warn("Declaring {name} as function [{start}]", def.name);
|
||||||
var ctor;
|
var ctor;
|
||||||
switch (fn.CTOR) {
|
switch (fn.CTOR) {
|
||||||
case AST_AsyncFunction:
|
case AST_AsyncFunction:
|
||||||
|
|
@ -7364,7 +7339,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
value = value && value.drop_side_effect_free(compressor);
|
value = value && value.drop_side_effect_free(compressor);
|
||||||
if (value) side_effects.push(value);
|
if (value) side_effects.push(value);
|
||||||
if (var_defs[sym.id] > 1) {
|
if (var_defs[sym.id] > 1) {
|
||||||
AST_Node.warn("Dropping duplicated declaration of variable {name} [{file}:{line},{col}]", template(def.name));
|
AST_Node.warn("Dropping duplicated declaration of variable {name} [{start}]", def.name);
|
||||||
var_defs[sym.id]--;
|
var_defs[sym.id]--;
|
||||||
sym.eliminated++;
|
sym.eliminated++;
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -7374,7 +7349,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
} else {
|
} else {
|
||||||
value = value && value.drop_side_effect_free(compressor);
|
value = value && value.drop_side_effect_free(compressor);
|
||||||
if (value) {
|
if (value) {
|
||||||
AST_Node.warn("Side effects in initialization of unused variable {name} [{file}:{line},{col}]", template(def.name));
|
AST_Node.warn("Side effects in initialization of unused variable {name} [{start}]", def.name);
|
||||||
side_effects.push(value);
|
side_effects.push(value);
|
||||||
} else {
|
} else {
|
||||||
log(def.name, "Dropping unused variable {name}");
|
log(def.name, "Dropping unused variable {name}");
|
||||||
|
|
@ -7459,7 +7434,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
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.info("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", template(def.name));
|
AST_Node.info("Dropping duplicated definition of variable {name} [{start}]", 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);
|
||||||
|
|
@ -7566,7 +7541,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
var body = [];
|
var body = [];
|
||||||
var value = node.object.drop_side_effect_free(compressor);
|
var value = node.object.drop_side_effect_free(compressor);
|
||||||
if (value) {
|
if (value) {
|
||||||
AST_Node.warn("Side effects in object of for-in loop [{file}:{line},{col}]", value.start);
|
AST_Node.warn("Side effects in object of for-in loop [{start}]", value);
|
||||||
body.push(make_node(AST_SimpleStatement, node, {
|
body.push(make_node(AST_SimpleStatement, node, {
|
||||||
body: value
|
body: value
|
||||||
}));
|
}));
|
||||||
|
|
@ -7599,31 +7574,17 @@ Compressor.prototype.compress = function(node) {
|
||||||
});
|
});
|
||||||
|
|
||||||
function log(sym, text) {
|
function log(sym, text) {
|
||||||
AST_Node[sym.definition().references.length > 0 ? "info" : "warn"](text + " [{file}:{line},{col}]", template(sym));
|
AST_Node[sym.definition().references.length > 0 ? "info" : "warn"](text + " [{start}]", sym);
|
||||||
}
|
}
|
||||||
|
|
||||||
function log_default(node, text) {
|
function log_default(node, text) {
|
||||||
if (node.name instanceof AST_SymbolFunarg) {
|
if (node.name instanceof AST_SymbolFunarg) {
|
||||||
log(node.name, text);
|
log(node.name, text);
|
||||||
} else {
|
} else {
|
||||||
AST_Node.info(text + " [{file}:{line},{col}]", {
|
AST_Node.info(text + " [{start}]", node);
|
||||||
name: node,
|
|
||||||
file: node.start.file,
|
|
||||||
line: node.start.line,
|
|
||||||
col : node.start.col,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function template(sym) {
|
|
||||||
return {
|
|
||||||
name: sym.name,
|
|
||||||
file: sym.start.file,
|
|
||||||
line: sym.start.line,
|
|
||||||
col : sym.start.col,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function get_rvalue(expr) {
|
function get_rvalue(expr) {
|
||||||
return expr[expr instanceof AST_Assign ? "right" : "value"];
|
return expr[expr instanceof AST_Assign ? "right" : "value"];
|
||||||
}
|
}
|
||||||
|
|
@ -8797,7 +8758,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
def(AST_Call, function(compressor, first_in_statement) {
|
def(AST_Call, function(compressor, first_in_statement) {
|
||||||
var self = this;
|
var self = this;
|
||||||
if (self.is_expr_pure(compressor)) {
|
if (self.is_expr_pure(compressor)) {
|
||||||
if (self.pure) AST_Node.warn("Dropping __PURE__ call [{file}:{line},{col}]", self.start);
|
if (self.pure) AST_Node.warn("Dropping __PURE__ call [{start}]", self);
|
||||||
var args = trim(self.args, compressor, first_in_statement, array_spread);
|
var args = trim(self.args, compressor, first_in_statement, array_spread);
|
||||||
return args && make_sequence(self, args.map(convert_spread));
|
return args && make_sequence(self, args.map(convert_spread));
|
||||||
}
|
}
|
||||||
|
|
@ -9074,7 +9035,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
var body = self.body;
|
var body = self.body;
|
||||||
var node = body.drop_side_effect_free(compressor, true);
|
var node = body.drop_side_effect_free(compressor, true);
|
||||||
if (!node) {
|
if (!node) {
|
||||||
AST_Node.warn("Dropping side-effect-free statement [{file}:{line},{col}]", self.start);
|
AST_Node.warn("Dropping side-effect-free statement [{start}]", self);
|
||||||
return make_node(AST_EmptyStatement, self);
|
return make_node(AST_EmptyStatement, self);
|
||||||
}
|
}
|
||||||
if (node !== body) {
|
if (node !== body) {
|
||||||
|
|
@ -9531,7 +9492,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
if (compressor.option("dead_code")) {
|
if (compressor.option("dead_code")) {
|
||||||
var cond = fuzzy_eval(compressor, self.condition);
|
var cond = fuzzy_eval(compressor, self.condition);
|
||||||
if (!cond) {
|
if (!cond) {
|
||||||
AST_Node.warn("Condition always false [{file}:{line},{col}]", self.condition.start);
|
AST_Node.warn("Condition always false [{start}]", self.condition);
|
||||||
var body = [
|
var body = [
|
||||||
make_node(AST_SimpleStatement, self.condition, { body: self.condition }).transform(compressor),
|
make_node(AST_SimpleStatement, self.condition, { body: self.condition }).transform(compressor),
|
||||||
];
|
];
|
||||||
|
|
@ -9539,7 +9500,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
if (self.alternative) body.push(self.alternative);
|
if (self.alternative) body.push(self.alternative);
|
||||||
return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor);
|
return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor);
|
||||||
} else if (!(cond instanceof AST_Node)) {
|
} else if (!(cond instanceof AST_Node)) {
|
||||||
AST_Node.warn("Condition always true [{file}:{line},{col}]", self.condition.start);
|
AST_Node.warn("Condition always true [{start}]", self.condition);
|
||||||
var body = [
|
var body = [
|
||||||
make_node(AST_SimpleStatement, self.condition, { body: self.condition }).transform(compressor),
|
make_node(AST_SimpleStatement, self.condition, { body: self.condition }).transform(compressor),
|
||||||
self.body,
|
self.body,
|
||||||
|
|
@ -10318,11 +10279,9 @@ Compressor.prototype.compress = function(node) {
|
||||||
for (var i = 0; i < length; i++) elements[i] = make_node(AST_Hole, self);
|
for (var i = 0; i < length; i++) elements[i] = make_node(AST_Hole, self);
|
||||||
return make_node(AST_Array, self, { elements: elements });
|
return make_node(AST_Array, self, { elements: elements });
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
AST_Node.warn("Invalid array length: {length} [{file}:{line},{col}]", {
|
AST_Node.warn("Invalid array length: {length} [{start}]", {
|
||||||
length: length,
|
length: length,
|
||||||
file: self.start.file,
|
start: self.start,
|
||||||
line: self.start.line,
|
|
||||||
col: self.start.col,
|
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -10381,12 +10340,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
value: RegExp.apply(RegExp, params),
|
value: RegExp.apply(RegExp, params),
|
||||||
}));
|
}));
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
AST_Node.warn("Error converting {expr} [{file}:{line},{col}]", {
|
AST_Node.warn("Error converting {this} [{start}]", self);
|
||||||
expr: self,
|
|
||||||
file: self.start.file,
|
|
||||||
line: self.start.line,
|
|
||||||
col: self.start.col,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
} else if (exp instanceof AST_Dot) switch (exp.property) {
|
} else if (exp instanceof AST_Dot) switch (exp.property) {
|
||||||
|
|
@ -10585,7 +10539,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
return self;
|
return self;
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
if (ex instanceof JS_Parse_Error) {
|
if (ex instanceof JS_Parse_Error) {
|
||||||
AST_Node.warn("Error parsing code passed to new Function [{file}:{line},{col}]", self.args[self.args.length - 1].start);
|
AST_Node.warn("Error parsing code passed to new Function [{start}]", self.args[self.args.length - 1]);
|
||||||
AST_Node.warn(ex.toString());
|
AST_Node.warn(ex.toString());
|
||||||
} else {
|
} else {
|
||||||
throw ex;
|
throw ex;
|
||||||
|
|
@ -11341,7 +11295,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
if (!compressor.option("booleans")) break;
|
if (!compressor.option("booleans")) break;
|
||||||
if (!compressor.in_boolean_context()) break;
|
if (!compressor.in_boolean_context()) break;
|
||||||
// typeof always returns a non-empty string, thus always truthy
|
// typeof always returns a non-empty string, thus always truthy
|
||||||
AST_Node.warn("Boolean expression always true [{file}:{line},{col}]", self.start);
|
AST_Node.warn("Boolean expression always true [{start}]", self);
|
||||||
var exprs = [ make_node(AST_True, self) ];
|
var exprs = [ make_node(AST_True, self) ];
|
||||||
if (!(exp instanceof AST_SymbolRef && can_drop_symbol(exp, compressor))) exprs.unshift(exp);
|
if (!(exp instanceof AST_SymbolRef && can_drop_symbol(exp, compressor))) exprs.unshift(exp);
|
||||||
return make_sequence(self, exprs).optimize(compressor);
|
return make_sequence(self, exprs).optimize(compressor);
|
||||||
|
|
@ -11587,7 +11541,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
case "===":
|
case "===":
|
||||||
case "!==":
|
case "!==":
|
||||||
if (is_undefined(self.left, compressor) && self.right.is_defined(compressor)) {
|
if (is_undefined(self.left, compressor) && self.right.is_defined(compressor)) {
|
||||||
AST_Node.warn("Expression always defined [{file}:{line},{col}]", self.start);
|
AST_Node.warn("Expression always defined [{start}]", self);
|
||||||
return make_sequence(self, [
|
return make_sequence(self, [
|
||||||
self.right,
|
self.right,
|
||||||
make_node(self.operator == "===" ? AST_False : AST_True, self)
|
make_node(self.operator == "===" ? AST_False : AST_True, self)
|
||||||
|
|
@ -11667,7 +11621,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
case "+":
|
case "+":
|
||||||
var ev = self.left.evaluate(compressor, true);
|
var ev = self.left.evaluate(compressor, true);
|
||||||
if (ev && typeof ev == "string" || (ev = self.right.evaluate(compressor, true)) && typeof ev == "string") {
|
if (ev && typeof ev == "string" || (ev = self.right.evaluate(compressor, true)) && typeof ev == "string") {
|
||||||
AST_Node.warn("+ in boolean context always true [{file}:{line},{col}]", self.start);
|
AST_Node.warn("+ in boolean context always true [{start}]", self);
|
||||||
var exprs = [];
|
var exprs = [];
|
||||||
if (self.left.evaluate(compressor) instanceof AST_Node) exprs.push(self.left);
|
if (self.left.evaluate(compressor) instanceof AST_Node) exprs.push(self.left);
|
||||||
if (self.right.evaluate(compressor) instanceof AST_Node) exprs.push(self.right);
|
if (self.right.evaluate(compressor) instanceof AST_Node) exprs.push(self.right);
|
||||||
|
|
@ -11752,20 +11706,20 @@ Compressor.prototype.compress = function(node) {
|
||||||
case "&&":
|
case "&&":
|
||||||
var ll = fuzzy_eval(compressor, self.left);
|
var ll = fuzzy_eval(compressor, self.left);
|
||||||
if (!ll) {
|
if (!ll) {
|
||||||
AST_Node.warn("Condition left of && always false [{file}:{line},{col}]", self.start);
|
AST_Node.warn("Condition left of && always false [{start}]", self);
|
||||||
return maintain_this_binding(parent, compressor.self(), self.left).optimize(compressor);
|
return maintain_this_binding(parent, compressor.self(), self.left).optimize(compressor);
|
||||||
} else if (!(ll instanceof AST_Node)) {
|
} else if (!(ll instanceof AST_Node)) {
|
||||||
AST_Node.warn("Condition left of && always true [{file}:{line},{col}]", self.start);
|
AST_Node.warn("Condition left of && always true [{start}]", self);
|
||||||
return make_sequence(self, [ self.left, self.right ]).optimize(compressor);
|
return make_sequence(self, [ self.left, self.right ]).optimize(compressor);
|
||||||
}
|
}
|
||||||
if (!self.right.evaluate(compressor, true)) {
|
if (!self.right.evaluate(compressor, true)) {
|
||||||
if (in_bool && !(self.right.evaluate(compressor) instanceof AST_Node)) {
|
if (in_bool && !(self.right.evaluate(compressor) instanceof AST_Node)) {
|
||||||
AST_Node.warn("Boolean && always false [{file}:{line},{col}]", self.start);
|
AST_Node.warn("Boolean && always false [{start}]", self);
|
||||||
return make_sequence(self, [ self.left, make_node(AST_False, self) ]).optimize(compressor);
|
return make_sequence(self, [ self.left, make_node(AST_False, self) ]).optimize(compressor);
|
||||||
} else self.falsy = true;
|
} else self.falsy = true;
|
||||||
} else if ((in_bool || parent.operator == "&&" && parent.left === compressor.self())
|
} else if ((in_bool || parent.operator == "&&" && parent.left === compressor.self())
|
||||||
&& !(self.right.evaluate(compressor) instanceof AST_Node)) {
|
&& !(self.right.evaluate(compressor) instanceof AST_Node)) {
|
||||||
AST_Node.warn("Dropping side-effect-free && [{file}:{line},{col}]", self.start);
|
AST_Node.warn("Dropping side-effect-free && [{start}]", self);
|
||||||
return self.left.optimize(compressor);
|
return self.left.optimize(compressor);
|
||||||
}
|
}
|
||||||
// (x || false) && y ---> x ? y : false
|
// (x || false) && y ---> x ? y : false
|
||||||
|
|
@ -11774,7 +11728,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
if (!lr) return make_node(AST_Conditional, self, {
|
if (!lr) return make_node(AST_Conditional, self, {
|
||||||
condition: self.left.left,
|
condition: self.left.left,
|
||||||
consequent: self.right,
|
consequent: self.right,
|
||||||
alternative: self.left.right
|
alternative: self.left.right,
|
||||||
}).optimize(compressor);
|
}).optimize(compressor);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
@ -11783,38 +11737,29 @@ Compressor.prototype.compress = function(node) {
|
||||||
case "||":
|
case "||":
|
||||||
var ll = fuzzy_eval(compressor, self.left, nullish);
|
var ll = fuzzy_eval(compressor, self.left, nullish);
|
||||||
if (nullish ? ll == null : !ll) {
|
if (nullish ? ll == null : !ll) {
|
||||||
AST_Node.warn("Condition left of {operator} always {value} [{file}:{line},{col}]", {
|
AST_Node.warn("Condition left of {operator} always {value} [{start}]", {
|
||||||
operator: self.operator,
|
operator: self.operator,
|
||||||
value: nullish ? "nulish" : "false",
|
value: nullish ? "nulish" : "false",
|
||||||
file: self.start.file,
|
start: self.start,
|
||||||
line: self.start.line,
|
|
||||||
col: self.start.col,
|
|
||||||
});
|
});
|
||||||
return make_sequence(self, [ self.left, self.right ]).optimize(compressor);
|
return make_sequence(self, [ self.left, self.right ]).optimize(compressor);
|
||||||
} else if (!(ll instanceof AST_Node)) {
|
} else if (!(ll instanceof AST_Node)) {
|
||||||
AST_Node.warn("Condition left of {operator} always {value} [{file}:{line},{col}]", {
|
AST_Node.warn("Condition left of {operator} always {value} [{start}]", {
|
||||||
operator: self.operator,
|
operator: self.operator,
|
||||||
value: nullish ? "defined" : "true",
|
value: nullish ? "defined" : "true",
|
||||||
file: self.start.file,
|
start: self.start,
|
||||||
line: self.start.line,
|
|
||||||
col: self.start.col,
|
|
||||||
});
|
});
|
||||||
return maintain_this_binding(parent, compressor.self(), self.left).optimize(compressor);
|
return maintain_this_binding(parent, compressor.self(), self.left).optimize(compressor);
|
||||||
}
|
}
|
||||||
var rr;
|
var rr;
|
||||||
if (!nullish && (rr = self.right.evaluate(compressor, true)) && !(rr instanceof AST_Node)) {
|
if (!nullish && (rr = self.right.evaluate(compressor, true)) && !(rr instanceof AST_Node)) {
|
||||||
if (in_bool && !(self.right.evaluate(compressor) instanceof AST_Node)) {
|
if (in_bool && !(self.right.evaluate(compressor) instanceof AST_Node)) {
|
||||||
AST_Node.warn("Boolean || always true [{file}:{line},{col}]", self.start);
|
AST_Node.warn("Boolean || always true [{start}]", self);
|
||||||
return make_sequence(self, [ self.left, make_node(AST_True, self) ]).optimize(compressor);
|
return make_sequence(self, [ self.left, make_node(AST_True, self) ]).optimize(compressor);
|
||||||
} else self.truthy = true;
|
} else self.truthy = true;
|
||||||
} else if ((in_bool || parent.operator == "||" && parent.left === compressor.self())
|
} else if ((in_bool || parent.operator == "||" && parent.left === compressor.self())
|
||||||
&& !self.right.evaluate(compressor)) {
|
&& !self.right.evaluate(compressor)) {
|
||||||
AST_Node.warn("Dropping side-effect-free {operator} [{file}:{line},{col}]", {
|
AST_Node.warn("Dropping side-effect-free {operator} [{start}]", self);
|
||||||
operator: self.operator,
|
|
||||||
file: self.start.file,
|
|
||||||
line: self.start.line,
|
|
||||||
col: self.start.col,
|
|
||||||
});
|
|
||||||
return self.left.optimize(compressor);
|
return self.left.optimize(compressor);
|
||||||
}
|
}
|
||||||
// x && true || y ---> x ? true : y
|
// x && true || y ---> x ? true : y
|
||||||
|
|
@ -11823,7 +11768,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
if (lr && !(lr instanceof AST_Node)) return make_node(AST_Conditional, self, {
|
if (lr && !(lr instanceof AST_Node)) return make_node(AST_Conditional, self, {
|
||||||
condition: self.left.left,
|
condition: self.left.left,
|
||||||
consequent: self.left.right,
|
consequent: self.left.right,
|
||||||
alternative: self.right
|
alternative: self.right,
|
||||||
}).optimize(compressor);
|
}).optimize(compressor);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
@ -11839,9 +11784,9 @@ Compressor.prototype.compress = function(node) {
|
||||||
left: make_node(AST_String, self.left, {
|
left: make_node(AST_String, self.left, {
|
||||||
value: "" + self.left.value + self.right.left.value,
|
value: "" + self.left.value + self.right.left.value,
|
||||||
start: self.left.start,
|
start: self.left.start,
|
||||||
end: self.right.left.end
|
end: self.right.left.end,
|
||||||
}),
|
}),
|
||||||
right: self.right.right
|
right: self.right.right,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// (x + "foo") + "bar" ---> x + "foobar"
|
// (x + "foo") + "bar" ---> x + "foobar"
|
||||||
|
|
@ -11856,8 +11801,8 @@ Compressor.prototype.compress = function(node) {
|
||||||
right: make_node(AST_String, self.right, {
|
right: make_node(AST_String, self.right, {
|
||||||
value: "" + self.left.right.value + self.right.value,
|
value: "" + self.left.right.value + self.right.value,
|
||||||
start: self.left.right.start,
|
start: self.left.right.start,
|
||||||
end: self.right.end
|
end: self.right.end,
|
||||||
})
|
}),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// a + -b ---> a - b
|
// a + -b ---> a - b
|
||||||
|
|
@ -11867,7 +11812,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
self = make_node(AST_Binary, self, {
|
self = make_node(AST_Binary, self, {
|
||||||
operator: "-",
|
operator: "-",
|
||||||
left: self.left,
|
left: self.left,
|
||||||
right: self.right.expression
|
right: self.right.expression,
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -11879,7 +11824,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
self = make_node(AST_Binary, self, {
|
self = make_node(AST_Binary, self, {
|
||||||
operator: "-",
|
operator: "-",
|
||||||
left: self.right,
|
left: self.right,
|
||||||
right: self.left.expression
|
right: self.left.expression,
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -11897,9 +11842,9 @@ Compressor.prototype.compress = function(node) {
|
||||||
left: make_node(AST_Binary, self, {
|
left: make_node(AST_Binary, self, {
|
||||||
operator: self.operator,
|
operator: self.operator,
|
||||||
left: self.right,
|
left: self.right,
|
||||||
right: self.left.left
|
right: self.left.left,
|
||||||
}),
|
}),
|
||||||
right: self.left.right
|
right: self.left.right,
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -11912,7 +11857,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
self = make_node(AST_Binary, self, {
|
self = make_node(AST_Binary, self, {
|
||||||
operator: "+",
|
operator: "+",
|
||||||
left: self.left,
|
left: self.left,
|
||||||
right: self.right.expression
|
right: self.right.expression,
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -11968,16 +11913,16 @@ Compressor.prototype.compress = function(node) {
|
||||||
left: self.left,
|
left: self.left,
|
||||||
right: self.right.left,
|
right: self.right.left,
|
||||||
start: self.left.start,
|
start: self.left.start,
|
||||||
end: self.right.left.end
|
end: self.right.left.end,
|
||||||
}),
|
}),
|
||||||
right: self.right.right
|
right: self.right.right,
|
||||||
});
|
});
|
||||||
if (self.operator == "+"
|
if (self.operator == "+"
|
||||||
&& !self.right.is_boolean(compressor)
|
&& !self.right.is_boolean(compressor)
|
||||||
&& !self.right.is_number(compressor)) {
|
&& !self.right.is_number(compressor)) {
|
||||||
self.right = make_node(AST_UnaryPrefix, self.right, {
|
self.right = make_node(AST_UnaryPrefix, self.right, {
|
||||||
operator: "+",
|
operator: "+",
|
||||||
expression: self.right
|
expression: self.right,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -12783,10 +12728,10 @@ Compressor.prototype.compress = function(node) {
|
||||||
}
|
}
|
||||||
condition = fuzzy_eval(compressor, condition);
|
condition = fuzzy_eval(compressor, condition);
|
||||||
if (!condition) {
|
if (!condition) {
|
||||||
AST_Node.warn("Condition always false [{file}:{line},{col}]", self.start);
|
AST_Node.warn("Condition always false [{start}]", self);
|
||||||
return make_sequence(self, [ self.condition, self.alternative ]).optimize(compressor);
|
return make_sequence(self, [ self.condition, self.alternative ]).optimize(compressor);
|
||||||
} else if (!(condition instanceof AST_Node)) {
|
} else if (!(condition instanceof AST_Node)) {
|
||||||
AST_Node.warn("Condition always true [{file}:{line},{col}]", self.start);
|
AST_Node.warn("Condition always true [{start}]", self);
|
||||||
return make_sequence(self, [ self.condition, self.consequent ]).optimize(compressor);
|
return make_sequence(self, [ self.condition, self.consequent ]).optimize(compressor);
|
||||||
}
|
}
|
||||||
var first = first_in_statement(compressor);
|
var first = first_in_statement(compressor);
|
||||||
|
|
@ -13158,12 +13103,10 @@ Compressor.prototype.compress = function(node) {
|
||||||
if (compressor.in_boolean_context()) return make_node(AST_Number, self, { value: +self.value });
|
if (compressor.in_boolean_context()) return make_node(AST_Number, self, { value: +self.value });
|
||||||
var p = compressor.parent();
|
var p = compressor.parent();
|
||||||
if (p instanceof AST_Binary && (p.operator == "==" || p.operator == "!=")) {
|
if (p instanceof AST_Binary && (p.operator == "==" || p.operator == "!=")) {
|
||||||
AST_Node.warn("Non-strict equality against boolean: {operator} {value} [{file}:{line},{col}]", {
|
AST_Node.warn("Non-strict equality against boolean: {operator} {value} [{start}]", {
|
||||||
operator : p.operator,
|
operator: p.operator,
|
||||||
value : self.value,
|
value: self.value,
|
||||||
file : p.start.file,
|
start: p.start,
|
||||||
line : p.start.line,
|
|
||||||
col : p.start.col,
|
|
||||||
});
|
});
|
||||||
return make_node(AST_Number, self, { value: +self.value });
|
return make_node(AST_Number, self, { value: +self.value });
|
||||||
}
|
}
|
||||||
|
|
@ -13440,12 +13383,7 @@ Compressor.prototype.compress = function(node) {
|
||||||
|
|
||||||
OPT(AST_Dot, function(self, compressor) {
|
OPT(AST_Dot, function(self, compressor) {
|
||||||
if (self.property == "arguments" || self.property == "caller") {
|
if (self.property == "arguments" || self.property == "caller") {
|
||||||
AST_Node.warn("Function.prototype.{prop} not supported [{file}:{line},{col}]", {
|
AST_Node.warn("Function.prototype.{property} not supported [{start}]", self);
|
||||||
prop: self.property,
|
|
||||||
file: self.start.file,
|
|
||||||
line: self.start.line,
|
|
||||||
col: self.start.col,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
var parent = compressor.parent();
|
var parent = compressor.parent();
|
||||||
if (is_lhs(compressor.self(), parent)) return self;
|
if (is_lhs(compressor.self(), parent)) return self;
|
||||||
|
|
|
||||||
|
|
@ -127,8 +127,10 @@ 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) {
|
||||||
var value = props[p];
|
var value = p == "this" ? props : props[p];
|
||||||
return value instanceof AST_Node ? value.print_to_string() : value;
|
if (value instanceof AST_Node) return value.print_to_string();
|
||||||
|
if (value instanceof AST_Token) return value.file + ":" + value.line + "," + value.col;
|
||||||
|
return value;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2086,7 +2086,7 @@ issue_5246_1: {
|
||||||
}
|
}
|
||||||
expect_stdout: "PASS"
|
expect_stdout: "PASS"
|
||||||
expect_warnings: [
|
expect_warnings: [
|
||||||
"INFO: Dropping unused default argument {}=42 [test/compress/default-values.js:1,29]",
|
"INFO: Dropping unused default argument {} [test/compress/default-values.js:1,29]",
|
||||||
]
|
]
|
||||||
node_version: ">=6"
|
node_version: ">=6"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user