fix invalid transform on const (#1919)

- preserve (re)assignment to `const` for runtime error
- suppress `cascade` on `const`, as runtime behaviour is ill-defined
This commit is contained in:
Alex Lam S.L
2017-05-12 04:51:44 +08:00
committed by alexlamsl
parent 13e5e33448
commit aa7e8783f8
4 changed files with 106 additions and 1 deletions

View File

@@ -440,6 +440,14 @@ merge(Compressor.prototype, {
return fixed();
});
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++)) {
@@ -1909,6 +1917,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;
@@ -3066,7 +3075,8 @@ merge(Compressor.prototype, {
}
if (left
&& !(left instanceof AST_SymbolRef
&& left.definition().orig[0] instanceof AST_SymbolLambda)) {
&& (left.definition().orig[0] instanceof AST_SymbolLambda
|| is_reference_const(left)))) {
var parent, field;
var cdr = self.cdr;
while (true) {