diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2017-05-12 04:51:44 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-12 04:51:44 +0800 |
commit | 1d407e761e72601fa607f884e0f53ced8ae4b94d (patch) | |
tree | c0d858a4d0d90c610337fdf37f0ba9e3e6a91dea /lib | |
parent | 2b44f4ae30921e3d1eea2846250f26b0942490df (diff) | |
download | tracifyjs-1d407e761e72601fa607f884e0f53ced8ae4b94d.tar.gz tracifyjs-1d407e761e72601fa607f884e0f53ced8ae4b94d.zip |
fix invalid transform on `const` (#1919)
- preserve (re)assignment to `const` for runtime error
- suppress `cascade` on `const`, as runtime behaviour is ill-defined
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compress.js | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/lib/compress.js b/lib/compress.js index 919ee20f..aff5c643 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -525,6 +525,14 @@ merge(Compressor.prototype, { return lhs instanceof AST_SymbolRef && lhs.definition().orig[0] instanceof AST_SymbolLambda; } + function is_reference_const(ref) { + if (!(ref instanceof AST_SymbolRef)) return false; + var orig = ref.definition().orig; + for (var i = orig.length; --i >= 0;) { + if (orig[i] instanceof AST_SymbolConst) return true; + } + } + function find_variable(compressor, name) { var scope, i = 0; while (scope = compressor.parent(i++)) { @@ -790,7 +798,8 @@ merge(Compressor.prototype, { return make_node(AST_SymbolRef, expr.name, expr.name); } } else { - return expr[expr instanceof AST_Assign ? "left" : "expression"]; + var lhs = expr[expr instanceof AST_Assign ? "left" : "expression"]; + return !is_reference_const(lhs) && lhs; } } @@ -1973,6 +1982,7 @@ merge(Compressor.prototype, { && node instanceof AST_Assign && node.operator == "=" && node.left instanceof AST_SymbolRef + && !is_reference_const(node.left) && scope === self) { node.right.walk(tw); return true; @@ -3178,7 +3188,7 @@ merge(Compressor.prototype, { && (left.operator == "++" || left.operator == "--")) { left = left.expression; } else left = null; - if (!left || is_lhs_read_only(left)) { + if (!left || is_lhs_read_only(left) || is_reference_const(left)) { expressions[++i] = cdr; continue; } |