forked from gluestack/gluestack-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testcodeshift.js
300 lines (285 loc) · 7.71 KB
/
testcodeshift.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
function removeVariant(originalObject) {
const newObject = {};
for (const key in originalObject) {
if (key === 'variants') {
if (!newObject.variants) {
newObject.variants = {
variant: originalObject.variants,
};
} else {
newObject.variants.variant = originalObject.variants;
}
// newObject.variant = removeVariant(originalObject[key]);
} else if (key === 'sizes') {
if (!newObject.variants) {
newObject.variants = {
sizes: originalObject.sizes,
};
} else {
newObject.variants.size = originalObject.sizes;
}
// newObject.variant = removeVariant(originalObject[key]);
} else {
newObject[key] = originalObject[key];
}
}
return newObject;
}
function removeBaseStyle(originalObject) {
const newObject = {};
for (const key in originalObject) {
if (key === 'baseStyle') {
const style = { ...originalObject[key].style };
delete originalObject[key].style;
Object.assign(newObject, { ...style, ...originalObject[key] });
} else {
newObject[key] = originalObject[key];
}
}
return newObject;
}
function removeStyle(originalObject) {
const newObject = {};
for (const key in originalObject) {
if (originalObject.hasOwnProperty(key)) {
if (
key === 'style' ||
key === 'state' ||
key === 'descendants' ||
key === 'platform' ||
key === 'colorMode'
) {
Object.assign(newObject, originalObject[key]);
Object.assign(newObject, removeStyle(newObject));
// newObject = removeStyle(newObject);
} else if (typeof originalObject[key] === 'object') {
newObject[key] = removeStyle(originalObject[key]);
} else {
newObject[key] = originalObject[key];
}
}
}
return newObject;
}
function renamePseudoClasses(obj) {
const renameMap = {
indeterminate: ':indeterminate',
checked: ':checked',
readOnly: ':readOnly',
required: ':required',
invalid: ':invalid',
focus: ':focus',
focusVisible: ':focusVisible',
hover: ':hover',
pressed: ':pressed',
active: ':active',
loading: ':loading',
disabled: ':disabled',
web: '_web',
android: '_android',
ios: '_ios',
light: '_light',
dark: '_dark',
};
for (const key in obj) {
if (renameMap[key]) {
obj[renameMap[key]] = obj[key];
delete obj[key];
Object.assign(obj, renamePseudoClasses(obj));
} else if (typeof obj[key] === 'object') {
renamePseudoClasses(obj[key]);
}
}
return obj;
}
var newObj = {
baseStyle: {
style: {
// @ts-ignore
fontSize: 'vvvv',
color: `$text50`,
borderColor: '$blue500',
bg: '$amber500',
w: 100,
h: 100,
},
state: {
hover: {
style: {
fontSize: 'sm',
},
state: {
active: {
style: {
fontSize: 'sm',
},
platform: {
web: {
style: {
color: 'red',
},
},
},
},
},
},
active: {
style: {
fontSize: 'sm',
},
},
},
},
variants: {
solid: {
style: {
color: `$text50`,
},
descendants: {
_text: {
style: {
color: 'red',
},
},
},
},
},
sizes: {
solid: {
style: {
color: `$text50`,
},
},
},
};
// const output1 = renamePseudoClasses(newObj);
const inputObj = newObj;
const outputcc = removeVariant(inputObj);
const output2 = removeBaseStyle(outputcc);
const output3 = removeStyle(output2);
const output1 = renamePseudoClasses(output3);
// console.log(process.argv[2]);
// console.log(JSON.stringify(output1, null, 2));
// jscodeshift can take a parser, like "babel", "babylon", "flow", "ts", or "tsx"
// Read more: https://github.com/facebook/jscodeshift#parser
export const parser = 'tsx';
const babel = require('@babel/parser');
const generate = require('@babel/generator').default;
const babelPresetTypeScript = require('@babel/preset-typescript');
const traverse = require('@babel/traverse').default;
const types = require('@babel/types');
function addQuotesToObjectKeys(code) {
const ast = babel.parse(`var a = ${code}`, {
presets: [babelPresetTypeScript],
sourceType: 'module',
});
traverse(ast, {
ObjectProperty: (path) => {
if (types.isTemplateLiteral(path.node.value)) {
path.node.value = types.stringLiteral(
path.node.value.quasis[0].value.raw
);
}
if (types.isIdentifier(path.node.key)) {
path.node.key = types.stringLiteral(path.node.key.name);
}
if (types.isNumericLiteral(path.node.key)) {
path.node.key = types.stringLiteral(path.node.key.extra.raw);
}
if (types.isStringLiteral(path.node.value)) {
path.node.value = types.stringLiteral(path.node.value.value);
}
},
});
let initAst;
traverse(ast, {
VariableDeclarator: (path) => {
initAst = path.node.init;
},
});
const { code: output } = generate(initAst, {
sourceType: 'module',
presets: [babelPresetTypeScript],
});
return output;
}
function replaceSingleQuotes(str) {
let inDoubleQuotes = false;
let newStr = '';
for (let i = 0; i < str.length; i++) {
if (str[i] === '"') {
inDoubleQuotes = !inDoubleQuotes;
}
if (str[i] === "'" && !inDoubleQuotes) {
newStr += '"';
} else {
newStr += str[i];
}
}
return newStr;
}
function generateObjectAst(obj) {
let properties = Object.entries(obj).map(([key, value]) => {
if (typeof value === 'object' && !Array.isArray(value)) {
return types.objectProperty(
types.stringLiteral(key),
generateObjectAst(value)
);
} else if (typeof value === 'object' && Array.isArray(value)) {
let elements = value.map((obj) => {
if (typeof obj === 'string') {
return types.stringLiteral(obj);
} else {
return generateObjectAst(obj);
}
});
return types.objectProperty(
types.stringLiteral(key),
types.arrayExpression(elements)
);
} else {
return types.objectProperty(
types.stringLiteral(key),
typeof value === 'number'
? types.numericLiteral(value)
: types.stringLiteral(value)
);
}
});
return types.objectExpression(properties);
}
// Press ctrl+space for code completion
export default function transformer(file, api) {
const j = api.jscodeshift;
function getObjectFromAstNode(node) {
let objectCode = generate(node).code;
objectCode = addQuotesToObjectKeys(
objectCode.replace(
/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g,
(m, g) => (g ? '' : m)
)
);
// Checking for single quotes and replacing it with " while keeping in mind to not replace single quotes inside double quotes
objectCode = replaceSingleQuotes(objectCode);
// console.log(objectCode);
return JSON.parse(objectCode);
}
return j(file.source)
.find(j.CallExpression)
.forEach((path) => {
if (path.node.callee.name === 'styled') {
let argument = path.node.arguments;
let arg1 = argument[1];
let inputObj = getObjectFromAstNode(arg1);
const outputcc = removeVariant(inputObj);
const output2 = removeBaseStyle(outputcc);
const output3 = removeStyle(output2);
const output1 = renamePseudoClasses(output3);
let outputAst = generateObjectAst(output1);
// console.log(outputAst);
path.node.arguments[1] = outputAst;
}
})
.toSource();
}
// jscodeshift -t ./transformer.js ./testcodeshift.js