Skip to content

Commit 3936f55

Browse files
author
Keyan Zhang
committed
handle constructor arguments properly
1 parent c4c3c1a commit 3936f55

10 files changed

+276
-124
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import React from 'React';
2+
3+
/*
4+
* Multiline
5+
*/
6+
var MyComponent = React.createClass({
7+
getInitialState: function() {
8+
var x = this.props.foo;
9+
return {
10+
heyoo: 23,
11+
};
12+
},
13+
14+
foo: function(): void {
15+
this.setState({heyoo: 24});
16+
},
17+
});
18+
19+
var ComponentWithBothPropsAndContextAccess = React.createClass({
20+
contextTypes: {
21+
name: React.PropTypes.string,
22+
},
23+
24+
// we actually _don't_ need a constructor here since this will be
25+
// initialized after a proper super(props, context) call.
26+
// in other words, `this` will be ready when it reaches here.
27+
getInitialState: function() {
28+
return {
29+
foo: this.props.foo,
30+
bar: this.context.bar,
31+
};
32+
},
33+
34+
render: function() {
35+
return (
36+
<div>{this.context.name}</div>
37+
);
38+
},
39+
});
40+
41+
const App = React.createClass({
42+
getInitialState() {
43+
const state = this.calculateState(); // _might_ use `this.context`
44+
return state;
45+
},
46+
calculateState() {
47+
return { color: this.context.color };
48+
},
49+
render() {
50+
return <div />;
51+
},
52+
});
53+
54+
const App2 = React.createClass({
55+
getInitialState() {
56+
const state = {
57+
whatever: this.context.whatever,
58+
};
59+
return state;
60+
},
61+
render() {
62+
return <div />;
63+
},
64+
});
65+
66+
App.contextTypes = {
67+
whatever: React.PropTypes.object,
68+
};
69+
70+
var MyComponent2 = React.createClass({
71+
getInitialState: function() {
72+
var x = this.props.foo.bar.wow.so.deep;
73+
return {
74+
heyoo: 23,
75+
};
76+
},
77+
78+
foo: function(): void {
79+
this.setState({heyoo: 24});
80+
},
81+
});
82+
83+
const getContextFromInstance = (x) => x.context; // meh
84+
85+
var MyComponent3 = React.createClass({
86+
getInitialState: function() {
87+
var x = getContextFromInstance(this);
88+
return {
89+
heyoo: x,
90+
};
91+
},
92+
93+
foo: function(): void {
94+
this.setState({heyoo: 24});
95+
},
96+
});
97+
98+
var MyComponent4 = React.createClass({
99+
getInitialState: function() {
100+
return {
101+
heyoo: getContextFromInstance(this),
102+
};
103+
},
104+
105+
foo: function(): void {
106+
this.setState({heyoo: 24});
107+
},
108+
});
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import React from 'React';
2+
3+
/*
4+
* Multiline
5+
*/
6+
class MyComponent extends React.Component {
7+
constructor(props) {
8+
super(props);
9+
var x = props.foo;
10+
11+
this.state = {
12+
heyoo: 23,
13+
};
14+
}
15+
16+
foo = (): void => {
17+
this.setState({heyoo: 24});
18+
};
19+
}
20+
21+
class ComponentWithBothPropsAndContextAccess extends React.Component {
22+
static contextTypes = {
23+
name: React.PropTypes.string,
24+
};
25+
26+
// we actually _don't_ need a constructor here since this will be
27+
// initialized after a proper super(props, context) call.
28+
// in other words, `this` will be ready when it reaches here.
29+
state = {
30+
foo: this.props.foo,
31+
bar: this.context.bar,
32+
};
33+
34+
render() {
35+
return (
36+
<div>{this.context.name}</div>
37+
);
38+
}
39+
}
40+
41+
class App extends React.Component {
42+
constructor(props, context) {
43+
super(props, context);
44+
const state = this.calculateState(); // _might_ use `this.context`
45+
this.state = state;
46+
}
47+
48+
calculateState = () => {
49+
return { color: this.context.color };
50+
};
51+
52+
render() {
53+
return <div />;
54+
}
55+
}
56+
57+
class App2 extends React.Component {
58+
constructor(props, context) {
59+
super(props, context);
60+
const state = {
61+
whatever: this.context.whatever,
62+
};
63+
this.state = state;
64+
}
65+
66+
render() {
67+
return <div />;
68+
}
69+
}
70+
71+
App.contextTypes = {
72+
whatever: React.PropTypes.object,
73+
};
74+
75+
class MyComponent2 extends React.Component {
76+
constructor(props) {
77+
super(props);
78+
var x = props.foo.bar.wow.so.deep;
79+
80+
this.state = {
81+
heyoo: 23,
82+
};
83+
}
84+
85+
foo = (): void => {
86+
this.setState({heyoo: 24});
87+
};
88+
}
89+
90+
const getContextFromInstance = (x) => x.context; // meh
91+
92+
class MyComponent3 extends React.Component {
93+
constructor(props, context) {
94+
super(props, context);
95+
var x = getContextFromInstance(this);
96+
97+
this.state = {
98+
heyoo: x,
99+
};
100+
}
101+
102+
foo = (): void => {
103+
this.setState({heyoo: 24});
104+
};
105+
}
106+
107+
class MyComponent4 extends React.Component {
108+
state = {
109+
heyoo: getContextFromInstance(this),
110+
};
111+
112+
foo = (): void => {
113+
this.setState({heyoo: 24});
114+
};
115+
}

transforms/__testfixtures__/class-pure-mixin1.output.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
var React = require('React');
22

33
class ComponentWithOnlyPureRenderMixin extends React.PureComponent {
4-
constructor(props) {
5-
super(props);
6-
7-
this.state = {
8-
counter: props.initialNumber + 1,
9-
};
10-
}
4+
state = {
5+
counter: this.props.initialNumber + 1,
6+
};
117

128
render() {
139
return (

transforms/__testfixtures__/class-pure-mixin2.output.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@ import React from 'React';
22
import dontPruneMe from 'foobar';
33

44
class ComponentWithOnlyPureRenderMixin extends React.PureComponent {
5-
constructor(props) {
6-
super(props);
7-
8-
this.state = {
9-
counter: props.initialNumber + 1,
10-
};
11-
}
5+
state = {
6+
counter: this.props.initialNumber + 1,
7+
};
128

139
render() {
1410
dontPruneMe();

transforms/__testfixtures__/class-test2.input.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,6 @@ var ComponentWithNonSimpleInitialState = React.createClass({
2626
},
2727
});
2828

29-
var ComponentWithBothPropsAndContextAccess = React.createClass({
30-
contextTypes: {
31-
name: React.PropTypes.string,
32-
},
33-
34-
getInitialState: function() {
35-
return {
36-
foo: this.props.foo,
37-
};
38-
},
39-
40-
render: function() {
41-
return (
42-
<div>{this.context.name}</div>
43-
);
44-
},
45-
});
46-
4729
// Comment
4830
module.exports = React.createClass({
4931
propTypes: {

transforms/__testfixtures__/class-test2.output.js

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,13 @@ class ComponentWithNonSimpleInitialState extends React.Component {
1212
return this;
1313
}
1414

15-
constructor(props) {
16-
super(props);
17-
18-
this.state = {
19-
counter: props.initialNumber + 1,
20-
};
21-
}
22-
23-
render() {
24-
return (
25-
<div>{this.state.counter}</div>
26-
);
27-
}
28-
}
29-
30-
class ComponentWithBothPropsAndContextAccess extends React.Component {
31-
static contextTypes = {
32-
name: React.PropTypes.string,
15+
state = {
16+
counter: this.props.initialNumber + 1,
3317
};
3418

35-
constructor(props, context) {
36-
super(props, context);
37-
38-
this.state = {
39-
foo: props.foo,
40-
};
41-
}
42-
4319
render() {
4420
return (
45-
<div>{this.context.name}</div>
21+
<div>{this.state.counter}</div>
4622
);
4723
}
4824
}
@@ -57,8 +33,8 @@ module.exports = class extends React.Component {
5733
foo: 12,
5834
};
5935

60-
constructor() {
61-
super();
36+
constructor(props) {
37+
super(props);
6238
// non-simple getInitialState
6339
var data = 'bar';
6440

transforms/__testfixtures__/class.input.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,6 @@ var Relay = require('Relay');
55

66
var Image = require('Image.react');
77

8-
/*
9-
* Multiline
10-
*/
11-
var MyComponent = React.createClass({
12-
getInitialState: function() {
13-
var x = this.props.foo;
14-
return {
15-
heyoo: 23,
16-
};
17-
},
18-
19-
foo: function(): void {
20-
this.setState({heyoo: 24});
21-
},
22-
});
23-
248
// Class comment
259
var MyComponent2 = React.createClass({
2610
getDefaultProps: function(): Object {

transforms/__testfixtures__/class.output.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,6 @@ var Relay = require('Relay');
55

66
var Image = require('Image.react');
77

8-
/*
9-
* Multiline
10-
*/
11-
class MyComponent extends React.Component {
12-
constructor(props) {
13-
super(props);
14-
var x = props.foo;
15-
16-
this.state = {
17-
heyoo: 23,
18-
};
19-
}
20-
21-
foo = (): void => {
22-
this.setState({heyoo: 24});
23-
};
24-
}
25-
268
// Class comment
279
class MyComponent2 extends React.Component {
2810
static defaultProps = {a: 1};

transforms/__tests__/class-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ defineTest(__dirname, 'class', null, 'export-default-class');
2222
defineTest(__dirname, 'class', pureMixinAlternativeOption, 'class-pure-mixin1');
2323
defineTest(__dirname, 'class', null, 'class-pure-mixin2');
2424
defineTest(__dirname, 'class', null, 'class-property-field');
25+
defineTest(__dirname, 'class', null, 'class-initial-state');

0 commit comments

Comments
 (0)