Skip to content

Commit

Permalink
Avoid computing the .length of referred arrays in case of mutations. C…
Browse files Browse the repository at this point in the history
…loses #1380
  • Loading branch information
fabiosantoscode committed Apr 18, 2023
1 parent 028514c commit dd6ebd3
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 11 deletions.
12 changes: 2 additions & 10 deletions lib/compress/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,24 +359,16 @@ def_eval(AST_PropAccess, function (compressor, depth) {
return obj.length;
}

const is_spreadless_array = obj =>
const is_spreadless_array =
obj instanceof AST_Array
&& obj.elements.every(el => !(el instanceof AST_Expansion));

if (
is_spreadless_array(obj)
is_spreadless_array
&& obj.elements.every(el => !el.has_side_effects(compressor))
) {
return obj.elements.length;
}

let fixed;
if (
obj instanceof AST_SymbolRef
&& is_spreadless_array((fixed = obj.definition().fixed_value()))
) {
return fixed.elements.length;
}
}

if (compressor.option("unsafe")) {
Expand Down
2 changes: 1 addition & 1 deletion test/compress/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ safe_array_string_length: {
}
expect: {
const array_ref = [1, leak("side effect")]
console.log(4, 2, 2, [id("side eff")]);
console.log(4, 2, array_ref.length, [id("side eff")]);
}
expect_stdout: "4 2 2 [ 'side eff' ]"
}
Expand Down

0 comments on commit dd6ebd3

Please sign in to comment.