-
Notifications
You must be signed in to change notification settings - Fork 109
/
rm-object-assign.js
37 lines (33 loc) · 1.21 KB
/
rm-object-assign.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
module.exports = (file, api, options) => {
const j = api.jscodeshift;
const printOptions = options.printOptions || { quote: 'single' };
const root = j(file.source);
const rmObjectAssignCall = path =>
j(path).replaceWith(
j.objectExpression(
path.value.arguments.reduce(
(allProperties, { comments, ...argument }) => {
if (argument.type === 'ObjectExpression') {
const { properties } = argument;
// Copy comments.
if (properties.length > 0 && comments && comments.length > 0) {
properties[0].comments = [
...(properties[0].comments || []),
...(comments || [])
];
}
return [...allProperties, ...properties];
}
return [...allProperties, { ...j.spreadProperty(argument), comments }];
}, [])
)
);
root
.find(j.CallExpression, {
callee: { object: { name: 'Object' }, property: { name: 'assign' } },
arguments: [{ type: 'ObjectExpression' }]
})
.filter(p => !p.value.arguments.some(a => a.type === 'SpreadElement'))
.forEach(rmObjectAssignCall);
return root.toSource(printOptions);
};