@@ -36,29 +36,42 @@ function isReactComponent(context, node) {
36
36
return Boolean ( isEmptyComponentRender || isComponentRender ) ;
37
37
}
38
38
39
+ /**
40
+ * Detect if the node is a component definition
41
+ * @param {ASTNode } node The AST node being checked.
42
+ * @returns {Boolean } True the node is a component definition, false if not.
43
+ */
44
+ function isComponentDefinition ( node ) {
45
+ var isES6Component = node . type === 'ClassDeclaration' ;
46
+ var isES5Component = Boolean (
47
+ node . type === 'ObjectExpression' &&
48
+ node . parent &&
49
+ node . parent . callee &&
50
+ node . parent . callee . object &&
51
+ node . parent . callee . property &&
52
+ node . parent . callee . object . name === 'React' &&
53
+ node . parent . callee . property . name === 'createClass'
54
+ ) ;
55
+ return isES5Component || isES6Component ;
56
+ }
57
+
39
58
/**
40
59
* Get the React component ASTNode from any child ASTNode
41
60
* @param {Object } context The current rule context.
42
61
* @param {ASTNode } node The AST node being checked.
43
62
* @returns {ASTNode } The ASTNode of the React component.
44
63
*/
45
64
function getNode ( context , node ) {
46
- var componentNode ;
47
-
48
- if ( node . type === 'ClassDeclaration' || node . type === 'ObjectExpression' ) {
49
- componentNode = node ;
50
- } else {
51
- var ancestors = context . getAncestors ( ) . reverse ( ) ;
52
- for ( var i = 0 , j = ancestors . length ; i < j ; i ++ ) {
53
- if ( ancestors [ i ] . type === 'ClassDeclaration' || ancestors [ i ] . type === 'ObjectExpression' ) {
54
- componentNode = ancestors [ i ] ;
55
- break ;
56
- }
57
- }
58
- }
65
+ var componentNode = null ;
66
+ var ancestors = context . getAncestors ( ) . reverse ( ) ;
59
67
60
- if ( ! componentNode ) {
61
- return null ;
68
+ ancestors . unshift ( node ) ;
69
+
70
+ for ( var i = 0 , j = ancestors . length ; i < j ; i ++ ) {
71
+ if ( isComponentDefinition ( ancestors [ i ] ) ) {
72
+ componentNode = ancestors [ i ] ;
73
+ break ;
74
+ }
62
75
}
63
76
64
77
return componentNode ;
@@ -188,6 +201,7 @@ module.exports = {
188
201
DEFAULT_COMPONENT_NAME : DEFAULT_COMPONENT_NAME ,
189
202
isReactComponent : isReactComponent ,
190
203
getNode : getNode ,
204
+ isComponentDefinition : isComponentDefinition ,
191
205
getIdentifiers : getIdentifiers ,
192
206
List : List
193
207
} ;
0 commit comments