diff options
author | alexlamsl <alexlamsl@gmail.com> | 2017-02-24 01:46:57 +0800 |
---|---|---|
committer | alexlamsl <alexlamsl@gmail.com> | 2017-02-24 01:46:57 +0800 |
commit | 4e49302916fe395f5c63992aa28c33392208fb27 (patch) | |
tree | 97a15589a105160f71214d548db5fee8202cbb14 /lib | |
parent | 1e51586996ae4fdac68a8ea597c20ab170809c43 (diff) | |
download | tracifyjs-4e49302916fe395f5c63992aa28c33392208fb27.tar.gz tracifyjs-4e49302916fe395f5c63992aa28c33392208fb27.zip |
enable `collapse_vars` & `reduce_vars` by default
- fix corner cases in `const` optimisation
- deprecate `/*@const*/`
fixes #1497
closes #1498
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compress.js | 12 | ||||
-rw-r--r-- | lib/scope.js | 12 |
2 files changed, 8 insertions, 16 deletions
diff --git a/lib/compress.js b/lib/compress.js index 95e9c1b3..2bc1c5a5 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -69,8 +69,8 @@ function Compressor(options, false_by_default) { hoist_vars : false, if_return : !false_by_default, join_vars : !false_by_default, - collapse_vars : false, - reduce_vars : false, + collapse_vars : !false_by_default, + reduce_vars : !false_by_default, cascade : !false_by_default, side_effects : !false_by_default, pure_getters : false, @@ -1252,7 +1252,7 @@ merge(Compressor.prototype, { this._evaluating = true; try { var d = this.definition(); - if ((d.constant || compressor.option("reduce_vars") && !d.modified) && d.init) { + if (compressor.option("reduce_vars") && !d.modified && d.init) { if (compressor.option("unsafe")) { if (!HOP(d.init, '_evaluated')) { d.init._evaluated = ev(d.init, compressor); @@ -3025,9 +3025,11 @@ merge(Compressor.prototype, { return make_node(AST_Infinity, self).transform(compressor); } } - if (compressor.option("evaluate") && !isLHS(self, compressor.parent())) { + if (compressor.option("evaluate") + && compressor.option("reduce_vars") + && !isLHS(self, compressor.parent())) { var d = self.definition(); - if (d && d.constant && d.init && d.init.is_constant(compressor)) { + if (d.constant && !d.modified && d.init && d.init.is_constant(compressor)) { var original_as_string = self.print_to_string(); var const_node = make_node_from_constant(compressor, d.init.constant_value(compressor), self); var const_node_as_string = const_node.print_to_string(); diff --git a/lib/scope.js b/lib/scope.js index b0d92d7d..29e4103a 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -97,7 +97,6 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){ var scope = self.parent_scope = null; var labels = new Dictionary(); var defun = null; - var last_var_had_const_pragma = false; var tw = new TreeWalker(function(node, descend){ if (options.screw_ie8 && node instanceof AST_Catch) { var save_scope = scope; @@ -154,13 +153,10 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){ // later. (node.scope = defun.parent_scope).def_function(node); } - else if (node instanceof AST_Var) { - last_var_had_const_pragma = node.has_const_pragma(); - } else if (node instanceof AST_SymbolVar || node instanceof AST_SymbolConst) { var def = defun.def_variable(node); - def.constant = node instanceof AST_SymbolConst || last_var_had_const_pragma; + def.constant = node instanceof AST_SymbolConst; def.init = tw.parent().value; } else if (node instanceof AST_SymbolCatch) { @@ -369,12 +365,6 @@ AST_Symbol.DEFMETHOD("global", function(){ return this.definition().global; }); -AST_Var.DEFMETHOD("has_const_pragma", function() { - var comments_before = this.start && this.start.comments_before; - var lastComment = comments_before && comments_before[comments_before.length - 1]; - return lastComment && /@const\b/.test(lastComment.value); -}); - AST_Toplevel.DEFMETHOD("_default_mangler_options", function(options){ return defaults(options, { except : [], |