-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·184 lines (172 loc) · 4.97 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
180
181
182
183
184
#!/usr/bin/env node
const fs = require('fs');
const babel = require('@babel/core');
const t = require('@babel/types');
const prettier = require('prettier');
const yargs = require('yargs');
const path = require('path');
const chalk = require('chalk');
const findUp = require('find-up');
const argv = yargs
.option('filename', {
alias: 'f',
type: 'string',
description: 'want to transform filename'
})
.option('prettirerc', {
alias: 'p',
type: 'string',
description: 'prettirerc file path'
}).argv;
if (!argv.filename) {
console.log(chalk.red('error: use --filename or -f pass file path'));
process.exit(1);
}
const filename = path.resolve(process.cwd(), argv.filename);
const prettirepath =
argv.prettirerc ||
findUp.sync(['.prettierrc', '.prettierrc.js', 'prettier.config.js'], {
cwd: filename
});
const get = (object, path) => {
if (typeof path === 'string')
path = path.split('.').filter(key => key.length);
return path.reduce((dive, key) => dive && dive[key], object);
};
function CallExpression(path, state) {
const callee = path.node.callee;
if (get(callee, 'callee.name') !== 'getFieldDecorator') {
return;
}
const args = callee.arguments;
const fieldName = args[0];
const options = args[1];
const children = path.node.arguments[0];
const formItemProps = state.formAttributes || [
t.jsxAttribute(t.jsxIdentifier('noStyle'))
];
formItemProps.push(
t.jsxAttribute(
t.jsxIdentifier('name'),
t.isStringLiteral(fieldName)
? fieldName
: t.jsxExpressionContainer(fieldName)
)
);
if (Array.isArray(options && options.properties)) {
options.properties.forEach(prop => {
if (prop.key.name === 'initialValue' && state.wrapperFormAttr) {
const attrIndex = state.wrapperFormAttr.findIndex(att => {
if (att.name.name === 'initialValue') {
return true;
}
});
if (attrIndex !== -1) {
state.wrapperFormAttr[attrIndex].value.expression.properties.push(
t.objectProperty(fieldName, prop.value)
);
} else {
state.wrapperFormAttr.push(
t.jsxAttribute(
t.jsxIdentifier('initialValue'),
t.jsxExpressionContainer(
t.objectExpression([t.objectProperty(fieldName, prop.value)])
)
)
);
}
return;
}
let propValue = prop.value;
if (t.isStringLiteral(propValue)) {
propValue = prop.value;
} else if (
t.isLiteral(propValue) ||
t.isExpression(propValue) ||
t.isTemplateLiteral(propValue)
) {
propValue = t.jsxExpressionContainer(propValue);
}
const p = t.jsxAttribute(t.jsxIdentifier(prop.key.name), propValue);
formItemProps.push(p);
});
}
const formOpen = t.jsxOpeningElement(
t.jsxIdentifier('Form.Item'),
formItemProps,
false
);
const formClose = t.jsxClosingElement(t.jsxIdentifier('Form.Item'));
const FormItem = t.jsxElement(formOpen, formClose, [children], false);
path.replaceWith(FormItem);
path.stop();
}
function JSXExpressionContainer(path, state) {
if (
get(path, 'node.expression.callee') &&
get(path, 'node.expression.callee.callee.name') === 'getFieldDecorator'
) {
path.traverse({ CallExpression }, state);
path.replaceWith(path.node.expression);
}
}
function FormJSXElement(path, state) {
path.traverse({ JSXElement, CallExpression, JSXExpressionContainer }, state);
}
function JSXElement(path, state) {
const openingElement = get(path, 'node.openingElement');
const jsxMemberExp = get(openingElement, 'name');
if (
get(jsxMemberExp, 'object.name') === 'Form' &&
get(jsxMemberExp, 'property.name') === 'Item'
) {
path.traverse(
{ JSXExpressionContainer },
{ ...state, formAttributes: openingElement.attributes }
);
path.replaceWithMultiple(path.node.children);
} else if (get(jsxMemberExp, 'name') === 'Form') {
FormJSXElement(path, {
...state,
wrapperFormAttr: path.node.openingElement.attributes
});
}
}
const output = babel.transformFileSync(filename, {
babelrc: false,
plugins: [
'@babel/plugin-syntax-class-properties',
'@babel/plugin-syntax-jsx',
function myCustomPlugin() {
return {
visitor: {
JSXExpressionContainer,
CallExpression,
JSXElement
}
};
}
]
});
if (prettirepath) {
const prettierrc = fs.readFileSync(prettirepath, 'utf8');
prettier.resolveConfig(prettierrc).then(options => {
fs.writeFile(filename, prettier.format(output.code, options), err => {
if (err) {
chalk.red('migration fail');
console.log(err);
} else {
chalk.green('migration successful');
}
});
});
} else {
fs.writeFile(filename, prettier.format(output.code), err => {
if (err) {
chalk.red('migration fail');
console.log(err);
} else {
chalk.green('migration successful');
}
});
}