-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
/
Copy pathbase-remove-unstyled-suffix.js
41 lines (37 loc) · 1.36 KB
/
base-remove-unstyled-suffix.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
/**
* @param {import('jscodeshift').FileInfo} file
* @param {import('jscodeshift').API} api
*/
export default function transformer(file, api) {
const j = api.jscodeshift;
const root = j(file.source);
const transformed = root
.find(j.ImportDeclaration)
.filter(({ node }) => {
const sourceVal = node.source.value;
return sourceVal.startsWith('@mui/base');
})
.forEach((path) => {
const sourceVal = path.node.source.value;
if (sourceVal.startsWith('@mui/base')) {
path.node.source = j.stringLiteral(sourceVal.replace(/unstyled/im, ''));
}
const specifiers = [];
path.node.specifiers.forEach((elementNode) => {
const importedName = elementNode.imported?.name || '';
if (elementNode.type === 'ImportSpecifier' && importedName.match(/unstyled/im)) {
elementNode.imported.name = importedName.replace(/unstyled/im, '');
if (elementNode.local.name === importedName) {
// specifier must be newly created to add "as";
// e.g., import { SwitchUnstyled } to import { Switch as SwitchUnstyled}
specifiers.push(j.importSpecifier(elementNode.imported, elementNode.local));
return;
}
}
specifiers.push(elementNode);
});
path.node.specifiers = specifiers;
})
.toSource();
return transformed;
}