fix(es/es2015): Fix injection location of this for getter/setter properties (#8993)

**Related issue:**

 - Closes #8992

---------

Co-authored-by: magic-akari <akari.ccino@gmail.com>
This commit is contained in:
Donny/강동윤 2024-05-31 09:32:00 +09:00 committed by GitHub
parent 4e1adfcc88
commit 09121a61cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 104 additions and 0 deletions

View File

@ -0,0 +1,19 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": false
},
"target": "es5",
"loose": false,
"minify": {
"compress": false,
"mangle": false
}
},
"module": {
"type": "es6"
},
"minify": false,
"isModule": false
}

View File

@ -0,0 +1,10 @@
const myObj = {
get foo() {
return () => this;
},
};
const fn = myObj.foo;
// should be true
console.log(fn() === myObj);

View File

@ -0,0 +1,13 @@
const o1 = {
x: "a",
foo() {
const o2 = {
get [(() => this.x)()]() {
return 1;
},
};
console.log(o2.a === 1);
},
};
o1.foo();

View File

@ -0,0 +1,10 @@
var myObj = {
get foo () {
var _this = this;
return function() {
return _this;
};
}
};
var fn = myObj.foo;
console.log(fn() === myObj);

View File

@ -0,0 +1,19 @@
var _define_enumerable_properties = require("@swc/helpers/_/_define_enumerable_properties");
var o1 = {
x: "a",
foo: function foo() {
var _this = this;
var _obj, _mutatorMap = {};
var o2 = (_obj = {}, _mutatorMap[function() {
return _this.x;
}()] = _mutatorMap[function() {
return _this.x;
}()] || {}, _mutatorMap[function() {
return _this.x;
}()].get = function() {
return 1;
}, _define_enumerable_properties._(_obj, _mutatorMap), _obj);
console.log(o2.a === 1);
}
};
o1.foo();

View File

@ -181,6 +181,22 @@ impl VisitMut for Arrow {
}
}
fn visit_mut_getter_prop(&mut self, f: &mut GetterProp) {
f.key.visit_mut_with(self);
if let Some(body) = &mut f.body {
let old_rep = self.hoister.take();
body.visit_mut_with(self);
let decl = mem::replace(&mut self.hoister, old_rep).to_stmt();
if let Some(stmt) = decl {
prepend_stmt(&mut body.stmts, stmt);
}
}
}
fn visit_mut_module_items(&mut self, stmts: &mut Vec<ModuleItem>) {
stmts.visit_mut_children_with(self);
@ -200,6 +216,23 @@ impl VisitMut for Arrow {
prepend_stmt(&mut script.body, stmt);
}
}
fn visit_mut_setter_prop(&mut self, f: &mut SetterProp) {
f.key.visit_mut_with(self);
f.param.visit_mut_with(self);
if let Some(body) = &mut f.body {
let old_rep = self.hoister.take();
body.visit_mut_with(self);
let decl = mem::replace(&mut self.hoister, old_rep).to_stmt();
if let Some(stmt) = decl {
prepend_stmt(&mut body.stmts, stmt);
}
}
}
}
impl InjectVars for Arrow {