diff --git a/ecmascript/transforms/src/resolver.rs b/ecmascript/transforms/src/resolver.rs index 9f32a631c124..b84001e0c47e 100644 --- a/ecmascript/transforms/src/resolver.rs +++ b/ecmascript/transforms/src/resolver.rs @@ -208,6 +208,27 @@ impl<'a> Fold for Resolver<'a> { } } +impl Fold for Resolver<'_> { + fn fold(&mut self, m: ClassMethod) -> ClassMethod { + let key = m.key.fold_with(self); + + let function = { + let child_mark = Mark::fresh(self.mark); + + // Child folder + let mut child = Resolver::new( + child_mark, + Scope::new(ScopeKind::Fn, Some(&self.current)), + None, + ); + + m.function.fold_with(&mut child) + }; + + ClassMethod { key, function, ..m } + } +} + impl<'a> Fold for Resolver<'a> { fn fold(&mut self, node: FnDecl) -> FnDecl { // We don't fold this as Hoister handles this. diff --git a/ecmascript/transforms/src/resolver/tests.rs b/ecmascript/transforms/src/resolver/tests.rs index 45942c469a7f..299be8181718 100644 --- a/ecmascript/transforms/src/resolver/tests.rs +++ b/ecmascript/transforms/src/resolver/tests.rs @@ -1,5 +1,8 @@ use super::*; -use crate::compat::es2015::block_scoping; +use crate::{ + compat::es2015::{block_scoping, destructuring, Classes}, + modules::common_js::common_js, +}; use swc_common::chain; use swc_ecma_parser::{EsConfig, Syntax}; @@ -909,3 +912,115 @@ class A extends C { } " ); + +test!( + syntax(), + |_| tr(), + issue_578_1, + " +import { myFunction } from './dep.js' + +class SomeClass { + constructor(properties) { + this.props = properties; + } + call () { + const {myFunction} = this.props + if (myFunction) { + myFunction() + } else { + console.log('DID NOT WORK!') + } + } +} + +let instance = new SomeClass({ + myFunction: () => { + console.log('CORRECT FUNCTION CALLED') + } +}); + +instance.call()", + "import { myFunction } from './dep.js'; +class SomeClass{ + constructor(properties){ + this.props = properties; + } + call() { + var { myFunction: myFunction1 } = this.props; + if (myFunction1) { + myFunction1(); + } else { + console.log('DID NOT WORK!'); + } + } +} +var instance = new SomeClass({ + myFunction: ()=>{ + console.log('CORRECT FUNCTION CALLED'); + } +}); +instance.call()" +); + +test!( + syntax(), + |_| chain!( + tr(), + Classes::default(), + destructuring(Default::default()), + common_js(Default::default()) + ), + issue_578_2, + " +import { myFunction } from './dep.js' + +class SomeClass { + constructor(properties) { + this.props = properties; + } + call () { + const {myFunction} = this.props + if (myFunction) { + myFunction() + } else { + console.log('DID NOT WORK!') + } + } +} + +let instance = new SomeClass({ + myFunction: () => { + console.log('CORRECT FUNCTION CALLED') + } +}); + +instance.call()", + "'use strict'; +var _depJs = require('./dep.js'); +let SomeClass = function() { + 'use strict'; + function SomeClass(properties) { + _classCallCheck(this, SomeClass); + this.props = properties; + } + _createClass(SomeClass, [{ + key: 'call', + value: function call() { + var _props = this.props, myFunction = _props.myFunction; + if (myFunction) { + myFunction(); + } else { + console.log('DID NOT WORK!'); + } + } + }]); + return SomeClass; +}(); +var instance = new SomeClass({ + myFunction: ()=>{ + console.log('CORRECT FUNCTION CALLED'); + } +}); +instance.call();" +);