forked from emotion-js/emotion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.js
45 lines (42 loc) · 1.42 KB
/
transform.js
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
// Press ctrl+space for code completion
export default function transformer(file, api) {
const j = api.jscodeshift
let reactEmotionImportSource = 'react-emotion'
let emotionImportSource = 'emotion'
// change this to @emotion/styled.macro if you want to use that
let newStyledImportSource = '@emotion/styled.macro'
let quote = 'single'
return j(file.source)
.find(j.ImportDeclaration, {
source: { value: reactEmotionImportSource }
})
.forEach(path => {
if (path.value.source.raw.charAt(0) === '"') {
quote = 'double'
}
if (
path.value.specifiers.length === 1 &&
path.value.specifiers[0].type === 'ImportDefaultSpecifier'
) {
path.value.source.value = newStyledImportSource
} else {
let defaultImportSpecifierIndex = path.value.specifiers.findIndex(
val => val.type === 'ImportDefaultSpecifier'
)
path.value.source.value = emotionImportSource
if (defaultImportSpecifierIndex !== -1) {
let index = path.parentPath.value.indexOf(path.value)
path.parentPath.value.splice(
index,
0,
j.importDeclaration(
[path.value.specifiers[defaultImportSpecifierIndex]],
j.literal(newStyledImportSource)
)
)
path.value.specifiers.splice(defaultImportSpecifierIndex, 1)
}
}
})
.toSource({ quote })
}