Skip to content

Commit

Permalink
use walk() some more
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiosantoscode committed Feb 27, 2020
1 parent 9519884 commit 6ba84bc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
28 changes: 14 additions & 14 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ import {
AST_With,
AST_Yield,
TreeWalker,
walk,
walk_abort
} from "./ast.js";
import {
get_full_char_code,
Expand Down Expand Up @@ -994,15 +996,12 @@ function OutputStream(options) {
// parens around it too, otherwise the call will be
// interpreted as passing the arguments to the upper New
// expression.
var parens = false;
this.walk(new TreeWalker(function(node) {
if (parens || node instanceof AST_Scope) return true;
return walk(this, node => {
if (node instanceof AST_Scope) return true;
if (node instanceof AST_Call) {
parens = true;
return true;
return walk_abort; // makes walk() return true.
}
}));
return parens;
});
}
});

Expand Down Expand Up @@ -1678,13 +1677,14 @@ function OutputStream(options) {
var parens = false;
// need to take some precautions here:
// https://github.com/mishoo/UglifyJS2/issues/60
if (noin) node.walk(new TreeWalker(function(node) {
if (parens || node instanceof AST_Scope) return true;
if (node instanceof AST_Binary && node.operator == "in") {
parens = true;
return true;
}
}));
if (noin) {
parens = walk(node, node => {
if (node instanceof AST_Scope) return true;
if (node instanceof AST_Binary && node.operator == "in") {
return walk_abort; // makes walk() return true
}
});
}
node.print(output, parens);
}

Expand Down
22 changes: 11 additions & 11 deletions lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ import {
AST_VarDef,
AST_With,
TreeWalker,
walk
} from "./ast.js";
import {
RESERVED_WORDS,
Expand Down Expand Up @@ -200,13 +201,12 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
});

// pass 1: setup scope chaining and handle definitions
var self = this;
var scope = self.parent_scope = null;
var scope = this.parent_scope = null;
var labels = new Map();
var defun = null;
var in_destructuring = null;
var for_scopes = [];
var tw = new TreeWalker(function(node, descend) {
var tw = new TreeWalker((node, descend) => {
if (node.is_block_scope()) {
const save_scope = scope;
node.block_scope = scope = new AST_Scope(node);
Expand Down Expand Up @@ -357,7 +357,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
);
}
});
self.walk(tw);
this.walk(tw);

function mark_export(def, level) {
if (in_destructuring) {
Expand All @@ -376,8 +376,8 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
}

// pass 2: find back references and eval
self.globals = new Map();
var tw = new TreeWalker(function(node) {
this.globals = new Map();
var tw = new TreeWalker(node => {
if (node instanceof AST_LoopControl && node.label) {
node.label.thedef.references.push(node);
return true;
Expand All @@ -392,7 +392,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
var sym;
if (tw.parent() instanceof AST_NameMapping && tw.parent(1).module_name
|| !(sym = node.scope.find_variable(name))) {
sym = self.def_global(node);
sym = this.def_global(node);
if (node instanceof AST_SymbolExport) sym.export = MASK_EXPORT_DONT_MANGLE;
} else if (sym.scope instanceof AST_Lambda && name == "arguments") {
sym.scope.uses_arguments = true;
Expand All @@ -416,16 +416,16 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
}
}
});
self.walk(tw);
this.walk(tw);

// pass 3: work around IE8 and Safari catch scope bugs
if (options.ie8 || options.safari10) {
self.walk(new TreeWalker(function(node) {
walk(this, node => {
if (node instanceof AST_SymbolCatch) {
var name = node.name;
var refs = node.thedef.references;
var scope = node.scope.get_defun_scope();
var def = scope.find_variable(name) || self.globals.get(name) || scope.def_variable(node);
var def = scope.find_variable(name) || this.globals.get(name) || scope.def_variable(node);
refs.forEach(function(ref) {
ref.thedef = def;
ref.reference(options);
Expand All @@ -434,7 +434,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
node.reference(options);
return true;
}
}));
});
}

// pass 4: add symbol definitions to loop scopes
Expand Down

0 comments on commit 6ba84bc

Please sign in to comment.