Skip to content

Commit 09121a6

Browse files
kdy1magic-akari
andauthoredMay 31, 2024··
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>
1 parent 4e1adfc commit 09121a6

File tree

6 files changed

+104
-0
lines changed

6 files changed

+104
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"jsc": {
3+
"parser": {
4+
"syntax": "ecmascript",
5+
"jsx": false
6+
},
7+
"target": "es5",
8+
"loose": false,
9+
"minify": {
10+
"compress": false,
11+
"mangle": false
12+
}
13+
},
14+
"module": {
15+
"type": "es6"
16+
},
17+
"minify": false,
18+
"isModule": false
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const myObj = {
2+
get foo() {
3+
return () => this;
4+
},
5+
};
6+
7+
const fn = myObj.foo;
8+
9+
// should be true
10+
console.log(fn() === myObj);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const o1 = {
2+
x: "a",
3+
foo() {
4+
const o2 = {
5+
get [(() => this.x)()]() {
6+
return 1;
7+
},
8+
};
9+
console.log(o2.a === 1);
10+
},
11+
};
12+
13+
o1.foo();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var myObj = {
2+
get foo () {
3+
var _this = this;
4+
return function() {
5+
return _this;
6+
};
7+
}
8+
};
9+
var fn = myObj.foo;
10+
console.log(fn() === myObj);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var _define_enumerable_properties = require("@swc/helpers/_/_define_enumerable_properties");
2+
var o1 = {
3+
x: "a",
4+
foo: function foo() {
5+
var _this = this;
6+
var _obj, _mutatorMap = {};
7+
var o2 = (_obj = {}, _mutatorMap[function() {
8+
return _this.x;
9+
}()] = _mutatorMap[function() {
10+
return _this.x;
11+
}()] || {}, _mutatorMap[function() {
12+
return _this.x;
13+
}()].get = function() {
14+
return 1;
15+
}, _define_enumerable_properties._(_obj, _mutatorMap), _obj);
16+
console.log(o2.a === 1);
17+
}
18+
};
19+
o1.foo();

‎crates/swc_ecma_compat_es2015/src/arrow.rs

+33
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,22 @@ impl VisitMut for Arrow {
181181
}
182182
}
183183

184+
fn visit_mut_getter_prop(&mut self, f: &mut GetterProp) {
185+
f.key.visit_mut_with(self);
186+
187+
if let Some(body) = &mut f.body {
188+
let old_rep = self.hoister.take();
189+
190+
body.visit_mut_with(self);
191+
192+
let decl = mem::replace(&mut self.hoister, old_rep).to_stmt();
193+
194+
if let Some(stmt) = decl {
195+
prepend_stmt(&mut body.stmts, stmt);
196+
}
197+
}
198+
}
199+
184200
fn visit_mut_module_items(&mut self, stmts: &mut Vec<ModuleItem>) {
185201
stmts.visit_mut_children_with(self);
186202

@@ -200,6 +216,23 @@ impl VisitMut for Arrow {
200216
prepend_stmt(&mut script.body, stmt);
201217
}
202218
}
219+
220+
fn visit_mut_setter_prop(&mut self, f: &mut SetterProp) {
221+
f.key.visit_mut_with(self);
222+
f.param.visit_mut_with(self);
223+
224+
if let Some(body) = &mut f.body {
225+
let old_rep = self.hoister.take();
226+
227+
body.visit_mut_with(self);
228+
229+
let decl = mem::replace(&mut self.hoister, old_rep).to_stmt();
230+
231+
if let Some(stmt) = decl {
232+
prepend_stmt(&mut body.stmts, stmt);
233+
}
234+
}
235+
}
203236
}
204237

205238
impl InjectVars for Arrow {

0 commit comments

Comments
 (0)
Please sign in to comment.