Skip to content

Commit 04cb124

Browse files
committed
Fix component detection in prop-types and display-name (fixes #45)
1 parent 106990a commit 04cb124

File tree

4 files changed

+59
-10
lines changed

4 files changed

+59
-10
lines changed

lib/rules/display-name.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ module.exports = function(context) {
1818
var MISSING_MESSAGE = 'Component definition is missing display name';
1919
var MISSING_MESSAGE_NAMED_COMP = '{{component}} component definition is missing display name';
2020

21+
/**
22+
* Checks if the component must be validated
23+
* @param {Object} component The component to process
24+
* @returns {Boolean} True if the component must be validated, false if not.
25+
*/
26+
function mustBeValidated(component) {
27+
return (
28+
component &&
29+
component.isReactComponent &&
30+
!component.hasDisplayName
31+
);
32+
}
33+
2134
/**
2235
* Checks if we are declaring a display name
2336
* @param {ASTNode} node The AST node being checked.
@@ -45,9 +58,6 @@ module.exports = function(context) {
4558
* @param {Object} component The component to process
4659
*/
4760
function reportMissingDisplayName(component) {
48-
if (!component || component.hasDisplayName === true) {
49-
return;
50-
}
5161
context.report(
5262
component.node,
5363
component.name === componentUtil.DEFAULT_COMPONENT_NAME ? MISSING_MESSAGE : MISSING_MESSAGE_NAMED_COMP, {
@@ -87,7 +97,7 @@ module.exports = function(context) {
8797
var list = componentList.getList();
8898
// Report missing display name for all classes
8999
for (var component in list) {
90-
if (!list.hasOwnProperty(component)) {
100+
if (!list.hasOwnProperty(component) || !mustBeValidated(list[component])) {
91101
continue;
92102
}
93103
reportMissingDisplayName(list[component]);
@@ -98,7 +108,9 @@ module.exports = function(context) {
98108
if (!componentUtil.isReactComponent(context, node)) {
99109
return;
100110
}
101-
componentList.set(context, node);
111+
componentList.set(context, node, {
112+
isReactComponent: true
113+
});
102114
}
103115
};
104116

lib/rules/prop-types.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ module.exports = function(context) {
5454
return ignored.indexOf(name) !== -1;
5555
}
5656

57+
/**
58+
* Checks if the component must be validated
59+
* @param {Object} component The component to process
60+
* @returns {Boolean} True if the component must be validated, false if not.
61+
*/
62+
function mustBeValidated(component) {
63+
return (
64+
component &&
65+
component.isReactComponent &&
66+
component.usedPropTypes &&
67+
!component.ignorePropsValidation
68+
);
69+
}
70+
5771
/**
5872
* Checks if the prop is declared
5973
* @param {String} name Name of the prop to check.
@@ -145,9 +159,6 @@ module.exports = function(context) {
145159
* @param {Object} component The component to process
146160
*/
147161
function reportUndeclaredPropTypes(component) {
148-
if (!component || !component.usedPropTypes || component.ignorePropsValidation === true) {
149-
return;
150-
}
151162
var name;
152163
for (var i = 0, j = component.usedPropTypes.length; i < j; i++) {
153164
name = component.usedPropTypes[i].name;
@@ -208,7 +219,7 @@ module.exports = function(context) {
208219
var list = componentList.getList();
209220
// Report undeclared proptypes for all classes
210221
for (var component in list) {
211-
if (!list.hasOwnProperty(component)) {
222+
if (!list.hasOwnProperty(component) || !mustBeValidated(list[component])) {
212223
continue;
213224
}
214225
reportUndeclaredPropTypes(list[component]);
@@ -219,7 +230,9 @@ module.exports = function(context) {
219230
if (!componentUtil.isReactComponent(context, node)) {
220231
return;
221232
}
222-
componentList.set(context, node);
233+
componentList.set(context, node, {
234+
isReactComponent: true
235+
});
223236
}
224237
};
225238

tests/lib/rules/display-name.js

+12
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ eslintTester.addRuleTest('lib/rules/display-name', {
4343
classes: true,
4444
jsx: true
4545
}
46+
}, {
47+
code: [
48+
'class Hello {',
49+
' render() {',
50+
' return \'Hello World\';',
51+
' }',
52+
'}'
53+
].join('\n'),
54+
ecmaFeatures: {
55+
classes: true,
56+
jsx: true
57+
}
4658
}],
4759

4860
invalid: [{

tests/lib/rules/prop-types.js

+12
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,18 @@ eslintTester.addRuleTest('lib/rules/prop-types', {
153153
ecmaFeatures: {
154154
jsx: true
155155
}
156+
}, {
157+
code: [
158+
'class Hello {',
159+
' render() {',
160+
' return \'Hello\' + this.props.name;',
161+
' }',
162+
'}'
163+
].join('\n'),
164+
ecmaFeatures: {
165+
classes: true,
166+
jsx: true
167+
}
156168
}
157169
],
158170

0 commit comments

Comments
 (0)