Skip to content

Commit f4c497d

Browse files
committed
v1 of the codemod -> replace import
1 parent 1dff9d0 commit f4c497d

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import j from 'jscodeshift';
2+
3+
module.exports = (file, api: j.API) => {
4+
const j = api.jscodeshift;
5+
const root = j(file.source);
6+
7+
replaceImports(root, j);
8+
9+
return root.toSource({ quote: 'single', lineTerminator: '\n' });
10+
};
11+
12+
const replaceImports = (root, j) => {
13+
// Check if there is an import from react-admin
14+
const reactAdminImport = root.find(j.ImportDeclaration, {
15+
source: {
16+
value: 'react-admin',
17+
},
18+
});
19+
if (!reactAdminImport.length) {
20+
return root.toSource();
21+
}
22+
23+
// Check if there is an import of DataGrid from react-admin
24+
const datagridImport = reactAdminImport.filter(path => {
25+
return path.node.specifiers.some(
26+
specifier =>
27+
j.ImportSpecifier.check(specifier) &&
28+
specifier.imported.name === 'Datagrid'
29+
);
30+
});
31+
console.log('toto - 4', datagridImport);
32+
if (!datagridImport.length) {
33+
console.log('toto - 5 - OUT');
34+
return root.toSource();
35+
}
36+
37+
// Replace import of DataGrid with DataTable
38+
reactAdminImport.replaceWith(({ node }) =>
39+
j.importDeclaration(
40+
node.specifiers.map(specifier => {
41+
if (
42+
j.ImportSpecifier.check(specifier) &&
43+
specifier.imported.name === 'Datagrid'
44+
) {
45+
return j.importSpecifier(j.identifier('DataTable'));
46+
}
47+
return specifier;
48+
}),
49+
node.source
50+
)
51+
);
52+
};

0 commit comments

Comments
 (0)