diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2017-03-25 23:03:26 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-25 23:03:26 +0800 |
commit | b19aa58cff73ea59438346db094de4a54463d669 (patch) | |
tree | ebdeceeb65f8cc761e9f6f323713108ca691bbc9 /lib/compress.js | |
parent | 0a65de89b97731cd83a077303dd0511ba7ff7151 (diff) | |
download | tracifyjs-b19aa58cff73ea59438346db094de4a54463d669.tar.gz tracifyjs-b19aa58cff73ea59438346db094de4a54463d669.zip |
fix `has_side_effects()` (#1675)
`AST_Try` is an `AST_Block`, so besides try block we also need to inspect catch and finally blocks for possible side effects.
Also extend this functionality to handle `AST_If` and `AST_LabeledStatement` while we are at it.
fixes #1673
Diffstat (limited to 'lib/compress.js')
-rw-r--r-- | lib/compress.js | 35 |
1 files changed, 22 insertions, 13 deletions
diff --git a/lib/compress.js b/lib/compress.js index bea9c9a4..805a84a7 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1600,14 +1600,29 @@ merge(Compressor.prototype, { return false; }); - def(AST_Block, function(compressor){ - for (var i = this.body.length; --i >= 0;) { - if (this.body[i].has_side_effects(compressor)) + function any(list, compressor) { + for (var i = list.length; --i >= 0;) + if (list[i].has_side_effects(compressor)) return true; - } return false; - }); + } + def(AST_Block, function(compressor){ + return any(this.body, compressor); + }); + def(AST_Try, function(compressor){ + return any(this.body, compressor) + || this.bcatch && this.bcatch.has_side_effects(compressor) + || this.bfinally && this.bfinally.has_side_effects(compressor); + }); + def(AST_If, function(compressor){ + return this.condition.has_side_effects(compressor) + || this.body && this.body.has_side_effects(compressor) + || this.alternative && this.alternative.has_side_effects(compressor); + }); + def(AST_LabeledStatement, function(compressor){ + return this.body.has_side_effects(compressor); + }); def(AST_SimpleStatement, function(compressor){ return this.body.has_side_effects(compressor); }); @@ -1633,19 +1648,13 @@ merge(Compressor.prototype, { return this.global() && this.undeclared(); }); def(AST_Object, function(compressor){ - for (var i = this.properties.length; --i >= 0;) - if (this.properties[i].has_side_effects(compressor)) - return true; - return false; + return any(this.properties, compressor); }); def(AST_ObjectProperty, function(compressor){ return this.value.has_side_effects(compressor); }); def(AST_Array, function(compressor){ - for (var i = this.elements.length; --i >= 0;) - if (this.elements[i].has_side_effects(compressor)) - return true; - return false; + return any(this.elements, compressor); }); def(AST_Dot, function(compressor){ if (!compressor.option("pure_getters")) return true; |