-
Notifications
You must be signed in to change notification settings - Fork 1
/
extend-theme-transformer-v3.js
181 lines (160 loc) · 4.21 KB
/
extend-theme-transformer-v3.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
180
181
const path = require("path");
const fs = require("fs");
const { setDirtyFile } = require("./utils");
const oldThemeObj = {
lineHeights: {
none: "1",
shorter: "1.25",
short: "1.375",
base: "1.5",
tall: "1.625",
taller: "2",
3: "12px",
4: "16px",
5: "20px",
6: "24px",
7: "28px",
8: "32px",
9: "36px",
10: "40px",
},
letterSpacings: {
xxs: "-1.5",
xs: "-0.5",
sm: "0",
md: "0.1",
lg: "0.15",
xl: "0.25",
"2xl": "0.4",
"3xl": "0.5",
"4xl": "1.25",
"5xl": "1.5",
},
fontSizes: {
xxs: "10",
},
radii: {
sm: "2",
md: "4",
lg: "6",
xl: "8",
pill: "25",
},
borderWidth: {
none: "0",
},
};
const oldTheme = JSON.stringify(oldThemeObj, null, 2);
// exit
export default (fileInfo, api) => {
const j = api.jscodeshift;
const root = j(fileInfo.source);
let extendThemeImport = false;
modifyExtendTheme();
modifyProvider();
function modifyExtendTheme() {
// extendTheme import
let localNameExtendTheme = "extendTheme";
root
.find(j.ImportDeclaration)
.filter(
(impDecNodePath) => impDecNodePath.value.source.value === "native-base"
)
.forEach((impDecNodePathFiltered) => {
j(impDecNodePathFiltered)
// find ImportSpecifier here instead of Identifier
.find(j.ImportSpecifier)
.forEach((impSpecNodePath) => {
if (impSpecNodePath.node.imported.name === "extendTheme") {
extendThemeImport = true;
if (impSpecNodePath.node.local.name) {
localNameExtendTheme = impSpecNodePath.node.local.name;
}
}
});
});
// if extend theme import
if (extendThemeImport) {
const callExpression = root.find(j.CallExpression, {
callee: {
type: "Identifier",
name: localNameExtendTheme,
},
});
const callExpressionLength = callExpression.length;
if (callExpressionLength) {
// const output = callExpression.find(j.ObjectExpression);
// .forEach((node) => {
// console.log(node);
// // node.insertBefore(`${oldTheme}`);
// });
callExpression.find(j.ObjectExpression).filter((node, index) => {
if (
node.parent.value.type === "CallExpression" &&
node.parent.value.callee.name === localNameExtendTheme &&
index === 0
) {
node.insertBefore(`${oldTheme}`);
setDirtyFile(fileInfo);
}
});
}
}
}
function modifyProvider() {
const nativeBaseProviderJSX = root.find(j.JSXOpeningElement, {
name: {
type: "JSXIdentifier",
name: "NativeBaseProvider",
},
});
if (nativeBaseProviderJSX.length === 0) {
return;
}
const themeAttribute = nativeBaseProviderJSX.find(j.JSXAttribute, {
name: { name: "theme" },
});
if (themeAttribute.length === 0) {
//import extend theme
addImportExtendTheme();
//
const imports = root.find(j.ImportDeclaration);
const lastImport = imports.at(imports.length - 1).get();
lastImport.insertAfter(`const theme_v3 = extendTheme(${oldTheme});
`);
nativeBaseProviderJSX.replaceWith(
`<NativeBaseProvider theme={theme_v3}>`
);
setDirtyFile(fileInfo);
} else {
// if empty theme
// const themeAttrNode = themeAttribute.nodes()[0];
// if (
// themeAttrNode &&
// themeAttrNode.value.type === "JSXExpressionContainer"
// ) {
// if (themeAttrNode.value.expression?.type === "Identifier") {
// // no need to change anything
// } else {
// }
// }
// themeAttribute.replaceWith(
// `theme={extendTheme(${oldTheme})}>`
// );
}
// // and not theme
// nativeBaseProviderAttribute.insertBefore(
// `theme={extendTheme(${oldTheme})}`
// );
// }
}
function addImportExtendTheme() {
if (!extendThemeImport) {
root
.find(j.ImportDeclaration)
.get()
.insertBefore('import { extendTheme } from "native-base";');
}
}
return root.toSource();
};