Skip to content

Commit a7d75cf

Browse files
committed
Improve component detection (fixes #233)
1 parent 65d450a commit a7d75cf

File tree

6 files changed

+103
-140
lines changed

6 files changed

+103
-140
lines changed

lib/rules/display-name.js

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,12 @@ module.exports = function(context) {
2121
var MISSING_MESSAGE = 'Component definition is missing display name';
2222
var MISSING_MESSAGE_NAMED_COMP = '{{component}} component definition is missing display name';
2323

24-
/**
25-
* Checks if the component must be validated
26-
* @param {Object} component The component to process
27-
* @returns {Boolean} True if the component must be validated, false if not.
28-
*/
29-
function mustBeValidated(component) {
30-
return (
31-
component &&
32-
component.isReactComponent &&
33-
!component.hasDisplayName
34-
);
35-
}
36-
3724
/**
3825
* Checks if we are declaring a display name
3926
* @param {ASTNode} node The AST node being checked.
4027
* @returns {Boolean} True if we are declaring a display name, false if not.
4128
*/
4229
function isDisplayNameDeclaration(node) {
43-
4430
// Special case for class properties
4531
// (babel-eslint does not expose property name so we have to rely on tokens)
4632
if (node.type === 'ClassProperty') {
@@ -126,7 +112,6 @@ module.exports = function(context) {
126112
if (!isDisplayNameDeclaration(node)) {
127113
return;
128114
}
129-
130115
markDisplayNameAsDeclared(node);
131116
},
132117

@@ -149,57 +134,39 @@ module.exports = function(context) {
149134
},
150135

151136
ClassDeclaration: function(node) {
137+
componentList.set(context, node);
152138
if (!acceptTranspilerName || !hasTranspilerName(node)) {
153139
return;
154140
}
155141
markDisplayNameAsDeclared(node);
156142
},
157143

158144
ObjectExpression: function(node) {
159-
// Search for the displayName declaration
160-
node.properties.forEach(function(property) {
161-
if (!property.key) {
162-
return;
163-
}
164-
165-
if (!isDisplayNameDeclaration(property.key)) {
166-
return;
167-
}
168-
markDisplayNameAsDeclared(node);
169-
});
170-
// Has transpiler name
171-
if (acceptTranspilerName && hasTranspilerName(node)) {
172-
markDisplayNameAsDeclared(node);
173-
}
174-
175-
if (componentUtil.isComponentDefinition(node)) {
176-
componentList.set(context, node, {
177-
isReactComponent: true
145+
componentList.set(context, node);
146+
if (!acceptTranspilerName || !hasTranspilerName(node)) {
147+
// Search for the displayName declaration
148+
node.properties.forEach(function(property) {
149+
if (!property.key || !isDisplayNameDeclaration(property.key)) {
150+
return;
151+
}
152+
markDisplayNameAsDeclared(node);
178153
});
154+
return;
179155
}
156+
markDisplayNameAsDeclared(node);
180157
},
181158

182159
'Program:exit': function() {
183160
var list = componentList.getList();
184-
// Report missing display name for all classes
161+
// Report missing display name for all components
185162
for (var component in list) {
186-
if (!list.hasOwnProperty(component) || !mustBeValidated(list[component])) {
163+
if (!list.hasOwnProperty(component) || list[component].hasDisplayName) {
187164
continue;
188165
}
189166
reportMissingDisplayName(list[component]);
190167
}
191-
},
192-
193-
ReturnStatement: function(node) {
194-
if (!componentUtil.isReactComponent(context, node)) {
195-
return;
196-
}
197-
componentList.set(context, node, {
198-
isReactComponent: true
199-
});
200168
}
201169
};
202-
203170
};
204171

205172
module.exports.schema = [{

lib/rules/no-direct-mutation-state.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ module.exports = function(context) {
2121
* @returns {Boolean} True if the component is valid, false if not.
2222
*/
2323
function isValid(component) {
24-
var isNotReactComponent = Boolean(component && !component.isReactComponent);
25-
var doNotMutateSetState = Boolean(component && !component.mutateSetState);
26-
27-
return isNotReactComponent || doNotMutateSetState;
24+
return Boolean(component && !component.mutateSetState);
2825
}
2926

3027
/**
@@ -45,6 +42,14 @@ module.exports = function(context) {
4542

4643
return {
4744

45+
ObjectExpression: function(node) {
46+
componentList.set(context, node);
47+
},
48+
49+
ClassDeclaration: function(node) {
50+
componentList.set(context, node);
51+
},
52+
4853
AssignmentExpression: function(node) {
4954
var item;
5055
if (!node.left || !node.left.object || !node.left.object.object) {
@@ -76,15 +81,6 @@ module.exports = function(context) {
7681
}
7782
reportMutations(list[component]);
7883
}
79-
},
80-
81-
ReturnStatement: function(node) {
82-
if (!componentUtil.isReactComponent(context, node)) {
83-
return;
84-
}
85-
componentList.set(context, node, {
86-
isReactComponent: true
87-
});
8884
}
8985
};
9086

lib/rules/no-multi-comp.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ module.exports = function(context) {
2222
// --------------------------------------------------------------------------
2323

2424
return {
25+
26+
ClassDeclaration: function(node) {
27+
componentList.set(context, node);
28+
},
29+
30+
ObjectExpression: function(node) {
31+
componentList.set(context, node);
32+
},
33+
2534
'Program:exit': function() {
2635
if (componentList.count() <= 1) {
2736
return;
@@ -36,13 +45,6 @@ module.exports = function(context) {
3645
}
3746
context.report(list[component].node, MULTI_COMP_MESSAGE);
3847
}
39-
},
40-
41-
ReturnStatement: function(node) {
42-
if (!componentUtil.isReactComponent(context, node)) {
43-
return;
44-
}
45-
componentList.set(context, node);
4648
}
4749
};
4850
};

lib/rules/prop-types.js

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ module.exports = function(context) {
9090
function mustBeValidated(component) {
9191
return Boolean(
9292
component &&
93-
component.isReactComponent &&
9493
component.usedPropTypes &&
9594
!component.ignorePropsValidation
9695
);
@@ -519,6 +518,10 @@ module.exports = function(context) {
519518

520519
return {
521520

521+
ClassDeclaration: function(node) {
522+
componentList.set(context, node);
523+
},
524+
522525
ClassProperty: function(node) {
523526
if (!isPropTypesDeclaration(node)) {
524527
return;
@@ -576,19 +579,14 @@ module.exports = function(context) {
576579
},
577580

578581
ObjectExpression: function(node) {
582+
componentList.set(context, node);
579583
// Search for the proptypes declaration
580584
node.properties.forEach(function(property) {
581585
if (!isPropTypesDeclaration(property.key)) {
582586
return;
583587
}
584588
markPropTypesAsDeclared(node, property.value);
585589
});
586-
587-
if (componentUtil.isComponentDefinition(node)) {
588-
componentList.set(context, node, {
589-
isReactComponent: true
590-
});
591-
}
592590
},
593591

594592
'Program:exit': function() {
@@ -600,15 +598,6 @@ module.exports = function(context) {
600598
}
601599
reportUndeclaredPropTypes(list[component]);
602600
}
603-
},
604-
605-
ReturnStatement: function(node) {
606-
if (!componentUtil.isReactComponent(context, node)) {
607-
return;
608-
}
609-
componentList.set(context, node, {
610-
isReactComponent: true
611-
});
612601
}
613602
};
614603

lib/rules/sort-comp.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ module.exports = function(context) {
8585
function mustBeValidated(component) {
8686
return (
8787
component &&
88-
component.isReactComponent &&
8988
!component.hasDisplayName
9089
);
9190
}
@@ -346,9 +345,17 @@ module.exports = function(context) {
346345
}
347346

348347
return {
348+
349+
ClassDeclaration: function(node) {
350+
componentList.set(context, node);
351+
},
352+
353+
ObjectExpression: function(node) {
354+
componentList.set(context, node);
355+
},
356+
349357
'Program:exit': function() {
350358
var list = componentList.getList();
351-
352359
for (var component in list) {
353360
if (!list.hasOwnProperty(component) || !mustBeValidated(list[component])) {
354361
continue;
@@ -358,15 +365,6 @@ module.exports = function(context) {
358365
}
359366

360367
reportErrors();
361-
},
362-
363-
ReturnStatement: function(node) {
364-
if (!componentUtil.isReactComponent(context, node)) {
365-
return;
366-
}
367-
componentList.set(context, node, {
368-
isReactComponent: true
369-
});
370368
}
371369
};
372370

0 commit comments

Comments
 (0)