UNPKG

@mui/codemod

Version:
90 lines (88 loc) 3.38 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = transformer; var _jscodeshiftAddImports = _interopRequireDefault(require("jscodeshift-add-imports")); // istanbul ignore next if (process.env.NODE_ENV === 'test') { const resolve = require.resolve; require.resolve = source => resolve(source.replace(/^@mui\/material\/modern/, '../../../mui-material/src')); } const barrelImportsToTransform = { material: {}, 'icons-material': {} }; const muiImportRegExp = /^@mui\/([^/]+)$/; function transformer(fileInfo, api, options) { const j = api.jscodeshift; const printOptions = options.printOptions || { quote: 'single', trailingComma: true }; const root = j(fileInfo.source); root.find(j.ImportDeclaration).forEach(path => { if (!path.node.specifiers.length) { return; } if (path.value.importKind && path.value.importKind !== 'value') { return; } const importPath = path.value.source.value; const match = importPath.match(muiImportRegExp); if (!match) { return; } const moduleName = match[1]; const importsToAdd = barrelImportsToTransform[moduleName]; if (!importsToAdd) { return; } const indexesToPrune = []; path.node.specifiers.forEach((specifier, index) => { if (specifier.importKind && specifier.importKind !== 'value') { return; } if (specifier.type === 'ImportNamespaceSpecifier') { return; } if (specifier.type === 'ImportSpecifier') { const name = specifier.imported.name; if (moduleName === 'material') { if (name === 'ThemeProvider' || name === 'createTheme') { importsToAdd.styles ?? (importsToAdd.styles = []); importsToAdd.styles.push(specifier); indexesToPrune.push(index); return; } if (name.endsWith('Classes')) { const base = name.replace(/Classes$/, ''); const componentName = base.charAt(0).toUpperCase() + base.slice(1); // autocomplete → Autocomplete importsToAdd[componentName] ?? (importsToAdd[componentName] = []); importsToAdd[componentName].push(j.importSpecifier(specifier.imported, specifier.local)); indexesToPrune.push(index); return; } } importsToAdd[name] ?? (importsToAdd[name] = []); importsToAdd[name].push(j.importDefaultSpecifier(specifier.local)); indexesToPrune.push(index); } }); // We prune imports starting with the highest index as otherwise subsequent indexes would become // invalid once an index that comes before it gets pruned. indexesToPrune.sort((a, b) => a - b).reverse(); indexesToPrune.forEach(index => path.get('specifiers', index).prune()); if (!path.node.specifiers.length) { path.prune(); } }); Object.entries(barrelImportsToTransform).forEach(([moduleName, importsToAdd]) => { Object.entries(importsToAdd).forEach(([module, specifiers]) => { const fullTargetModule = `@mui/${moduleName}/${module}`; (0, _jscodeshiftAddImports.default)(root, j.importDeclaration(specifiers, j.stringLiteral(fullTargetModule))); }); }); return root.toSource(printOptions); }