if (node instanceof AST_SymbolRef) {
var d = node.definition();
d.references.push(node);
- if (d.fixed === undefined || !is_safe(d)
+ if (d.fixed === undefined || !safe_to_read(d)
|| is_modified(node, 0, is_immutable(node.fixed_value()))) {
d.fixed = false;
}
}
if (node instanceof AST_VarDef) {
var d = node.name.definition();
- if (d.fixed == null) {
+ if (d.fixed === undefined || safe_to_assign(d, node.value)) {
if (node.value) {
d.fixed = function() {
return node.value;
&& node.operator == "="
&& node.left instanceof AST_SymbolRef) {
var d = node.left.definition();
- if (HOP(safe_ids, d.id) && d.fixed == null) {
+ if (safe_to_assign(d, node.right)) {
+ d.references.push(node.left);
d.fixed = function() {
return node.right;
};
}
if (node instanceof AST_Defun) {
var d = node.name.definition();
- if (!toplevel && d.global || is_safe(d)) {
+ if (!toplevel && d.global || safe_to_read(d)) {
d.fixed = false;
} else {
d.fixed = node;
safe_ids = save_ids;
return true;
}
- var iife;
- if (node instanceof AST_Function
- && (iife = tw.parent()) instanceof AST_Call
- && iife.expression === node) {
- if (node.name) {
- node.name.definition().fixed = node;
- } else {
+ if (node instanceof AST_Function) {
+ push();
+ var iife;
+ if (!node.name
+ && (iife = tw.parent()) instanceof AST_Call
+ && iife.expression === node) {
// Virtually turn IIFE parameters into variable definitions:
// (function(a,b) {...})(c,d) => (function() {var a=c,b=d; ...})()
// So existing transformation rules can work on them.
mark(d, true);
});
}
+ descend();
+ pop();
+ return true;
}
if (node instanceof AST_Binary
&& (node.operator == "&&" || node.operator == "||")) {
}
if (node instanceof AST_For) {
if (node.init) node.init.walk(tw);
+ if (node.condition) {
+ push();
+ node.condition.walk(tw);
+ pop();
+ }
push();
- if (node.condition) node.condition.walk(tw);
node.body.walk(tw);
- if (node.step) node.step.walk(tw);
pop();
+ if (node.step) {
+ push();
+ node.step.walk(tw);
+ pop();
+ }
return true;
}
if (node instanceof AST_ForIn) {
safe_ids[def.id] = safe;
}
- function is_safe(def) {
+ function safe_to_read(def) {
if (safe_ids[def.id]) {
if (def.fixed == null) {
var orig = def.orig[0];
}
}
+ function safe_to_assign(def, value) {
+ if (!HOP(safe_ids, def.id)) return false;
+ if (!safe_to_read(def)) return false;
+ if (def.fixed === false) return false;
+ if (def.fixed != null && (!value || def.references.length > 0)) return false;
+ return !def.orig.some(function(sym) {
+ return sym instanceof AST_SymbolConst
+ || sym instanceof AST_SymbolDefun
+ || sym instanceof AST_SymbolLambda;
+ });
+ }
+
function push() {
safe_ids = Object.create(safe_ids);
}
conditionals : true,
evaluate : true,
reduce_vars : true,
- unused : true
+ unused : true,
}
input: {
function f0() {
}
function f2() {
- var b = 2;
- b = 3;
- console.log(1 + b);
- console.log(b + 3);
+ 3;
console.log(4);
- console.log(1 + b + 3);
+ console.log(6);
+ console.log(4);
+ console.log(7);
}
function f3() {
}
expect: {
function f() {
- var b = 2;
- b = 3;
- console.log(1 + b);
- console.log(b + 3);
+ 3;
console.log(4);
- console.log(1 + b + 3);
+ console.log(6);
+ console.log(4);
+ console.log(7);
}
}
}
}
}
-inner_var_for: {
+inner_var_for_1: {
options = {
evaluate: true,
reduce_vars: true,
}
}
+inner_var_for_2: {
+ options = {
+ evaluate: true,
+ reduce_vars: true,
+ unused: true,
+ }
+ input: {
+ !function() {
+ var a = 1;
+ for (var b = 1; --b;) var a = 2;
+ console.log(a);
+ }();
+ }
+ expect: {
+ !function() {
+ a = 1;
+ for (var b = 1; --b;) var a = 2;
+ console.log(a);
+ }();
+ }
+ expect_stdout: "1"
+}
+
inner_var_for_in_1: {
options = {
evaluate: true,
console.log(function(a) {
var a;
return typeof a;
- }([]), "number", function(a) {
- var a = void 0;
- return typeof a;
- }([]));
+ }([]), "number", "undefined");
}
expect_stdout: "object number undefined"
}
expect_stdout: "2 undefined"
}
+var_assign_6: {
+ options = {
+ evaluate: true,
+ reduce_vars: true,
+ unused: true,
+ }
+ input: {
+ !function() {
+ var a = function(){}(a = 1);
+ console.log(a);
+ }();
+ }
+ expect: {
+ !function() {
+ var a = function(){}(a = 1);
+ console.log(a);
+ }();
+ }
+ expect_stdout: "undefined"
+}
+
immutable: {
options = {
evaluate: true,
}
expect_stdout: "undefined"
}
+
+iife_assign: {
+ options = {
+ evaluate: true,
+ reduce_vars: true,
+ unused: true,
+ }
+ input: {
+ !function() {
+ var a = 1, b = 0;
+ !function() {
+ b++;
+ return;
+ a = 2;
+ }();
+ console.log(a);
+ }();
+ }
+ expect: {
+ !function() {
+ var a = 1, b = 0;
+ !function() {
+ b++;
+ return;
+ a = 2;
+ }();
+ console.log(a);
+ }();
+ }
+ expect_stdout: "1"
+}