-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[material-ui][ToggleButtonGroup] Deprecate composed classes (#41288)
- Loading branch information
Showing
18 changed files
with
390 additions
and
6 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/toggle-button-group-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 './toggle-button-group-classes'; |
33 changes: 33 additions & 0 deletions
33
packages/mui-codemod/src/deprecations/toggle-button-group-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: ' .MuiToggleButtonGroup-groupedHorizontal', | ||
replacementSelector: '.MuiToggleButtonGroup-horizontal > .MuiToggleButtonGroup-grouped', | ||
}, | ||
{ | ||
deprecatedClass: ' .MuiToggleButtonGroup-groupedVertical', | ||
replacementSelector: '.MuiToggleButtonGroup-vertical > .MuiToggleButtonGroup-grouped', | ||
}, | ||
]; | ||
|
||
const plugin = () => { | ||
return { | ||
postcssPlugin: `Replace deperecated ToggleButtonGroup 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/toggle-button-group-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], | ||
}; |
7 changes: 7 additions & 0 deletions
7
packages/mui-codemod/src/deprecations/toggle-button-group-classes/test-cases/actual.css
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,7 @@ | ||
.MuiToggleButtonGroup-root .MuiToggleButtonGroup-groupedHorizontal { | ||
color: red; | ||
} | ||
|
||
.MuiToggleButtonGroup-root .MuiToggleButtonGroup-groupedVertical { | ||
color: red; | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/mui-codemod/src/deprecations/toggle-button-group-classes/test-cases/actual.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,6 @@ | ||
import { toggleButtonGroupClasses } from '@mui/material/ToggleButtonGroup'; | ||
|
||
('& .MuiToggleButtonGroup-groupedHorizontal'); | ||
('& .MuiToggleButtonGroup-groupedVertical'); | ||
`& .${toggleButtonGroupClasses.groupedHorizontal}`; | ||
`& .${toggleButtonGroupClasses.groupedVertical}`; |
7 changes: 7 additions & 0 deletions
7
packages/mui-codemod/src/deprecations/toggle-button-group-classes/test-cases/expected.css
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,7 @@ | ||
.MuiToggleButtonGroup-root.MuiToggleButtonGroup-horizontal > .MuiToggleButtonGroup-grouped { | ||
color: red; | ||
} | ||
|
||
.MuiToggleButtonGroup-root.MuiToggleButtonGroup-vertical > .MuiToggleButtonGroup-grouped { | ||
color: red; | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/mui-codemod/src/deprecations/toggle-button-group-classes/test-cases/expected.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,6 @@ | ||
import { toggleButtonGroupClasses } from '@mui/material/ToggleButtonGroup'; | ||
|
||
("&.MuiToggleButtonGroup-horizontal > .MuiToggleButtonGroup-grouped"); | ||
("&.MuiToggleButtonGroup-vertical > .MuiToggleButtonGroup-grouped"); | ||
`&.${toggleButtonGroupClasses.horizontal} > .${toggleButtonGroupClasses.grouped}`; | ||
`&.${toggleButtonGroupClasses.vertical} > .${toggleButtonGroupClasses.grouped}`; |
127 changes: 127 additions & 0 deletions
127
...s/mui-codemod/src/deprecations/toggle-button-group-classes/toggle-button-group-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\/ToggleButtonGroup$/)) | ||
.forEach((path) => { | ||
path.node.specifiers.forEach((specifier) => { | ||
if ( | ||
specifier.type === 'ImportSpecifier' && | ||
specifier.imported.name === 'toggleButtonGroupClasses' | ||
) { | ||
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('MuiToggleButtonGroup-', '') | ||
.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.