diff --git a/crates/swc/tests/fixture/issues-8xxx/8992/input/.swcrc b/crates/swc/tests/fixture/issues-8xxx/8992/input/.swcrc new file mode 100644 index 000000000000..5d09848c1ff9 --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8992/input/.swcrc @@ -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 +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-8xxx/8992/input/1.js b/crates/swc/tests/fixture/issues-8xxx/8992/input/1.js new file mode 100644 index 000000000000..6c04f245ab97 --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8992/input/1.js @@ -0,0 +1,10 @@ +const myObj = { + get foo() { + return () => this; + }, +}; + +const fn = myObj.foo; + +// should be true +console.log(fn() === myObj); \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-8xxx/8992/input/2.js b/crates/swc/tests/fixture/issues-8xxx/8992/input/2.js new file mode 100644 index 000000000000..3f28043c1ace --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8992/input/2.js @@ -0,0 +1,13 @@ +const o1 = { + x: "a", + foo() { + const o2 = { + get [(() => this.x)()]() { + return 1; + }, + }; + console.log(o2.a === 1); + }, +}; + +o1.foo(); \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-8xxx/8992/output/1.js b/crates/swc/tests/fixture/issues-8xxx/8992/output/1.js new file mode 100644 index 000000000000..ac5040fa21a8 --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8992/output/1.js @@ -0,0 +1,10 @@ +var myObj = { + get foo () { + var _this = this; + return function() { + return _this; + }; + } +}; +var fn = myObj.foo; +console.log(fn() === myObj); diff --git a/crates/swc/tests/fixture/issues-8xxx/8992/output/2.js b/crates/swc/tests/fixture/issues-8xxx/8992/output/2.js new file mode 100644 index 000000000000..344af14e5de2 --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8992/output/2.js @@ -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(); diff --git a/crates/swc_ecma_compat_es2015/src/arrow.rs b/crates/swc_ecma_compat_es2015/src/arrow.rs index 6a36fda8c1fc..8337405b6741 100644 --- a/crates/swc_ecma_compat_es2015/src/arrow.rs +++ b/crates/swc_ecma_compat_es2015/src/arrow.rs @@ -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) { 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 {