Skip to content

Commit cb88509

Browse files
author
Keyan Zhang
committed
updated constructor args (only use props/context when needed)
1 parent d4faea4 commit cb88509

File tree

6 files changed

+66
-20
lines changed

6 files changed

+66
-20
lines changed

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

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

33
class ComponentWithOnlyPureRenderMixin extends React.PureComponent {
4-
constructor(props, context) {
5-
super(props, context);
4+
constructor(props) {
5+
super(props);
66

77
this.state = {
88
counter: props.initialNumber + 1,

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

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

44
class ComponentWithOnlyPureRenderMixin extends React.PureComponent {
5-
constructor(props, context) {
6-
super(props, context);
5+
constructor(props) {
6+
super(props);
77

88
this.state = {
99
counter: props.initialNumber + 1,

transforms/__testfixtures__/class-test2.input.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ var ComponentWithNonSimpleInitialState = React.createClass({
2323
},
2424
});
2525

26+
var ComponentWithBothPropsAndContextAccess = React.createClass({
27+
contextTypes: {
28+
name: React.PropTypes.string,
29+
},
30+
31+
getInitialState: function() {
32+
return {
33+
foo: this.props.foo,
34+
};
35+
},
36+
37+
render: function() {
38+
return (
39+
<div>{this.context.name}</div>
40+
);
41+
},
42+
});
43+
2644
// Comment
2745
module.exports = React.createClass({
2846
propTypes: {

transforms/__testfixtures__/class-test2.output.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class ComponentWithNonSimpleInitialState extends React.Component {
88
static iDontKnowWhyYouNeedThis = true; // but comment it
99
static foo = 'bar';
1010

11-
constructor(props, context) {
12-
super(props, context);
11+
constructor(props) {
12+
super(props);
1313

1414
this.state = {
1515
counter: props.initialNumber + 1,
@@ -23,6 +23,26 @@ class ComponentWithNonSimpleInitialState extends React.Component {
2323
}
2424
}
2525

26+
class ComponentWithBothPropsAndContextAccess extends React.Component {
27+
static contextTypes = {
28+
name: React.PropTypes.string,
29+
};
30+
31+
constructor(props, context) {
32+
super(props, context);
33+
34+
this.state = {
35+
foo: props.foo,
36+
};
37+
}
38+
39+
render() {
40+
return (
41+
<div>{this.context.name}</div>
42+
);
43+
}
44+
}
45+
2646
// Comment
2747
module.exports = class extends React.Component {
2848
static propTypes = {
@@ -33,8 +53,8 @@ module.exports = class extends React.Component {
3353
foo: 12,
3454
};
3555

36-
constructor(props, context) {
37-
super(props, context);
56+
constructor() {
57+
super();
3858
// non-simple getInitialState
3959
var data = 'bar';
4060

transforms/__testfixtures__/class.output.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ var Image = require('Image.react');
99
* Multiline
1010
*/
1111
class MyComponent extends React.Component {
12-
constructor(props, context) {
13-
super(props, context);
12+
constructor(props) {
13+
super(props);
1414
var x = props.foo;
1515

1616
this.state = {
@@ -54,8 +54,8 @@ class MyComponent3 extends React.Component {
5454
};
5555
}();
5656

57-
constructor(props, context) {
58-
super(props, context);
57+
constructor(props) {
58+
super(props);
5959
props.foo();
6060

6161
this.state = {

transforms/class.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -307,26 +307,32 @@ module.exports = (file, api, options) => {
307307
false
308308
), getInitialState);
309309

310-
const createConstructorArgs = () => {
311-
return [j.identifier('props'), j.identifier('context')];
310+
const createConstructorArgs = (hasPropsAccess, hasContextAccess) => {
311+
if (hasContextAccess) {
312+
return [j.identifier('props'), j.identifier('context')];
313+
} else if (hasPropsAccess) {
314+
return [j.identifier('props')];
315+
}
316+
return [];
312317
};
313318

314-
const createConstructor = getInitialState => {
315-
updatePropsAccess(getInitialState);
319+
const createConstructor = (getInitialState, hasContextAccess) => {
320+
const hasPropsAccess = updatePropsAccess(getInitialState).size() > 0;
321+
const constructorArgs = createConstructorArgs(hasPropsAccess, hasContextAccess);
316322

317323
return [
318324
createMethodDefinition({
319325
key: j.identifier('constructor'),
320326
value: j.functionExpression(
321327
null,
322-
createConstructorArgs(),
328+
constructorArgs,
323329
j.blockStatement(
324330
[].concat(
325331
[
326332
j.expressionStatement(
327333
j.callExpression(
328334
j.identifier('super'),
329-
[j.identifier('props'), j.identifier('context')]
335+
constructorArgs
330336
)
331337
),
332338
],
@@ -387,13 +393,15 @@ module.exports = (file, api, options) => {
387393
) => {
388394
let maybeConstructor = [];
389395
const initialStateProperty = [];
390-
396+
const hasContextAccess = !!staticProperties.find((prop) =>
397+
prop.key.name === 'contextTypes'
398+
);
391399
if (isInitialStateLiftable(getInitialState)) {
392400
if (getInitialState) {
393401
initialStateProperty.push(convertInitialStateToClassProperty(getInitialState));
394402
}
395403
} else {
396-
maybeConstructor = createConstructor(getInitialState);
404+
maybeConstructor = createConstructor(getInitialState, hasContextAccess);
397405
}
398406

399407
const propertiesAndMethods = rawProperties.map(prop => {

0 commit comments

Comments
 (0)