Skip to content

Commit

Permalink
fix(es/es2015): Fix injection location of this for getter/setter pr…
Browse files Browse the repository at this point in the history
…operties (#8993)

**Related issue:**

 - Closes #8992

---------

Co-authored-by: magic-akari <akari.ccino@gmail.com>
  • Loading branch information
kdy1 and magic-akari committed May 31, 2024
1 parent 4e1adfc commit 09121a6
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 0 deletions.
19 changes: 19 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8992/input/.swcrc
Original file line number Diff line number Diff line change
@@ -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 changes: 10 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8992/input/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const myObj = {
get foo() {
return () => this;
},
};

const fn = myObj.foo;

// should be true
console.log(fn() === myObj);
13 changes: 13 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8992/input/2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const o1 = {
x: "a",
foo() {
const o2 = {
get [(() => this.x)()]() {
return 1;
},
};
console.log(o2.a === 1);
},
};

o1.foo();
10 changes: 10 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8992/output/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var myObj = {
get foo () {
var _this = this;
return function() {
return _this;
};
}
};
var fn = myObj.foo;
console.log(fn() === myObj);
19 changes: 19 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8992/output/2.js
Original file line number Diff line number Diff line change
@@ -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();
33 changes: 33 additions & 0 deletions crates/swc_ecma_compat_es2015/src/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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 {
Expand Down

0 comments on commit 09121a6

Please sign in to comment.