-
-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathdetectors.js
More file actions
144 lines (122 loc) · 4.36 KB
/
detectors.js
File metadata and controls
144 lines (122 loc) · 4.36 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
137
138
139
140
141
142
143
144
const VALID_TOP_LEVEL_IMPORT_PATHS = [
'styled-components',
'styled-components/no-tags',
'styled-components/native',
'styled-components/primitives',
]
const CACHE_MISS = Symbol('CACHE_MISS');
export const isValidTopLevelImport = x =>
VALID_TOP_LEVEL_IMPORT_PATHS.includes(x)
const localNameCache = {}
export const importLocalName = (name, state, bypassCache = false) => {
const cacheKey = name + state.file.opts.filename
if (!bypassCache && cacheKey in localNameCache && localNameCache[cacheKey] !== CACHE_MISS) {
return localNameCache[cacheKey]
}
let localName = state.styledRequired
? name === 'default'
? 'styled'
: name
: CACHE_MISS
state.file.path.traverse({
ImportDeclaration: {
exit(path) {
const { node } = path
if (isValidTopLevelImport(node.source.value)) {
for (const specifier of path.get('specifiers')) {
if (specifier.isImportDefaultSpecifier()) {
localName = specifier.node.local.name
}
if (
specifier.isImportSpecifier() &&
specifier.node.imported.name === name
) {
localName = specifier.node.local.name
}
if (specifier.isImportNamespaceSpecifier()) {
localName = specifier.node.local.name
}
}
}
},
},
})
localNameCache[cacheKey] = localName
return localName
}
// cache styled tags that we've already found from previous calls to isStyled()
const visitedStyledTags = new WeakSet()
export const isStyled = t => (tag, state, includeIIFE = false) => {
if (includeIIFE) {
// check to see if this is an IIFE wrapper created by pureWrapStaticProps()
// that replaced what was originally a `styled` call
if (t.isArrowFunctionExpression(tag) && tag.body && tag.body.body[0]) {
const statement = tag.body.body[0]
if (t.isVariableDeclaration(statement)) {
const callee = statement.declarations[0].init.callee
if (callee && isStyled(t)(callee, state)) {
return true
}
}
}
}
if (
t.isCallExpression(tag) &&
t.isMemberExpression(tag.callee) &&
tag.callee.property.name !== 'default' /** ignore default for #93 below */
) {
// styled.something()
return isStyled(t)(tag.callee.object, state)
}
if (visitedStyledTags.has(tag)) {
return true
}
const ret = Boolean(
(t.isMemberExpression(tag) &&
tag.object.name === importLocalName('default', state)) ||
(t.isCallExpression(tag) &&
tag.callee.name === importLocalName('default', state)) ||
/**
* #93 Support require()
* styled-components might be imported using a require()
* call and assigned to a variable of any name.
* - styled.default.div``
* - styled.default.something()
*/
(state.styledRequired &&
t.isMemberExpression(tag) &&
t.isMemberExpression(tag.object) &&
tag.object.property.name === 'default' &&
tag.object.object.name === state.styledRequired) ||
(state.styledRequired &&
t.isCallExpression(tag) &&
t.isMemberExpression(tag.callee) &&
tag.callee.property.name === 'default' &&
tag.callee.object.name === state.styledRequired)
)
if (ret) {
visitedStyledTags.add(tag)
}
return ret
}
export const isCSSHelper = t => (tag, state) =>
t.isIdentifier(tag) && tag.name === importLocalName('css', state)
export const isCreateGlobalStyleHelper = t => (tag, state) =>
t.isIdentifier(tag) &&
tag.name === importLocalName('createGlobalStyle', state)
export const isInjectGlobalHelper = t => (tag, state) =>
t.isIdentifier(tag) && tag.name === importLocalName('injectGlobal', state)
export const isKeyframesHelper = t => (tag, state) =>
t.isIdentifier(tag) && tag.name === importLocalName('keyframes', state)
export const isWithThemeHelper = t => (tag, state) =>
t.isIdentifier(tag) && tag.name === importLocalName('withTheme', state)
export const isHelper = t => (tag, state) =>
isCSSHelper(t)(tag, state) ||
isKeyframesHelper(t)(tag, state) ||
isWithThemeHelper(t)(tag, state)
export const isPureHelper = t => (tag, state) =>
isCSSHelper(t)(tag, state) ||
isKeyframesHelper(t)(tag, state) ||
isCreateGlobalStyleHelper(t)(tag, state) ||
isWithThemeHelper(t)(tag, state)
export { isFunctionComponent } from './isFunctionComponent'