Skip to content

Commit

Permalink
Properly resolve defaultProps identifiers
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fkling committed Jan 4, 2016
1 parent 1386a4e commit 27c9ec2
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 50 deletions.
114 changes: 64 additions & 50 deletions src/__tests__/main-test.js
Expand Up @@ -56,21 +56,25 @@ describe('main', () => {
test(`
var React = require("React");
var PropTypes = React.PropTypes;
var defaultProps = {
foo: true,
};
var propTypes = {
/**
* Example prop description
*/
foo: PropTypes.bool
};
/**
* Example component description
*/
var Component = React.createClass({
displayName: 'ABC',
propTypes: {
/**
* Example prop description
*/
foo: PropTypes.bool
},
propTypes,
getDefaultProps: function() {
return {
foo: true
};
return defaultProps;
}
});
module.exports = Component
Expand All @@ -79,23 +83,27 @@ describe('main', () => {

describe('Class definition', () => {
test(`
var React = require("React");
var PropTypes = React.PropTypes;
const React = require("React");
const PropTypes = React.PropTypes;
const defaultProps = {
foo: true,
};
const propTypes = {
/**
* Example prop description
*/
foo: PropTypes.bool
};
/**
* Example component description
*/
export default class Component extends React.Component {
static propTypes = {
/**
* Example prop description
*/
foo: PropTypes.bool
};
static propTypes = propTypes;
// ...
}
Component.defaultProps = {
foo: true,
};
Component.defaultProps = defaultProps;
Component.displayName = 'ABC';
`);
});
Expand All @@ -104,21 +112,23 @@ describe('main', () => {
test(`
import React, {PropTypes} from "React";
const defaultProps = {
foo: true,
};
const propTypes = {
/**
* Example prop description
*/
foo: PropTypes.bool
};
/**
* Example component description
*/
let Component = props => <div />;
Component.displayName = 'ABC';
Component.defaultProps = {
foo: true
};
Component.propTypes = {
/**
* Example prop description
*/
foo: PropTypes.bool
};
Component.defaultProps = defaultProps;
Component.propTypes = propTypes;
export default Component;
`);
Expand All @@ -128,6 +138,16 @@ describe('main', () => {
test(`
import React, {PropTypes} from "React";
const defaultProps = {
foo: true,
};
const propTypes = {
/**
* Example prop description
*/
foo: PropTypes.bool
};
/**
* Example component description
*/
Expand All @@ -136,16 +156,8 @@ describe('main', () => {
}
Component.displayName = 'ABC';
Component.defaultProps = {
foo: true
};
Component.propTypes = {
/**
* Example prop description
*/
foo: PropTypes.bool
};
Component.defaultProps = defaultProps;
Component.propTypes = propTypes;
export default Component;
`);
Expand All @@ -155,6 +167,16 @@ describe('main', () => {
test(`
import React, {PropTypes} from "React";
const defaultProps = {
foo: true,
};
const propTypes = {
/**
* Example prop description
*/
foo: PropTypes.bool
};
/**
* Example component description
*/
Expand All @@ -163,16 +185,8 @@ describe('main', () => {
}
Component.displayName = 'ABC';
Component.defaultProps = {
foo: true
};
Component.propTypes = {
/**
* Example prop description
*/
foo: PropTypes.bool
};
Component.defaultProps = defaultProps;
Component.propTypes = propTypes;
export default Component;
`);
Expand Down
5 changes: 5 additions & 0 deletions src/handlers/defaultPropsHandler.js
Expand Up @@ -52,6 +52,11 @@ export default function defaultPropsHandler(
return;
}

defaultPropsPath = resolveToValue(defaultPropsPath);
if (!defaultPropsPath) {
return;
}

if (types.FunctionExpression.check(defaultPropsPath.node)) {
// Find the value that is returned from the function and process it if it is
// an object literal.
Expand Down

0 comments on commit 27c9ec2

Please sign in to comment.