forked from TejasQ/babel-plugin-glamorous-to-emotion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
179 lines (166 loc) · 5.47 KB
/
index.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* Glamorous to Emotion codemod
*
* This babel plugin should migrate any existing codebase
* using React or Preact and glamorous to one using
* emotion (emotion.sh).
*
* It follows the glamorous to emotion migration guide
* found at https://github.com/paypal/glamorous/blob/master/other/EMOTION_MIGRATION.md
*
* You can use it as a babel plugin by adding it to your .babelrc
* under "plugins", or use it as a one-off codemod by using the
* babel cli:
*
* babel [your-source-dir] --plugins=glamorous-to-emotion --presets=react,etc... --out-dir=[your-source-dir]
*
* A demo can be seen at:
* https://astexplorer.net/#/gist/7bc4771564a12c9f93c4904b3934aa1c/latests
*/
module.exports = function(babel) {
const {types: t} = babel;
const fixContentProp = args => {
return args.map(argument => {
if (argument.type !== "ObjectExpression") {
return argument;
}
return {
...argument,
properties: argument.properties.map(prop => {
if (prop.key.name !== "content") {
return prop;
}
// Add quotes to the content property.
return {
...prop,
value: t.stringLiteral(`"${prop.value.value}"`),
};
}),
};
});
};
const transformJSXAttributes = attrs => {
if (!attrs) return [];
const appendToCss = [];
let cssAttr = null;
const newAttrs = attrs.filter(attr => {
const {value, name, type} = attr;
if (type === "JSXSpreadAttribute") return true;
if (name.name === "css") {
cssAttr = attr;
} else {
// event handlers are no CSS Props!
if (name.name.indexOf("on") === 0) return true;
appendToCss.push({
name: t.identifier(name.name),
value: value.type === "JSXExpressionContainer" ? value.expression : value,
});
return false;
}
return true;
});
if (appendToCss.length > 0) {
if (!cssAttr) {
cssAttr = t.jsxAttribute(
t.jsxIdentifier("css"),
t.jsxExpressionContainer(t.objectExpression([]))
);
newAttrs.push(cssAttr);
} else if (cssAttr.value.expression.type !== "ObjectExpression") {
// turn <span css={obj} .../> into <span css={{...obj}} .../>
cssAttr.value.expression = t.objectExpression([t.spreadElement(cssAttr.value.expression)]);
}
appendToCss.forEach(({name, value}) => {
cssAttr.value.expression.properties.push(t.objectProperty(name, value));
});
cssAttr.value.expression.properties;
}
return newAttrs;
};
const styledVisitor = {
ReferencedIdentifier(path, {getNewName, oldName}) {
if (path.node.name !== oldName) return;
switch (path.parent.type) {
case "CallExpression": {
path.node.name = getNewName();
break;
}
case "MemberExpression": {
const grandParentPath = path.parentPath.parentPath;
if (grandParentPath.node.type === "CallExpression") {
grandParentPath.replaceWith(
t.callExpression(
t.callExpression(t.identifier(getNewName()), [
t.stringLiteral(grandParentPath.node.callee.property.name),
]),
fixContentProp(grandParentPath.node.arguments)
)
);
} else {
throw new Error(
`Not sure how to deal with glamorous within MemberExpression @ ${path.node.loc}`
);
}
break;
}
case "JSXMemberExpression": {
const grandParent = path.parentPath.parent;
grandParent.name = t.identifier(grandParent.name.property.name.toLowerCase());
if (grandParent.type === "JSXOpeningElement") {
grandParent.attributes = transformJSXAttributes(grandParent.attributes);
}
break;
}
default: {
console.log("default", path.parent.type);
}
}
},
};
return {
name: "glamorousToEmotion",
visitor: {
ImportDeclaration(path, {opts}) {
if (
path.node.source.value !== "glamorous" &&
path.node.source.value !== "glamorous.macro"
) {
return;
}
const newName = path.scope.hasBinding("styled")
? path.scope.generateUidIdentifier("styled").name
: "styled";
let newImports = [];
let useDefaultImport = false;
const getNewName = () => {
if (!useDefaultImport) {
newImports.push(
t.importDeclaration(
[t.importDefaultSpecifier(t.identifier(newName))],
t.stringLiteral(opts.preact ? "preact-emotion" : "react-emotion")
)
);
useDefaultImport = true;
}
return newName;
};
path.node.specifiers.filter(s => s.type === "ImportDefaultSpecifier").forEach(s => {
path.parentPath.traverse(styledVisitor, {getNewName, oldName: s.local.name});
});
const themeProvider = path.node.specifiers.find(
specifier => specifier.local.name === "ThemeProvider"
);
if (themeProvider) {
newImports.push(
t.importDeclaration(
[t.importSpecifier(t.identifier("ThemeProvider"), t.identifier("ThemeProvider"))],
t.stringLiteral("emotion-theming")
)
);
}
newImports.forEach(ni => path.insertBefore(ni));
path.remove();
},
},
};
};