Skip to content

Commit

Permalink
fix(es/minifier): Check this in function params (#9229)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #9186
  • Loading branch information
CPunisher authored Jul 13, 2024
1 parent 2f22fee commit da4866d
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 1 deletion.
6 changes: 5 additions & 1 deletion crates/swc_ecma_minifier/src/compress/pure/arrows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ impl Pure<'_> {
function,
}) = e
{
if contains_this_expr(&function.body) || function.is_generator {
if function.params.iter().any(contains_this_expr)
|| contains_this_expr(&function.body)
|| function.is_generator
{
return;
}

Expand Down Expand Up @@ -65,6 +68,7 @@ impl Pure<'_> {
if m.function.is_generator
|| contains_arguments(&m.function.body)
|| contains_super(&m.function.body)
|| m.function.params.iter().any(contains_this_expr)
{
return;
}
Expand Down
15 changes: 15 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/9186/1/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
o = {
foo() {
return val;
},
s: "test",
};
console.log(o.foo().length);

o = {
foo(val = this.s) {
return val;
},
s: "test",
};
console.log(o.foo().length);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
console.log((o = {
foo: ()=>val,
s: "test"
}).foo().length), console.log((o = {
foo (val1 = this.s) {
return val1;
},
s: "test"
}).foo().length);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"unsafe_arrows": true,
"ecma": 2015,
"evaluate": true,
"side_effects": true
}
14 changes: 14 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/9186/2/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
console.log(
(function () {
while (true) {
console.log(123);
}
})()
);
console.log(
(function (a = this.a) {
while (true) {
console.log(123);
}
})()
);
10 changes: 10 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/9186/2/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
console.log((()=>{
while(true){
console.log(123);
}
})());
console.log(function(a = this.a) {
while(true){
console.log(123);
}
}());

0 comments on commit da4866d

Please sign in to comment.