Skip to content

Commit

Permalink
resolver: handle methods correctly (#579)
Browse files Browse the repository at this point in the history
swc_ecma_transform:
 - handle a class method in child scope (#578)
  • Loading branch information
kdy1 authored Jan 11, 2020
1 parent fea4c5b commit b7f8282
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 1 deletion.
21 changes: 21 additions & 0 deletions ecmascript/transforms/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,27 @@ impl<'a> Fold<FnExpr> for Resolver<'a> {
}
}

impl Fold<ClassMethod> 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<FnDecl> for Resolver<'a> {
fn fold(&mut self, node: FnDecl) -> FnDecl {
// We don't fold this as Hoister handles this.
Expand Down
117 changes: 116 additions & 1 deletion ecmascript/transforms/src/resolver/tests.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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();"
);

0 comments on commit b7f8282

Please sign in to comment.