Skip to content

Commit 27c9ec2

Browse files
committed
Properly resolve defaultProps identifiers
The defaultProps handler didn't resolve identifiers for class or statement components. Now it does. This also updates main-test to use references for propTypes and defaultProps to verify the are correctly resolved.
1 parent 1386a4e commit 27c9ec2

File tree

2 files changed

+69
-50
lines changed

2 files changed

+69
-50
lines changed

src/__tests__/main-test.js

Lines changed: 64 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,25 @@ describe('main', () => {
5656
test(`
5757
var React = require("React");
5858
var PropTypes = React.PropTypes;
59+
60+
var defaultProps = {
61+
foo: true,
62+
};
63+
var propTypes = {
64+
/**
65+
* Example prop description
66+
*/
67+
foo: PropTypes.bool
68+
};
69+
5970
/**
6071
* Example component description
6172
*/
6273
var Component = React.createClass({
6374
displayName: 'ABC',
64-
propTypes: {
65-
/**
66-
* Example prop description
67-
*/
68-
foo: PropTypes.bool
69-
},
75+
propTypes,
7076
getDefaultProps: function() {
71-
return {
72-
foo: true
73-
};
77+
return defaultProps;
7478
}
7579
});
7680
module.exports = Component
@@ -79,23 +83,27 @@ describe('main', () => {
7983

8084
describe('Class definition', () => {
8185
test(`
82-
var React = require("React");
83-
var PropTypes = React.PropTypes;
86+
const React = require("React");
87+
const PropTypes = React.PropTypes;
88+
89+
const defaultProps = {
90+
foo: true,
91+
};
92+
const propTypes = {
93+
/**
94+
* Example prop description
95+
*/
96+
foo: PropTypes.bool
97+
};
98+
8499
/**
85100
* Example component description
86101
*/
87102
export default class Component extends React.Component {
88-
static propTypes = {
89-
/**
90-
* Example prop description
91-
*/
92-
foo: PropTypes.bool
93-
};
103+
static propTypes = propTypes;
94104
// ...
95105
}
96-
Component.defaultProps = {
97-
foo: true,
98-
};
106+
Component.defaultProps = defaultProps;
99107
Component.displayName = 'ABC';
100108
`);
101109
});
@@ -104,21 +112,23 @@ describe('main', () => {
104112
test(`
105113
import React, {PropTypes} from "React";
106114
115+
const defaultProps = {
116+
foo: true,
117+
};
118+
const propTypes = {
119+
/**
120+
* Example prop description
121+
*/
122+
foo: PropTypes.bool
123+
};
124+
107125
/**
108126
* Example component description
109127
*/
110128
let Component = props => <div />;
111129
Component.displayName = 'ABC';
112-
Component.defaultProps = {
113-
foo: true
114-
};
115-
116-
Component.propTypes = {
117-
/**
118-
* Example prop description
119-
*/
120-
foo: PropTypes.bool
121-
};
130+
Component.defaultProps = defaultProps;
131+
Component.propTypes = propTypes;
122132
123133
export default Component;
124134
`);
@@ -128,6 +138,16 @@ describe('main', () => {
128138
test(`
129139
import React, {PropTypes} from "React";
130140
141+
const defaultProps = {
142+
foo: true,
143+
};
144+
const propTypes = {
145+
/**
146+
* Example prop description
147+
*/
148+
foo: PropTypes.bool
149+
};
150+
131151
/**
132152
* Example component description
133153
*/
@@ -136,16 +156,8 @@ describe('main', () => {
136156
}
137157
138158
Component.displayName = 'ABC';
139-
Component.defaultProps = {
140-
foo: true
141-
};
142-
143-
Component.propTypes = {
144-
/**
145-
* Example prop description
146-
*/
147-
foo: PropTypes.bool
148-
};
159+
Component.defaultProps = defaultProps;
160+
Component.propTypes = propTypes;
149161
150162
export default Component;
151163
`);
@@ -155,6 +167,16 @@ describe('main', () => {
155167
test(`
156168
import React, {PropTypes} from "React";
157169
170+
const defaultProps = {
171+
foo: true,
172+
};
173+
const propTypes = {
174+
/**
175+
* Example prop description
176+
*/
177+
foo: PropTypes.bool
178+
};
179+
158180
/**
159181
* Example component description
160182
*/
@@ -163,16 +185,8 @@ describe('main', () => {
163185
}
164186
165187
Component.displayName = 'ABC';
166-
Component.defaultProps = {
167-
foo: true
168-
};
169-
170-
Component.propTypes = {
171-
/**
172-
* Example prop description
173-
*/
174-
foo: PropTypes.bool
175-
};
188+
Component.defaultProps = defaultProps;
189+
Component.propTypes = propTypes;
176190
177191
export default Component;
178192
`);

src/handlers/defaultPropsHandler.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ export default function defaultPropsHandler(
5252
return;
5353
}
5454

55+
defaultPropsPath = resolveToValue(defaultPropsPath);
56+
if (!defaultPropsPath) {
57+
return;
58+
}
59+
5560
if (types.FunctionExpression.check(defaultPropsPath.node)) {
5661
// Find the value that is returned from the function and process it if it is
5762
// an object literal.

0 commit comments

Comments
 (0)