-
Notifications
You must be signed in to change notification settings - Fork 801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add transform to support arrow function class properties #322
Changes from 12 commits
019cd08
c07dcb0
40e5e36
2e957fa
61c0fa9
b4e6dd0
f474b69
caf6d51
752e32d
669b9a5
698d592
28e9332
7c78ec8
43cb67e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"presets": ["es2015", "react"] | ||
} | ||
"presets": ["es2015", "react", "stage-2"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"presets": ["es2015", "stage-2", "react"], | ||
"plugins": ["../../src/babel"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -181,6 +181,235 @@ function runAllTests(useWeakMap) { | |
|
||
expect(wrapper.text()).toBe('new render + old state'); | ||
}); | ||
|
||
it('replaces children class methods', () => { | ||
const spy = createSpy(); | ||
|
||
class App extends Component { | ||
componentWillMount() { | ||
this.state = 'old'; | ||
} | ||
|
||
shouldComponentUpdate() { | ||
return false; | ||
} | ||
|
||
handleClick() { | ||
spy('foo'); | ||
} | ||
|
||
render() { | ||
return ( | ||
<span onClick={this.handleClick}>old render + {this.state} state</span> | ||
); | ||
} | ||
} | ||
RHL.register(App, 'App', 'test.js'); | ||
|
||
const wrapper = mount(<AppContainer><App /></AppContainer>); | ||
wrapper.find('span').simulate('click'); | ||
expect(spy).toHaveBeenCalledWith('foo'); | ||
expect(wrapper.text()).toBe('old render + old state'); | ||
|
||
spy.reset(); | ||
{ | ||
class App extends Component { | ||
componentWillMount() { | ||
this.state = 'new'; | ||
} | ||
|
||
shouldComponentUpdate() { | ||
return false; | ||
} | ||
|
||
handleClick() { | ||
spy('bar'); | ||
} | ||
|
||
render() { | ||
return ( | ||
<span onClick={this.handleClick}>new render + {this.state} state</span> | ||
); | ||
} | ||
} | ||
RHL.register(App, 'App', 'test.js'); | ||
wrapper.setProps({ children: <App /> }); | ||
} | ||
|
||
wrapper.find('span').simulate('click'); | ||
expect(spy).toHaveBeenCalledWith('bar'); | ||
expect(wrapper.text()).toBe('new render + old state'); | ||
}); | ||
|
||
it('replaces children class property arrow functions', () => { | ||
const spy = createSpy(); | ||
|
||
class App extends Component { | ||
componentWillMount() { | ||
this.state = 'old'; | ||
} | ||
|
||
shouldComponentUpdate() { | ||
return false; | ||
} | ||
|
||
handleClick = () => { | ||
spy('foo'); | ||
}; | ||
|
||
render() { | ||
return ( | ||
<span onClick={this.handleClick}>old render + {this.state} state</span> | ||
); | ||
} | ||
} | ||
RHL.register(App, 'App', 'test.js'); | ||
|
||
const wrapper = mount(<AppContainer><App /></AppContainer>); | ||
wrapper.find('span').simulate('click'); | ||
expect(spy).toHaveBeenCalledWith('foo'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above about testing that it doesnt remount |
||
expect(wrapper.text()).toBe('old render + old state'); | ||
|
||
spy.reset(); | ||
{ | ||
class App extends Component { | ||
componentWillMount() { | ||
this.state = 'new'; | ||
} | ||
|
||
shouldComponentUpdate() { | ||
return false; | ||
} | ||
|
||
handleClick = () => { | ||
spy('bar'); | ||
}; | ||
|
||
render() { | ||
return ( | ||
<span onClick={this.handleClick}>new render + {this.state} state</span> | ||
); | ||
} | ||
} | ||
RHL.register(App, 'App', 'test.js'); | ||
wrapper.setProps({ children: <App /> }); | ||
} | ||
|
||
wrapper.find('span').simulate('click'); | ||
expect(spy).toHaveBeenCalledWith('bar'); | ||
expect(wrapper.text()).toBe('new render + old state'); | ||
}); | ||
|
||
it('replaces children class property arrow functions without block statement bodies', () => { | ||
const spy = createSpy(); | ||
|
||
class App extends Component { | ||
componentWillMount() { | ||
this.state = 'old'; | ||
} | ||
|
||
shouldComponentUpdate() { | ||
return false; | ||
} | ||
|
||
handleClick = () => spy('foo'); | ||
|
||
render() { | ||
return ( | ||
<span onClick={this.handleClick}>old render + {this.state} state</span> | ||
); | ||
} | ||
} | ||
RHL.register(App, 'App', 'test.js'); | ||
|
||
const wrapper = mount(<AppContainer><App /></AppContainer>); | ||
wrapper.find('span').simulate('click'); | ||
expect(spy).toHaveBeenCalledWith('foo'); | ||
expect(wrapper.text()).toBe('old render + old state'); | ||
|
||
spy.reset(); | ||
{ | ||
class App extends Component { | ||
componentWillMount() { | ||
this.state = 'new'; | ||
} | ||
|
||
shouldComponentUpdate() { | ||
return false; | ||
} | ||
|
||
handleClick = () => spy('bar'); | ||
|
||
render() { | ||
return ( | ||
<span onClick={this.handleClick}>new render + {this.state} state</span> | ||
); | ||
} | ||
} | ||
RHL.register(App, 'App', 'test.js'); | ||
wrapper.setProps({ children: <App /> }); | ||
} | ||
|
||
wrapper.find('span').simulate('click'); | ||
expect(spy).toHaveBeenCalledWith('bar'); | ||
expect(wrapper.text()).toBe('new render + old state'); | ||
}); | ||
|
||
it('replaces children with class property arrow ' + | ||
'functions with different numbers of arguments', () => { | ||
const spy = createSpy(); | ||
|
||
class App extends Component { | ||
componentWillMount() { | ||
this.state = 'old'; | ||
} | ||
|
||
shouldComponentUpdate() { | ||
return false; | ||
} | ||
|
||
handleClick = () => spy('foo'); | ||
|
||
render() { | ||
return ( | ||
<span onClick={this.handleClick}>old render + {this.state} state</span> | ||
); | ||
} | ||
} | ||
RHL.register(App, 'App', 'test.js'); | ||
|
||
const wrapper = mount(<AppContainer><App /></AppContainer>); | ||
wrapper.find('span').simulate('click'); | ||
expect(spy).toHaveBeenCalledWith('foo'); | ||
expect(wrapper.text()).toBe('old render + old state'); | ||
|
||
spy.reset(); | ||
{ | ||
class App extends Component { | ||
componentWillMount() { | ||
this.state = 'new'; | ||
} | ||
|
||
shouldComponentUpdate() { | ||
return false; | ||
} | ||
|
||
handleClick = ({ target }) => spy(target.value); | ||
|
||
render() { | ||
return ( | ||
<span onClick={this.handleClick}>new render + {this.state} state</span> | ||
); | ||
} | ||
} | ||
RHL.register(App, 'App', 'test.js'); | ||
wrapper.setProps({ children: <App /> }); | ||
} | ||
|
||
wrapper.find('span').simulate('click', { target: { value: 'bar' } }); | ||
expect(spy).toHaveBeenCalledWith('bar'); | ||
expect(wrapper.text()).toBe('new render + old state'); | ||
}); | ||
}); | ||
|
||
describe('with createClass root', () => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["syntax-class-properties", "../../../../../src/babel"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Foo { | ||
bar = (a, b) => { | ||
return a(b); | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
var _this = this; | ||
|
||
class Foo { | ||
bar = (...params) => _this.__bar__REACT_HOT_LOADER__(...params); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just noticed this line. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @janv I'd noticed that before and thought it was weird too (like, how would it work at all if it was really outputting this code?), but that's apparently what happens when you use babel with only |
||
|
||
__bar__REACT_HOT_LOADER__(a, b) { | ||
return a(b); | ||
} | ||
|
||
} | ||
; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not related with this PR I think but what's up with this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assumed it was Babel weirdness, I was making the tests pass here. According to AST explorer (highlight the semicolon) that's an additional empty statement, I'll see if I'm accidentally creating one. |
||
|
||
(function () { | ||
if (typeof __REACT_HOT_LOADER__ === 'undefined') { | ||
return; | ||
} | ||
|
||
__REACT_HOT_LOADER__.register(Foo, "Foo", __FILENAME__); | ||
})(); | ||
|
||
; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["syntax-class-properties", "../../../../../src/babel"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Foo { | ||
bar = (a = "foo") => { | ||
return `${a}bar`; | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
var _this = this; | ||
|
||
class Foo { | ||
bar = (...params) => _this.__bar__REACT_HOT_LOADER__(...params); | ||
|
||
__bar__REACT_HOT_LOADER__(a = "foo") { | ||
return `${ a }bar`; | ||
} | ||
|
||
} | ||
; | ||
|
||
(function () { | ||
if (typeof __REACT_HOT_LOADER__ === 'undefined') { | ||
return; | ||
} | ||
|
||
__REACT_HOT_LOADER__.register(Foo, "Foo", __FILENAME__); | ||
})(); | ||
|
||
; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["syntax-class-properties", "../../../../../src/babel"] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@calesce it might be worth it to test that the method is replaced without remounting the component, eg. like it is done here https://github.com/gaearon/react-hot-loader/blob/next/test/AppContainer/AppContainer.dev.js#L182
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, doesn't hurt.