};
} else if (Array.isArray(pure_funcs)) {
this.pure_funcs = function(node) {
- return pure_funcs.indexOf(node.expression.print_to_string()) < 0;
+ return !member(node.expression.print_to_string(), pure_funcs);
};
} else {
this.pure_funcs = return_true;
top_retain = top_retain.split(/,/);
}
this.top_retain = function(def) {
- return top_retain.indexOf(def.name) >= 0;
+ return member(def.name, top_retain);
};
}
var toplevel = this.options["toplevel"];
CHANGED = true;
statements.splice(i, 1);
} else if (stat instanceof AST_Directive) {
- if (seen_dirs.indexOf(stat.value) < 0) {
+ if (!member(stat.value, seen_dirs)) {
i++;
seen_dirs.push(stat.value);
} else {
var fixed = this.fixed_value();
if (!fixed) return this;
var value;
- if (cached.indexOf(fixed) >= 0) {
+ if (member(fixed, cached)) {
value = fixed._eval();
} else {
this._eval = return_this;
var defun = make_node(AST_Defun, def, def.value);
defun.name = make_node(AST_SymbolDefun, def.name, def.name);
var name_def = def.name.scope.resolve().def_function(defun.name);
- if (def.value.name) def.value.name.definition().references.forEach(function(ref) {
- ref.name = name_def.name;
- ref.thedef = name_def;
- ref.reference({});
- });
+ if (def.value.name) {
+ var old_def = def.value.name.definition();
+ def.value.walk(new TreeWalker(function(node) {
+ if (node instanceof AST_SymbolRef && node.definition() === old_def) {
+ node.name = name_def.name;
+ node.thedef = name_def;
+ node.reference({});
+ }
+ }));
+ }
body.push(defun);
} else {
if (side_effects.length > 0) {
// https://github.com/mishoo/UglifyJS2/issues/44
// https://github.com/mishoo/UglifyJS2/issues/1830
// https://github.com/mishoo/UglifyJS2/issues/1838
+ // https://github.com/mishoo/UglifyJS2/issues/3371
// that's an invalid AST.
// We fix it at this stage by moving the `var` outside the `for`.
if (node instanceof AST_For) {
node.init = block.body.pop();
block.body.push(node);
}
- if (node.init instanceof AST_SimpleStatement) {
+ if (node.init instanceof AST_Defun) {
+ if (!block) {
+ block = make_node(AST_BlockStatement, node, {
+ body: [ node ]
+ });
+ }
+ block.body.splice(-1, 0, node.init);
+ node.init = null;
+ } else if (node.init instanceof AST_SimpleStatement) {
node.init = node.init.body;
} else if (is_empty(node.init)) {
node.init = null;