Skip to content

Commit b580375

Browse files
committed
refactoring stateless check function(s)
1 parent 0a50807 commit b580375

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/isStatelessComponent.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1+
function isJSXElementOrReactCreateElement(node) {
2+
if (node.type === 'JSXElement') {
3+
return true;
4+
}
5+
if (node.callee && node.callee.object && node.callee.object.name === 'React' && node.callee.property && node.callee.property.name === 'createElement') {
6+
return true;
7+
}
8+
9+
if (node.callee && node.callee.name === 'createElement') {
10+
return true;
11+
}
12+
return false;
13+
}
14+
115
function isReturningJSXElement(path) {
216
/**
317
* Early exit for ArrowFunctionExpressions, there is no ReturnStatement node.
418
*/
5-
if (path.node.init && path.node.init.body && path.node.init.body.type === 'JSXElement') {
19+
if (path.node.init && path.node.init.body && isJSXElementOrReactCreateElement(path.node.init.body)) {
620
return true;
721
}
822

@@ -17,16 +31,14 @@ function isReturningJSXElement(path) {
1731

1832
const argument = path2.get('argument');
1933

20-
if (argument.node.type === 'JSXElement') {
34+
if (isJSXElementOrReactCreateElement(argument.node)) {
2135
visited = true;
22-
} else if (argument.node.type === 'CallExpression') {
23-
24-
const { node } = argument.get('callee');
25-
if (node.name === 'createElement' || node.object.name === 'React' && node.property.name === 'createElement') {
26-
visited = true;
27-
}
36+
return;
37+
}
2838

29-
const binding = path.scope.getBinding(node.name);
39+
if (argument.node.type === 'CallExpression') {
40+
const name = argument.get('callee').node.name;
41+
const binding = path.scope.getBinding(name);
3042

3143
if (!binding) {
3244
return;
@@ -36,7 +48,7 @@ function isReturningJSXElement(path) {
3648
visited = true;
3749
}
3850
}
39-
}
51+
},
4052
});
4153

4254
return visited;

0 commit comments

Comments
 (0)