-
-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathisFunctionComponent.js
More file actions
136 lines (115 loc) · 3.54 KB
/
isFunctionComponent.js
File metadata and controls
136 lines (115 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* Adapted from https://github.com/oliviertassinari/babel-plugin-transform-react-remove-prop-types/blob/master/src/isStatelessComponent.js
*/
import { isStyled } from '../utils/detectors'
const traversed = Symbol('traversed')
function isJSXElementOrReactCreateElement(
path,
filterFn = null // optional filter function to match only certain kinds of React elements
) {
let visited = false
path.traverse({
CallExpression(path2) {
const callee = path2.get('callee')
if (
callee.matchesPattern('React.createElement') ||
callee.matchesPattern('React.cloneElement') ||
callee.node.name === 'cloneElement'
) {
if (visited) {
return
}
visited = filterFn ? filterFn(path2) : true
}
},
JSXElement(path2) {
if (visited) {
return
}
visited = filterFn ? filterFn(path2) : true
},
})
return visited
}
function isReturningJSXElement(path, state, filterFn = null, iteration = 0) {
// Early exit for ArrowFunctionExpressions, there is no ReturnStatement node.
if (
path.node.init &&
path.node.init.body &&
isJSXElementOrReactCreateElement(path, filterFn)
) {
return true
}
if (iteration > 20) {
throw Error('babel-plugin-styled-components: infinite loop detected.')
}
let visited = false
path.traverse({
ReturnStatement(path2) {
// We have already found what we are looking for.
if (visited) {
return
}
const argument = path2.get('argument')
// Nothing is returned
if (!argument.node) {
return
}
if (isJSXElementOrReactCreateElement(path2, filterFn)) {
visited = true
return
}
if (argument.node.type === 'CallExpression') {
const name = argument.get('callee').node.name
const binding = path.scope.getBinding(name)
if (!binding) {
return
}
// Prevents infinite traverse loop.
if (binding.path[traversed]) {
return
}
binding.path[traversed] = true
if (
isReturningJSXElement(binding.path, state, filterFn, iteration + 1)
) {
visited = true
}
}
},
})
return visited
}
/**
* IMPORTANT: This function assumes that the given path is a VariableDeclarator or FunctionDeclaration,
* and will return false positives otherwise. If a more robust version is needed in the future,
* see https://github.com/oliviertassinari/babel-plugin-transform-react-remove-prop-types/blob/master/src/isStatelessComponent.js
*
* Returns true if the given path is a React function component definition
* @param {Path<VariableDeclarator | FunctionDeclaration>} path
*/
export function isFunctionComponent(
path,
state,
types,
mustConsumeStyledComponent = false // only return true if the component might render a styled component
) {
let filterFn
if (mustConsumeStyledComponent) {
filterFn = reactElementPath => {
// look up the component and check if it's a styled component
const componentId = reactElementPath.isJSXElement()
? reactElementPath.node.openingElement.name
: reactElementPath.node.arguments[0]
const binding = reactElementPath.scope.getBinding(componentId.name)
if (binding && binding.path.isVariableDeclarator()) {
const { init } = binding.path.node
if (isStyled(types)(init.callee, state, true /* includeIIFE */)) {
return true
}
}
return false
}
}
return isReturningJSXElement(path, state, filterFn)
}