diff options
author | Mihai Bazon <mihai.bazon@gmail.com> | 2013-08-20 17:45:52 +0300 |
---|---|---|
committer | Mihai Bazon <mihai.bazon@gmail.com> | 2013-08-20 17:45:52 +0300 |
commit | ed80b4a534083510082419b305e0b60b395b10c6 (patch) | |
tree | 9a264d89beb4304dfcbdae48572cf4d7e142154e /lib/compress.js | |
parent | 4f09df238e15bfb9d03d28ba718c402bad0b1078 (diff) | |
download | tracifyjs-ed80b4a534083510082419b305e0b60b395b10c6.tar.gz tracifyjs-ed80b4a534083510082419b305e0b60b395b10c6.zip |
Move support for `negate_iife` in the compressor, rather than code generator
(the code generator doesn't maintain enough context to know whether
the return value is important or discarded)
Fixes #272
Diffstat (limited to 'lib/compress.js')
-rw-r--r-- | lib/compress.js | 40 |
1 files changed, 40 insertions, 0 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) { |