// Constant single use vars can be replaced in any scope.
if (var_decl.value.is_constant()) {
var ctt = new TreeTransformer(function(node) {
- if (node === ref
- && !ctt.find_parent(AST_ForIn)) {
- return replace_var(node, ctt.parent(), true);
+ var parent = ctt.parent();
+ if (parent instanceof AST_IterationStatement
+ && (parent.condition === node || parent.init === node)) {
+ return node;
}
+ if (node === ref)
+ return replace_var(node, parent, true);
});
stat.transform(ctt);
continue;
}
input: {
function f1(y) {
- // The constant do-while condition `c` will be replaced.
+ // The constant do-while condition `c` will not be replaced.
var c = 9;
- do { } while (c === 77);
+ do {} while (c === 77);
}
function f2(y) {
// The non-constant do-while condition `c` will not be replaced.
}
expect: {
function f1(y) {
- do ; while (false);
+ var c = 9;
+ do ; while (77 === c);
}
function f2(y) {
var c = 5 - y;
}
input: {
function f1(y) {
- // The constant do-while condition `c` will be replaced.
+ // The constant do-while condition `c` will be not replaced.
var c = 9;
- do { } while (c === 77);
+ do {} while (c === 77);
}
function f2(y) {
// The non-constant do-while condition `c` will not be replaced.
}
expect: {
function f1(y) {
- do ; while (false);
+ var c = 9;
+ do ; while (77 === c);
}
function f2(y) {
var c = 5 - y;
};
}
(function(){
- var result, rx = /ab*/g;
- while (result = rx.exec('acdabcdeabbb'))
+ var result, s = "acdabcdeabbb", rx = /ab*/g;
+ while (result = rx.exec(s))
console.log(result[0]);
})();
}
for (k in {prop: 'val'});
}
}
+
+issue_1562: {
+ options = {
+ collapse_vars: true,
+ }
+ input: {
+ var v = 1, B = 2;
+ for (v in objs) f(B);
+
+ var x = 3, C = 10;
+ while(x + 2) bar(C);
+
+ var y = 4, D = 20;
+ do bar(D); while(y + 2);
+
+ var z = 5, E = 30;
+ for (; f(z + 2) ;) bar(E);
+ }
+ expect: {
+ var v = 1;
+ for (v in objs) f(2);
+
+ var x = 3;
+ while(x + 2) bar(10);
+
+ var y = 4;
+ do bar(20); while(y + 2);
+
+ var z = 5;
+ for (; f(z + 2) ;) bar(30);
+ }
+}