-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove-unused-lodash-fp.js
47 lines (41 loc) · 1.27 KB
/
remove-unused-lodash-fp.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
import { difference } from 'lodash/fp';
const findImportSpecifier = (j, node) => {
const arr = [];
node.find(j.ImportSpecifier).forEach(node => arr.push(node.value.imported.name));
return arr;
};
const findImport = (j, root, packageName) => {
return root.find(j.ImportDeclaration, node => node.source.value === packageName);
};
const createImportNode = (j, methods, packageName) => {
const specifiers = methods.map(name => {
return j.importSpecifier(j.identifier(name));
});
return j.importDeclaration(specifiers, j.literal(packageName));
};
export default (file, api) => {
const j = api.jscodeshift;
const root = j(file.source);
const lodashFpImport = findImport(j, root, 'lodash/fp');
if (lodashFpImport.length < 1) return file.source;
const lodashFpItems = findImportSpecifier(j, lodashFpImport);
// eslint-disable-next-line
let removeGet = true;
root
.find(j.CallExpression, {
callee: {
name: 'get',
},
})
.forEach(() => {
// eslint-disable-next-line
removeGet = false;
});
if (removeGet) {
lodashFpImport.replaceWith(() => {
const newMethods = difference(lodashFpItems, ['get']);
return createImportNode(j, newMethods, 'lodash/fp');
});
}
return root.toSource({ quote: 'single' });
};