-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
019cd08
enable stage-1 transforms and add babel-eslint
calesce c07dcb0
Add a passing test showing that component class methods get replaced
calesce 40e5e36
extend the existing babel plugin to transform class property arrow fu…
calesce 2e957fa
support arrow functions without block statement bodies, and add match…
calesce 61c0fa9
only copy the parameter identifier on the class
calesce b4e6dd0
move test into its proper `describe()` block
calesce f474b69
for the class properties transform,
calesce caf6d51
Change class property transform to use rest params/spread operator
calesce 752e32d
Bump stage-1 dependency to stage-2
calesce 669b9a5
Remove unneeded transforms from babel plugin tests
calesce 698d592
extend AppContainer class property tests
calesce 28e9332
update stage-1 references to stage-2
calesce 7c78ec8
opt out of class properties transpile on use of arguments, new.target…
43cb67e
Merge pull request #1 from nfcampos/class-props-fix
calesce File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"presets": ["es2015", "react"] | ||
} | ||
"presets": ["es2015", "react", "stage-2"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"presets": ["es2015", "stage-2", "react"], | ||
"plugins": ["../../src/babel"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', () => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["syntax-class-properties", "../../../../../src/babel"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Foo { | ||
bar = (a, b) => { | ||
arguments; | ||
|
||
return a(b); | ||
}; | ||
} |
19 changes: 19 additions & 0 deletions
19
test/babel/fixtures/class-properties/arguments/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
var _arguments = arguments; | ||
class Foo { | ||
bar = (a, b) => { | ||
_arguments; | ||
|
||
return a(b); | ||
}; | ||
} | ||
; | ||
|
||
(function () { | ||
if (typeof __REACT_HOT_LOADER__ === 'undefined') { | ||
return; | ||
} | ||
|
||
__REACT_HOT_LOADER__.register(Foo, "Foo", __FILENAME__); | ||
})(); | ||
|
||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["syntax-class-properties", "../../../../../src/babel"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Foo { | ||
bar = (a, b) => { | ||
return a(b); | ||
}; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.