-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[material-ui][StepConnector] deprecate composed classes (#41740)
- Loading branch information
Showing
16 changed files
with
368 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/mui-codemod/src/deprecations/step-connector-classes/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './step-connector-classes'; |
33 changes: 33 additions & 0 deletions
33
packages/mui-codemod/src/deprecations/step-connector-classes/postcss-plugin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const classes = [ | ||
{ | ||
deprecatedClass: ' .MuiStepConnector-lineHorizontal', | ||
replacementSelector: '.MuiStepConnector-horizontal > .MuiStepConnector-line', | ||
}, | ||
{ | ||
deprecatedClass: ' .MuiStepConnector-lineVertical', | ||
replacementSelector: '.MuiStepConnector-vertical > .MuiStepConnector-line', | ||
}, | ||
]; | ||
|
||
const plugin = () => { | ||
return { | ||
postcssPlugin: `Replace deperecated StepConnector classes with new classes`, | ||
Rule(rule) { | ||
const { selector } = rule; | ||
|
||
classes.forEach(({ deprecatedClass, replacementSelector }) => { | ||
const selectorRegex = new RegExp(`${deprecatedClass}$`); | ||
|
||
if (selector.match(selectorRegex)) { | ||
rule.selector = selector.replace(selectorRegex, replacementSelector); | ||
} | ||
}); | ||
}, | ||
}; | ||
}; | ||
plugin.postcss = true; | ||
|
||
module.exports = { | ||
plugin, | ||
classes, | ||
}; |
5 changes: 5 additions & 0 deletions
5
packages/mui-codemod/src/deprecations/step-connector-classes/postcss.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const { plugin } = require('./postcss-plugin'); | ||
|
||
module.exports = { | ||
plugins: [plugin], | ||
}; |
127 changes: 127 additions & 0 deletions
127
packages/mui-codemod/src/deprecations/step-connector-classes/step-connector-classes.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { classes } from './postcss-plugin'; | ||
|
||
/** | ||
* @param {import('jscodeshift').FileInfo} file | ||
* @param {import('jscodeshift').API} api | ||
*/ | ||
export default function transformer(file, api, options) { | ||
const j = api.jscodeshift; | ||
const root = j(file.source); | ||
const printOptions = options.printOptions; | ||
classes.forEach(({ deprecatedClass, replacementSelector }) => { | ||
const replacementSelectorPrefix = '&'; | ||
root | ||
.find(j.ImportDeclaration) | ||
.filter((path) => path.node.source.value.match(/^@mui\/material\/StepConnector$/)) | ||
.forEach((path) => { | ||
path.node.specifiers.forEach((specifier) => { | ||
if ( | ||
specifier.type === 'ImportSpecifier' && | ||
specifier.imported.name === 'stepConnectorClasses' | ||
) { | ||
const deprecatedAtomicClass = deprecatedClass.replace( | ||
`${deprecatedClass.split('-')[0]}-`, | ||
'', | ||
); | ||
root | ||
.find(j.MemberExpression, { | ||
object: { name: specifier.local.name }, | ||
property: { name: deprecatedAtomicClass }, | ||
}) | ||
.forEach((memberExpression) => { | ||
const parent = memberExpression.parentPath.parentPath.value; | ||
if (parent.type === j.TemplateLiteral.name) { | ||
const memberExpressionIndex = parent.expressions.findIndex( | ||
(expression) => expression === memberExpression.value, | ||
); | ||
const precedingTemplateElement = parent.quasis[memberExpressionIndex]; | ||
const atomicClasses = replacementSelector | ||
.replaceAll('MuiStepConnector-', '') | ||
.replaceAll(replacementSelectorPrefix, '') | ||
.replaceAll(' > ', '') | ||
.split('.') | ||
.filter(Boolean); | ||
|
||
if ( | ||
precedingTemplateElement.value.raw.endsWith( | ||
deprecatedClass.startsWith(' ') | ||
? `${replacementSelectorPrefix} .` | ||
: `${replacementSelectorPrefix}.`, | ||
) | ||
) { | ||
const atomicClassesArgs = [ | ||
memberExpressionIndex, | ||
1, | ||
...atomicClasses.map((atomicClass) => | ||
j.memberExpression( | ||
memberExpression.value.object, | ||
j.identifier(atomicClass), | ||
), | ||
), | ||
]; | ||
parent.expressions.splice(...atomicClassesArgs); | ||
|
||
if (replacementSelector.includes(' > ')) { | ||
const quasisArgs = [ | ||
memberExpressionIndex, | ||
1, | ||
j.templateElement( | ||
{ | ||
raw: precedingTemplateElement.value.raw.replace(' ', ''), | ||
cooked: precedingTemplateElement.value.cooked.replace(' ', ''), | ||
}, | ||
false, | ||
), | ||
j.templateElement({ raw: ' > .', cooked: ' > .' }, false), | ||
]; | ||
|
||
if (atomicClasses.length === 3) { | ||
quasisArgs.splice( | ||
3, | ||
0, | ||
j.templateElement({ raw: '.', cooked: '.' }, false), | ||
); | ||
} | ||
|
||
parent.quasis.splice(...quasisArgs); | ||
} else { | ||
parent.quasis.splice( | ||
memberExpressionIndex, | ||
1, | ||
j.templateElement( | ||
{ | ||
raw: precedingTemplateElement.value.raw, | ||
cooked: precedingTemplateElement.value.cooked, | ||
}, | ||
false, | ||
), | ||
|
||
j.templateElement({ raw: '.', cooked: '.' }, false), | ||
); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
const selectorRegex = new RegExp(`${replacementSelectorPrefix}${deprecatedClass}$`); | ||
root | ||
.find( | ||
j.Literal, | ||
(literal) => typeof literal.value === 'string' && literal.value.match(selectorRegex), | ||
) | ||
.forEach((path) => { | ||
path.replace( | ||
j.literal( | ||
path.value.value.replace( | ||
selectorRegex, | ||
`${replacementSelectorPrefix}${replacementSelector}`, | ||
), | ||
), | ||
); | ||
}); | ||
}); | ||
return root.toSource(printOptions); | ||
} |
Oops, something went wrong.