|
| 1 | +import { classes } from './postcss-plugin'; |
| 2 | + |
| 3 | +/** |
| 4 | + * @param {import('jscodeshift').FileInfo} file |
| 5 | + * @param {import('jscodeshift').API} api |
| 6 | + */ |
| 7 | +export default function transformer(file, api, options) { |
| 8 | + const j = api.jscodeshift; |
| 9 | + const root = j(file.source); |
| 10 | + const printOptions = options.printOptions; |
| 11 | + classes.forEach(({ deprecatedClass, replacementSelector }) => { |
| 12 | + const replacementSelectorPrefix = '&'; |
| 13 | + root |
| 14 | + .find(j.ImportDeclaration) |
| 15 | + .filter((path) => path.node.source.value.match(/^@mui\/material\/Dialog$/)) |
| 16 | + .forEach((path) => { |
| 17 | + path.node.specifiers.forEach((specifier) => { |
| 18 | + if (specifier.type === 'ImportSpecifier' && specifier.imported.name === 'dialogClasses') { |
| 19 | + const deprecatedAtomicClass = deprecatedClass.replace( |
| 20 | + `${deprecatedClass.split('-')[0]}-`, |
| 21 | + '', |
| 22 | + ); |
| 23 | + root |
| 24 | + .find(j.MemberExpression, { |
| 25 | + object: { name: specifier.local.name }, |
| 26 | + property: { name: deprecatedAtomicClass }, |
| 27 | + }) |
| 28 | + .forEach((memberExpression) => { |
| 29 | + const parent = memberExpression.parentPath.parentPath.value; |
| 30 | + if (parent.type === j.TemplateLiteral.name) { |
| 31 | + const memberExpressionIndex = parent.expressions.findIndex( |
| 32 | + (expression) => expression === memberExpression.value, |
| 33 | + ); |
| 34 | + const precedingTemplateElement = parent.quasis[memberExpressionIndex]; |
| 35 | + const atomicClasses = replacementSelector |
| 36 | + .replaceAll('MuiDialog-', '') |
| 37 | + .replaceAll(replacementSelectorPrefix, '') |
| 38 | + .replaceAll(' > ', '') |
| 39 | + .replaceAll(' ', '') |
| 40 | + .split('.') |
| 41 | + .filter(Boolean); |
| 42 | + |
| 43 | + if ( |
| 44 | + precedingTemplateElement.value.raw.endsWith( |
| 45 | + deprecatedClass.startsWith(' ') |
| 46 | + ? `${replacementSelectorPrefix} .` |
| 47 | + : `${replacementSelectorPrefix}.`, |
| 48 | + ) |
| 49 | + ) { |
| 50 | + parent.expressions.splice( |
| 51 | + memberExpressionIndex, |
| 52 | + 1, |
| 53 | + j.memberExpression( |
| 54 | + memberExpression.value.object, |
| 55 | + j.identifier(atomicClasses[0]), |
| 56 | + ), |
| 57 | + |
| 58 | + j.memberExpression( |
| 59 | + memberExpression.value.object, |
| 60 | + j.identifier(atomicClasses[1]), |
| 61 | + ), |
| 62 | + ); |
| 63 | + |
| 64 | + if (replacementSelector.includes(' > ')) { |
| 65 | + parent.quasis.splice( |
| 66 | + memberExpressionIndex, |
| 67 | + 1, |
| 68 | + j.templateElement( |
| 69 | + { |
| 70 | + raw: precedingTemplateElement.value.raw, |
| 71 | + cooked: precedingTemplateElement.value.cooked, |
| 72 | + }, |
| 73 | + false, |
| 74 | + ), |
| 75 | + j.templateElement({ raw: ' > .', cooked: ' > .' }, false), |
| 76 | + ); |
| 77 | + } else { |
| 78 | + parent.quasis.splice( |
| 79 | + memberExpressionIndex, |
| 80 | + 1, |
| 81 | + j.templateElement( |
| 82 | + { |
| 83 | + raw: precedingTemplateElement.value.raw, |
| 84 | + cooked: precedingTemplateElement.value.cooked, |
| 85 | + }, |
| 86 | + false, |
| 87 | + ), |
| 88 | + |
| 89 | + j.templateElement({ raw: '.', cooked: '.' }, false), |
| 90 | + ); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + }); |
| 95 | + } |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + const selectorRegex = new RegExp(`^${replacementSelectorPrefix}${deprecatedClass}`); |
| 100 | + root |
| 101 | + .find( |
| 102 | + j.Literal, |
| 103 | + (literal) => typeof literal.value === 'string' && literal.value.match(selectorRegex), |
| 104 | + ) |
| 105 | + .forEach((path) => { |
| 106 | + path.replace( |
| 107 | + j.literal( |
| 108 | + path.value.value.replace( |
| 109 | + selectorRegex, |
| 110 | + `${replacementSelectorPrefix}${deprecatedClass.startsWith(' ') ? ' ' : ''}${replacementSelector.trim()}`, |
| 111 | + ), |
| 112 | + ), |
| 113 | + ); |
| 114 | + }); |
| 115 | + }); |
| 116 | + return root.toSource(printOptions); |
| 117 | +} |
0 commit comments