mirror of
https://github.com/swc-project/swc.git
synced 2024-10-04 12:18:08 +03:00
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:
parent
4e1adfcc88
commit
09121a61cc
19
crates/swc/tests/fixture/issues-8xxx/8992/input/.swcrc
Normal file
19
crates/swc/tests/fixture/issues-8xxx/8992/input/.swcrc
Normal 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
|
||||
}
|
10
crates/swc/tests/fixture/issues-8xxx/8992/input/1.js
Normal file
10
crates/swc/tests/fixture/issues-8xxx/8992/input/1.js
Normal file
@ -0,0 +1,10 @@
|
||||
const myObj = {
|
||||
get foo() {
|
||||
return () => this;
|
||||
},
|
||||
};
|
||||
|
||||
const fn = myObj.foo;
|
||||
|
||||
// should be true
|
||||
console.log(fn() === myObj);
|
13
crates/swc/tests/fixture/issues-8xxx/8992/input/2.js
Normal file
13
crates/swc/tests/fixture/issues-8xxx/8992/input/2.js
Normal file
@ -0,0 +1,13 @@
|
||||
const o1 = {
|
||||
x: "a",
|
||||
foo() {
|
||||
const o2 = {
|
||||
get [(() => this.x)()]() {
|
||||
return 1;
|
||||
},
|
||||
};
|
||||
console.log(o2.a === 1);
|
||||
},
|
||||
};
|
||||
|
||||
o1.foo();
|
10
crates/swc/tests/fixture/issues-8xxx/8992/output/1.js
Normal file
10
crates/swc/tests/fixture/issues-8xxx/8992/output/1.js
Normal file
@ -0,0 +1,10 @@
|
||||
var myObj = {
|
||||
get foo () {
|
||||
var _this = this;
|
||||
return function() {
|
||||
return _this;
|
||||
};
|
||||
}
|
||||
};
|
||||
var fn = myObj.foo;
|
||||
console.log(fn() === myObj);
|
19
crates/swc/tests/fixture/issues-8xxx/8992/output/2.js
Normal file
19
crates/swc/tests/fixture/issues-8xxx/8992/output/2.js
Normal 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();
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user