Skip to content

Commit ade2c05

Browse files
committed
Fix usage of propTypes with an external object (fixes #9)
1 parent f7792b3 commit ade2c05

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

docs/rules/prop-types.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,12 @@ var Hello = React.createClass({
4242
return <div>Hello {this.props.name}</div>;
4343
}
4444
});
45+
46+
// Referencing an external object disable the rule for the component
47+
var Hello = React.createClass({
48+
propTypes: myPropTypes,
49+
render: function() {
50+
return <div>Hello {this.props.name}</div>;
51+
}
52+
});
4553
```

lib/rules/prop-types.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = function(context) {
1212

1313
var declaredPropTypes = [];
1414
var usedPropTypes = [];
15+
var ignorePropsValidation = false;
1516

1617
function isComponentDefinition(node) {
1718
return (
@@ -48,6 +49,11 @@ module.exports = function(context) {
4849
if (keyName !== 'propTypes') {
4950
return;
5051
}
52+
if (property.value.type !== 'ObjectExpression') {
53+
ignorePropsValidation = true;
54+
return;
55+
}
56+
5157
for (var i = 0, j = property.value.properties.length; i < j; i++) {
5258
declaredPropTypes.push(property.value.properties[i].key.name);
5359
}
@@ -60,14 +66,16 @@ module.exports = function(context) {
6066
return;
6167
}
6268

63-
for (var i = 0, j = usedPropTypes.length; i < j; i++) {
64-
if (declaredPropTypes.indexOf(usedPropTypes[i]) === -1 && usedPropTypes[i] !== 'children') {
65-
context.report(node, '\'' + usedPropTypes[i] + '\' is missing in props validation');
69+
for (var i = 0, j = usedPropTypes.length; !ignorePropsValidation && i < j; i++) {
70+
if (declaredPropTypes.indexOf(usedPropTypes[i]) !== -1 || usedPropTypes[i] === 'children') {
71+
continue;
6672
}
73+
context.report(node, '\'' + usedPropTypes[i] + '\' is missing in props validation');
6774
}
6875

6976
declaredPropTypes.length = 0;
7077
usedPropTypes.length = 0;
78+
ignorePropsValidation = false;
7179
}
7280
};
7381

tests/lib/rules/prop-types.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ eslintTester.addRuleTest('lib/rules/prop-types', {
7676
ecmaFeatures: {
7777
jsx: true
7878
}
79+
}, {
80+
code: '\
81+
var Hello = React.createClass({\
82+
propTypes: externalPropTypes,\
83+
render: function() {\
84+
return <div>Hello {this.props.name}</div>;\
85+
}\
86+
});',
87+
ecmaFeatures: {
88+
jsx: true
89+
}
7990
}
8091
],
8192

0 commit comments

Comments
 (0)