Skip to content

Commit

Permalink
Fix name calculation for classes in functions (fixes #264)
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickcr committed Oct 21, 2015
1 parent b84ec2b commit 9f4b4e6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ module.exports = function(context) {
markPropTypesAsUsed(node);
break;
case 'declaration':
var component = componentList.getByName(node.object.name);
var component = componentList.getByName(context.getSource(node.object));
if (!component) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/util/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ function isStatelessFunctionComponent(context, node) {
function getIdentifiers(node) {
var name = [];
var loopNode = node;
var namePart = [];
while (loopNode) {
var namePart;
while (loopNode && (!name.length || loopNode.type !== 'FunctionDeclaration')) {
namePart = (loopNode.id && loopNode.id.name) || (loopNode.key && loopNode.key.name);
if (namePart) {
name.unshift(namePart);
Expand Down
60 changes: 60 additions & 0 deletions tests/lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,34 @@ ruleTester.run('prop-types', rule, {
'};'
].join('\n'),
parser: 'babel-eslint'
}, {
code: [
'function HelloComponent() {',
' class Hello extends React.Component {',
' render() {',
' return <div>Hello {this.props.name}</div>;',
' }',
' }',
' Hello.propTypes = { name: React.PropTypes.string };',
' return Hello;',
'}',
'module.exports = HelloComponent();'
].join('\n'),
parser: 'babel-eslint'
}, {
code: [
'function HelloComponent() {',
' var Hello = React.createClass({',
' propTypes: { name: React.PropTypes.string },',
' render: function() {',
' return <div>Hello {this.props.name}</div>;',
' }',
' });',
' return Hello;',
'}',
'module.exports = HelloComponent();'
].join('\n'),
parser: 'babel-eslint'
}
],

Expand Down Expand Up @@ -1326,6 +1354,38 @@ ruleTester.run('prop-types', rule, {
{message: '\'source\' is missing in props validation for Hello'},
{message: '\'source.uri\' is missing in props validation for Hello'}
]
}, {
code: [
'function HelloComponent() {',
' class Hello extends React.Component {',
' render() {',
' return <div>Hello {this.props.name}</div>;',
' }',
' }',
' return Hello;',
'}',
'module.exports = HelloComponent();'
].join('\n'),
parser: 'babel-eslint',
errors: [
{message: '\'name\' is missing in props validation for Hello'}
]
}, {
code: [
'function HelloComponent() {',
' var Hello = React.createClass({',
' render: function() {',
' return <div>Hello {this.props.name}</div>;',
' }',
' });',
' return Hello;',
'}',
'module.exports = HelloComponent();'
].join('\n'),
parser: 'babel-eslint',
errors: [
{message: '\'name\' is missing in props validation for Hello'}
]
}
]
});

0 comments on commit 9f4b4e6

Please sign in to comment.