diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compress.js | 40 | ||||
-rw-r--r-- | lib/output.js | 19 |
2 files changed, 47 insertions, 12 deletions
diff --git a/lib/compress.js b/lib/compress.js index 8dfbc72b..8d936bac 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -66,6 +66,7 @@ function Compressor(options, false_by_default) { join_vars : !false_by_default, cascade : !false_by_default, side_effects : !false_by_default, + negate_iife : !false_by_default, screw_ie8 : false, warnings : true, @@ -214,6 +215,11 @@ merge(Compressor.prototype, { statements = join_consecutive_vars(statements, compressor); } } while (CHANGED); + + if (compressor.option("negate_iife")) { + negate_iifes(statements, compressor); + } + return statements; function eliminate_spurious_blocks(statements) { @@ -497,6 +503,40 @@ merge(Compressor.prototype, { }, []); }; + function negate_iifes(statements, compressor) { + statements.forEach(function(stat){ + if (stat instanceof AST_SimpleStatement) { + stat.body = (function transform(thing) { + return thing.transform(new TreeTransformer(function(node){ + if (node instanceof AST_Call && node.expression instanceof AST_Function) { + return make_node(AST_UnaryPrefix, node, { + operator: "!", + expression: node + }); + } + else if (node instanceof AST_Call) { + node.expression = transform(node.expression); + } + else if (node instanceof AST_Seq) { + node.car = transform(node.car); + } + else if (node instanceof AST_Conditional) { + var expr = transform(node.condition); + if (expr !== node.condition) { + // it has been negated, reverse + node.condition = expr; + var tmp = node.consequent; + node.consequent = node.alternative; + node.alternative = tmp; + } + } + return node; + })); + })(stat.body); + } + }); + }; + }; function extract_declarations_from_unreachable_code(compressor, stat, target) { diff --git a/lib/output.js b/lib/output.js index 6d0dac5a..b7bcd1e3 100644 --- a/lib/output.js +++ b/lib/output.js @@ -61,7 +61,6 @@ function OutputStream(options) { comments : false, preserve_line : false, screw_ie8 : false, - negate_iife : !(options && options.beautify), }, true); var indentation = 0; @@ -351,21 +350,17 @@ function OutputStream(options) { AST_Node.DEFMETHOD("print", function(stream, force_parens){ var self = this, generator = self._codegen; - stream.push_node(self); - var needs_parens = self.needs_parens(stream); - var fc = self instanceof AST_Function && stream.option("negate_iife"); - if (force_parens || (needs_parens && !fc)) { - stream.with_parens(function(){ - self.add_comments(stream); - self.add_source_map(stream); - generator(self, stream); - }); - } else { + function doit() { self.add_comments(stream); - if (needs_parens && fc) stream.print("!"); self.add_source_map(stream); generator(self, stream); } + stream.push_node(self); + if (force_parens || self.needs_parens(stream)) { + stream.with_parens(doit); + } else { + doit(); + } stream.pop_node(); }); |