@@ -905,10 +905,18 @@ merge(Compressor.prototype, {
|
|||||||
if (abort) return node;
|
if (abort) return node;
|
||||||
// Scan case expressions first in a switch statement
|
// Scan case expressions first in a switch statement
|
||||||
if (node instanceof AST_Switch) {
|
if (node instanceof AST_Switch) {
|
||||||
|
if (!hit) {
|
||||||
|
if (node !== hit_stack[hit_index]) return node;
|
||||||
|
hit_index++;
|
||||||
|
}
|
||||||
node.expression = node.expression.transform(scanner);
|
node.expression = node.expression.transform(scanner);
|
||||||
for (var i = 0, len = node.body.length; !abort && i < len; i++) {
|
for (var i = 0, len = node.body.length; !abort && i < len; i++) {
|
||||||
var branch = node.body[i];
|
var branch = node.body[i];
|
||||||
if (branch instanceof AST_Case) {
|
if (branch instanceof AST_Case) {
|
||||||
|
if (!hit) {
|
||||||
|
if (branch !== hit_stack[hit_index]) continue;
|
||||||
|
hit_index++;
|
||||||
|
}
|
||||||
branch.expression = branch.expression.transform(scanner);
|
branch.expression = branch.expression.transform(scanner);
|
||||||
if (side_effects || !replace_all) break;
|
if (side_effects || !replace_all) break;
|
||||||
}
|
}
|
||||||
@@ -918,13 +926,13 @@ merge(Compressor.prototype, {
|
|||||||
}
|
}
|
||||||
// Skip nodes before `candidate` as quickly as possible
|
// Skip nodes before `candidate` as quickly as possible
|
||||||
if (!hit) {
|
if (!hit) {
|
||||||
if (node === candidate) {
|
if (node !== hit_stack[hit_index]) return node;
|
||||||
hit = true;
|
hit_index++;
|
||||||
stop_after = find_stop(node, 0);
|
if (hit_index < hit_stack.length) return;
|
||||||
if (stop_after === node) abort = true;
|
hit = true;
|
||||||
return node;
|
stop_after = find_stop(node, 0);
|
||||||
}
|
if (stop_after === node) abort = true;
|
||||||
return;
|
return node;
|
||||||
}
|
}
|
||||||
// Stop immediately if these node types are encountered
|
// Stop immediately if these node types are encountered
|
||||||
var parent = scanner.parent();
|
var parent = scanner.parent();
|
||||||
@@ -1013,11 +1021,11 @@ merge(Compressor.prototype, {
|
|||||||
if (abort) return node;
|
if (abort) return node;
|
||||||
// Skip nodes before `candidate` as quickly as possible
|
// Skip nodes before `candidate` as quickly as possible
|
||||||
if (!hit) {
|
if (!hit) {
|
||||||
if (node === candidate) {
|
if (node !== hit_stack[hit_index]) return node;
|
||||||
hit = true;
|
hit_index++;
|
||||||
return node;
|
if (hit_index < hit_stack.length) return;
|
||||||
}
|
hit = true;
|
||||||
return;
|
return node;
|
||||||
}
|
}
|
||||||
// Replace variable when found
|
// Replace variable when found
|
||||||
if (node instanceof AST_SymbolRef
|
if (node instanceof AST_SymbolRef
|
||||||
@@ -1038,9 +1046,12 @@ merge(Compressor.prototype, {
|
|||||||
// var a = x(), b = undefined;
|
// var a = x(), b = undefined;
|
||||||
if (stat_index == 0 && compressor.option("unused")) extract_args();
|
if (stat_index == 0 && compressor.option("unused")) extract_args();
|
||||||
// Find collapsible assignments
|
// Find collapsible assignments
|
||||||
|
var hit_stack = [];
|
||||||
extract_candidates(statements[stat_index]);
|
extract_candidates(statements[stat_index]);
|
||||||
while (candidates.length > 0) {
|
while (candidates.length > 0) {
|
||||||
var candidate = candidates.pop();
|
hit_stack = candidates.pop();
|
||||||
|
var hit_index = 0;
|
||||||
|
var candidate = hit_stack[hit_stack.length - 1];
|
||||||
var value_def = null;
|
var value_def = null;
|
||||||
var stop_after = null;
|
var stop_after = null;
|
||||||
var lhs = get_lhs(candidate);
|
var lhs = get_lhs(candidate);
|
||||||
@@ -1074,6 +1085,7 @@ merge(Compressor.prototype, {
|
|||||||
if (abort && def.references.length - def.replaced > replaced) replaced = false;
|
if (abort && def.references.length - def.replaced > replaced) replaced = false;
|
||||||
else {
|
else {
|
||||||
abort = false;
|
abort = false;
|
||||||
|
hit_index = 0;
|
||||||
hit = funarg;
|
hit = funarg;
|
||||||
for (var i = stat_index; !abort && i < statements.length; i++) {
|
for (var i = stat_index; !abort && i < statements.length; i++) {
|
||||||
statements[i].transform(multi_replacer);
|
statements[i].transform(multi_replacer);
|
||||||
@@ -1124,18 +1136,24 @@ merge(Compressor.prototype, {
|
|||||||
});
|
});
|
||||||
arg.walk(tw);
|
arg.walk(tw);
|
||||||
}
|
}
|
||||||
if (arg) candidates.unshift(make_node(AST_VarDef, sym, {
|
if (arg) candidates.unshift([ make_node(AST_VarDef, sym, {
|
||||||
name: sym,
|
name: sym,
|
||||||
value: arg
|
value: arg
|
||||||
}));
|
}) ]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function extract_candidates(expr) {
|
function extract_candidates(expr) {
|
||||||
if (expr instanceof AST_Assign && !expr.left.has_side_effects(compressor)
|
hit_stack.push(expr);
|
||||||
|| expr instanceof AST_Unary && (expr.operator == "++" || expr.operator == "--")) {
|
if (expr instanceof AST_Assign) {
|
||||||
candidates.push(expr);
|
if (!expr.left.has_side_effects(compressor)) {
|
||||||
|
candidates.push(hit_stack.slice());
|
||||||
|
}
|
||||||
|
} else if (expr instanceof AST_Unary) {
|
||||||
|
if (expr.operator == "++" || expr.operator == "--") {
|
||||||
|
candidates.push(hit_stack.slice());
|
||||||
|
}
|
||||||
} else if (expr instanceof AST_Call) {
|
} else if (expr instanceof AST_Call) {
|
||||||
extract_candidates(expr.expression);
|
extract_candidates(expr.expression);
|
||||||
expr.args.forEach(extract_candidates);
|
expr.args.forEach(extract_candidates);
|
||||||
@@ -1146,9 +1164,7 @@ merge(Compressor.prototype, {
|
|||||||
extract_candidates(expr.consequent);
|
extract_candidates(expr.consequent);
|
||||||
extract_candidates(expr.alternative);
|
extract_candidates(expr.alternative);
|
||||||
} else if (expr instanceof AST_Definitions) {
|
} else if (expr instanceof AST_Definitions) {
|
||||||
expr.definitions.forEach(function(var_def) {
|
expr.definitions.forEach(extract_candidates);
|
||||||
if (var_def.value) candidates.push(var_def);
|
|
||||||
});
|
|
||||||
} else if (expr instanceof AST_Exit) {
|
} else if (expr instanceof AST_Exit) {
|
||||||
if (expr.value) extract_candidates(expr.value);
|
if (expr.value) extract_candidates(expr.value);
|
||||||
} else if (expr instanceof AST_For) {
|
} else if (expr instanceof AST_For) {
|
||||||
@@ -1162,7 +1178,10 @@ merge(Compressor.prototype, {
|
|||||||
} else if (expr instanceof AST_Switch) {
|
} else if (expr instanceof AST_Switch) {
|
||||||
extract_candidates(expr.expression);
|
extract_candidates(expr.expression);
|
||||||
expr.body.forEach(extract_candidates);
|
expr.body.forEach(extract_candidates);
|
||||||
|
} else if (expr instanceof AST_VarDef) {
|
||||||
|
if (expr.value) candidates.push(hit_stack.slice());
|
||||||
}
|
}
|
||||||
|
hit_stack.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
function find_stop(node, level) {
|
function find_stop(node, level) {
|
||||||
|
|||||||
@@ -3990,3 +3990,25 @@ cascade_call: {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
replace_all_var: {
|
||||||
|
options = {
|
||||||
|
collapse_vars: true,
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
var a = "PASS";
|
||||||
|
(function() {
|
||||||
|
var b = b || c && c[a = "FAIL"], c = a;
|
||||||
|
})();
|
||||||
|
console.log(a);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
var a = "PASS";
|
||||||
|
(function() {
|
||||||
|
var b = b || c && c[a = "FAIL"], c = a;
|
||||||
|
})();
|
||||||
|
console.log(a);
|
||||||
|
}
|
||||||
|
expect_stdout: "PASS"
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user