Skip to content

Commit

Permalink
Prevent inlining variables into optional chains. Closes #1198
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiosantoscode committed Feb 12, 2023
1 parent 6268e5f commit 12b7709
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/compress/tighten-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ export function tighten_body(statements, compressor) {
&& (node.logical || node.operator != "=" && lhs.equivalent_to(node.left))
|| node instanceof AST_Await
|| node instanceof AST_Call && lhs instanceof AST_PropAccess && lhs.equivalent_to(node.expression)
||
(node instanceof AST_Call || node instanceof AST_PropAccess)
&& node.optional
|| node instanceof AST_Debugger
|| node instanceof AST_Destructuring
|| node instanceof AST_Expansion
Expand Down Expand Up @@ -454,7 +457,11 @@ export function tighten_body(statements, compressor) {
var hit = funarg;
var abort = false, replaced = 0, can_replace = !args || !hit;
if (!can_replace) {
for (var j = compressor.self().argnames.lastIndexOf(candidate.name) + 1; !abort && j < args.length; j++) {
for (
let j = compressor.self().argnames.lastIndexOf(candidate.name) + 1;
!abort && j < args.length;
j++
) {
args[j].transform(scanner);
}
can_replace = true;
Expand Down
38 changes: 38 additions & 0 deletions test/compress/collapse_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -6073,6 +6073,44 @@ do_not_place_chain_on_lhs_2: {
}
}

optional_chain_call_argument_side_effect: {
options = {
collapse_vars: true,
}
input: {
((this_is_null) => {
const results = console.log("PASS");
this_is_null?.(results);
})(id(null));
}
expect: {
((this_is_null) => {
const results = console.log("PASS");
this_is_null?.(results);
})(id(null));
}
expect_stdout: "PASS"
}

optional_chain_call_prop_side_effect: {
options = {
collapse_vars: true,
}
input: {
((this_is_null) => {
const results = console.log("PASS");
this_is_null?.[results];
})(id(null));
}
expect: {
((this_is_null) => {
const results = console.log("PASS");
this_is_null?.[results];
})(id(null));
}
expect_stdout: "PASS"
}

shadowed_variable: {
options = {
pure_getters: true,
Expand Down

0 comments on commit 12b7709

Please sign in to comment.