-
Notifications
You must be signed in to change notification settings - Fork 1
/
replacer.js
33 lines (30 loc) · 1.03 KB
/
replacer.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
const j = require('jscodeshift');
module.exports = (source) => {
let findPath = arrowFunctionExpression(source);
if (findPath.__paths.length === 0) {
findPath = functionExpression(source);
}
return findPath
// eslint-disable-next-line array-callback-return
.filter(p => {
const root = p.value;
for (const param of root.params) {
return param.type !== 'ObjectPattern' && root.body.type === 'BlockStatement' && !root.body.left;
}
})
.replaceWith(p => {
const root = p.value;
root.params.forEach(param => {
if (param.typeAnnotation) {
delete param.typeAnnotation;
}
});
const firstParam = root.params[0];
const lastParam = root.params[root.params.length - 1];
firstParam.name = `{ ${firstParam.name}`;
lastParam.name = `${lastParam.name} }`;
return root;
});
};
const arrowFunctionExpression = (source) => source.find(j.ArrowFunctionExpression);
const functionExpression = (source) => source.find(j.FunctionExpression);