fix(es/minifier): Check this in function params (#9229)

**Related issue:**

 - Closes https://github.com/swc-project/swc/issues/9186
This commit is contained in:
CPunisher 2024-07-13 13:52:11 +08:00 committed by GitHub
parent 2f22fee353
commit da4866d13b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 59 additions and 1 deletions

View File

@ -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;
}
@ -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;
}

View File

@ -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);

View File

@ -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);

View File

@ -0,0 +1,6 @@
{
"unsafe_arrows": true,
"ecma": 2015,
"evaluate": true,
"side_effects": true
}

View File

@ -0,0 +1,14 @@
console.log(
(function () {
while (true) {
console.log(123);
}
})()
);
console.log(
(function (a = this.a) {
while (true) {
console.log(123);
}
})()
);

View File

@ -0,0 +1,10 @@
console.log((()=>{
while(true){
console.log(123);
}
})());
console.log(function(a = this.a) {
while(true){
console.log(123);
}
}());