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:
@@ -440,6 +440,14 @@ merge(Compressor.prototype, {
|
|||||||
return fixed();
|
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) {
|
function find_variable(compressor, name) {
|
||||||
var scope, i = 0;
|
var scope, i = 0;
|
||||||
while (scope = compressor.parent(i++)) {
|
while (scope = compressor.parent(i++)) {
|
||||||
@@ -1909,6 +1917,7 @@ merge(Compressor.prototype, {
|
|||||||
&& node instanceof AST_Assign
|
&& node instanceof AST_Assign
|
||||||
&& node.operator == "="
|
&& node.operator == "="
|
||||||
&& node.left instanceof AST_SymbolRef
|
&& node.left instanceof AST_SymbolRef
|
||||||
|
&& !is_reference_const(node.left)
|
||||||
&& scope === self) {
|
&& scope === self) {
|
||||||
node.right.walk(tw);
|
node.right.walk(tw);
|
||||||
return true;
|
return true;
|
||||||
@@ -3066,7 +3075,8 @@ merge(Compressor.prototype, {
|
|||||||
}
|
}
|
||||||
if (left
|
if (left
|
||||||
&& !(left instanceof AST_SymbolRef
|
&& !(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 parent, field;
|
||||||
var cdr = self.cdr;
|
var cdr = self.cdr;
|
||||||
while (true) {
|
while (true) {
|
||||||
|
|||||||
@@ -1592,3 +1592,49 @@ var_side_effects_3: {
|
|||||||
}
|
}
|
||||||
expect_stdout: true
|
expect_stdout: true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
reassign_const_1: {
|
||||||
|
options = {
|
||||||
|
collapse_vars: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
function f() {
|
||||||
|
const a = 1;
|
||||||
|
a = 2;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
console.log(f());
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
function f() {
|
||||||
|
const a = 1;
|
||||||
|
a = 2;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
console.log(f());
|
||||||
|
}
|
||||||
|
expect_stdout: true
|
||||||
|
}
|
||||||
|
|
||||||
|
reassign_const_2: {
|
||||||
|
options = {
|
||||||
|
collapse_vars: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
function f() {
|
||||||
|
const a = 1;
|
||||||
|
++a;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
console.log(f());
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
function f() {
|
||||||
|
const a = 1;
|
||||||
|
++a;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
console.log(f());
|
||||||
|
}
|
||||||
|
expect_stdout: true
|
||||||
|
}
|
||||||
|
|||||||
@@ -1064,3 +1064,28 @@ issue_1830_2: {
|
|||||||
}
|
}
|
||||||
expect_stdout: "1"
|
expect_stdout: "1"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
reassign_const: {
|
||||||
|
options = {
|
||||||
|
cascade: true,
|
||||||
|
sequences: true,
|
||||||
|
side_effects: true,
|
||||||
|
unused: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
function f() {
|
||||||
|
const a = 1;
|
||||||
|
a = 2;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
console.log(f());
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
function f() {
|
||||||
|
const a = 1;
|
||||||
|
return a = 2, a;
|
||||||
|
}
|
||||||
|
console.log(f());
|
||||||
|
}
|
||||||
|
expect_stdout: true
|
||||||
|
}
|
||||||
|
|||||||
@@ -610,3 +610,27 @@ delete_seq_6: {
|
|||||||
}
|
}
|
||||||
expect_stdout: true
|
expect_stdout: true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
reassign_const: {
|
||||||
|
options = {
|
||||||
|
cascade: true,
|
||||||
|
sequences: true,
|
||||||
|
side_effects: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
function f() {
|
||||||
|
const a = 1;
|
||||||
|
a++;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
console.log(f());
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
function f() {
|
||||||
|
const a = 1;
|
||||||
|
return a++, a;
|
||||||
|
}
|
||||||
|
console.log(f());
|
||||||
|
}
|
||||||
|
expect_stdout: true
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user