-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
handle constructor arguments properly
- Loading branch information
Keyan Zhang
committed
Jun 22, 2016
1 parent
c4c3c1a
commit 3936f55
Showing
10 changed files
with
276 additions
and
124 deletions.
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
transforms/__testfixtures__/class-initial-state.input.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,108 @@ | ||
import React from 'React'; | ||
|
||
/* | ||
* Multiline | ||
*/ | ||
var MyComponent = React.createClass({ | ||
getInitialState: function() { | ||
var x = this.props.foo; | ||
return { | ||
heyoo: 23, | ||
}; | ||
}, | ||
|
||
foo: function(): void { | ||
this.setState({heyoo: 24}); | ||
}, | ||
}); | ||
|
||
var ComponentWithBothPropsAndContextAccess = React.createClass({ | ||
contextTypes: { | ||
name: React.PropTypes.string, | ||
}, | ||
|
||
// we actually _don't_ need a constructor here since this will be | ||
// initialized after a proper super(props, context) call. | ||
// in other words, `this` will be ready when it reaches here. | ||
getInitialState: function() { | ||
return { | ||
foo: this.props.foo, | ||
bar: this.context.bar, | ||
}; | ||
}, | ||
|
||
render: function() { | ||
return ( | ||
<div>{this.context.name}</div> | ||
); | ||
}, | ||
}); | ||
|
||
const App = React.createClass({ | ||
getInitialState() { | ||
const state = this.calculateState(); // _might_ use `this.context` | ||
return state; | ||
}, | ||
calculateState() { | ||
return { color: this.context.color }; | ||
}, | ||
render() { | ||
return <div />; | ||
}, | ||
}); | ||
|
||
const App2 = React.createClass({ | ||
getInitialState() { | ||
const state = { | ||
whatever: this.context.whatever, | ||
}; | ||
return state; | ||
}, | ||
render() { | ||
return <div />; | ||
}, | ||
}); | ||
|
||
App.contextTypes = { | ||
whatever: React.PropTypes.object, | ||
}; | ||
|
||
var MyComponent2 = React.createClass({ | ||
getInitialState: function() { | ||
var x = this.props.foo.bar.wow.so.deep; | ||
return { | ||
heyoo: 23, | ||
}; | ||
}, | ||
|
||
foo: function(): void { | ||
this.setState({heyoo: 24}); | ||
}, | ||
}); | ||
|
||
const getContextFromInstance = (x) => x.context; // meh | ||
|
||
var MyComponent3 = React.createClass({ | ||
getInitialState: function() { | ||
var x = getContextFromInstance(this); | ||
return { | ||
heyoo: x, | ||
}; | ||
}, | ||
|
||
foo: function(): void { | ||
this.setState({heyoo: 24}); | ||
}, | ||
}); | ||
|
||
var MyComponent4 = React.createClass({ | ||
getInitialState: function() { | ||
return { | ||
heyoo: getContextFromInstance(this), | ||
}; | ||
}, | ||
|
||
foo: function(): void { | ||
this.setState({heyoo: 24}); | ||
}, | ||
}); |
115 changes: 115 additions & 0 deletions
115
transforms/__testfixtures__/class-initial-state.output.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,115 @@ | ||
import React from 'React'; | ||
|
||
/* | ||
* Multiline | ||
*/ | ||
class MyComponent extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
var x = props.foo; | ||
|
||
this.state = { | ||
heyoo: 23, | ||
}; | ||
} | ||
|
||
foo = (): void => { | ||
this.setState({heyoo: 24}); | ||
}; | ||
} | ||
|
||
class ComponentWithBothPropsAndContextAccess extends React.Component { | ||
static contextTypes = { | ||
name: React.PropTypes.string, | ||
}; | ||
|
||
// we actually _don't_ need a constructor here since this will be | ||
// initialized after a proper super(props, context) call. | ||
// in other words, `this` will be ready when it reaches here. | ||
state = { | ||
foo: this.props.foo, | ||
bar: this.context.bar, | ||
}; | ||
|
||
render() { | ||
return ( | ||
<div>{this.context.name}</div> | ||
); | ||
} | ||
} | ||
|
||
class App extends React.Component { | ||
constructor(props, context) { | ||
super(props, context); | ||
const state = this.calculateState(); // _might_ use `this.context` | ||
this.state = state; | ||
} | ||
|
||
calculateState = () => { | ||
return { color: this.context.color }; | ||
}; | ||
|
||
render() { | ||
return <div />; | ||
} | ||
} | ||
|
||
class App2 extends React.Component { | ||
constructor(props, context) { | ||
super(props, context); | ||
const state = { | ||
whatever: this.context.whatever, | ||
}; | ||
this.state = state; | ||
} | ||
|
||
render() { | ||
return <div />; | ||
} | ||
} | ||
|
||
App.contextTypes = { | ||
whatever: React.PropTypes.object, | ||
}; | ||
|
||
class MyComponent2 extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
var x = props.foo.bar.wow.so.deep; | ||
|
||
this.state = { | ||
heyoo: 23, | ||
}; | ||
} | ||
|
||
foo = (): void => { | ||
this.setState({heyoo: 24}); | ||
}; | ||
} | ||
|
||
const getContextFromInstance = (x) => x.context; // meh | ||
|
||
class MyComponent3 extends React.Component { | ||
constructor(props, context) { | ||
super(props, context); | ||
var x = getContextFromInstance(this); | ||
|
||
this.state = { | ||
heyoo: x, | ||
}; | ||
} | ||
|
||
foo = (): void => { | ||
this.setState({heyoo: 24}); | ||
}; | ||
} | ||
|
||
class MyComponent4 extends React.Component { | ||
state = { | ||
heyoo: getContextFromInstance(this), | ||
}; | ||
|
||
foo = (): void => { | ||
this.setState({heyoo: 24}); | ||
}; | ||
} |
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
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
Oops, something went wrong.