From ae456473eb9bfde47333c973fc2fa63bc39bc9ec Mon Sep 17 00:00:00 2001 From: sai chand <60743144+sai6855@users.noreply.github.com> Date: Thu, 11 Apr 2024 02:00:08 +0530 Subject: [PATCH] [material-ui][StepConnector] deprecate composed classes (#41740) --- .../migrating-from-deprecated-apis.md | 40 ++++++ .../pages/material-ui/api/step-connector.json | 10 +- .../step-connector/step-connector.json | 10 +- packages/mui-codemod/README.md | 36 +++++ .../src/deprecations/all/deprecations-all.js | 2 + .../src/deprecations/all/postcss.config.js | 2 + .../step-connector-classes/index.js | 1 + .../step-connector-classes/postcss-plugin.js | 33 +++++ .../step-connector-classes/postcss.config.js | 5 + .../step-connector-classes.js | 127 ++++++++++++++++++ .../step-connector-classes.test.js | 78 +++++++++++ .../test-cases/actual.css | 7 + .../test-cases/actual.js | 6 + .../test-cases/expected.css | 7 + .../test-cases/expected.js | 6 + .../src/StepConnector/stepConnectorClasses.ts | 8 +- 16 files changed, 368 insertions(+), 10 deletions(-) create mode 100644 packages/mui-codemod/src/deprecations/step-connector-classes/index.js create mode 100644 packages/mui-codemod/src/deprecations/step-connector-classes/postcss-plugin.js create mode 100644 packages/mui-codemod/src/deprecations/step-connector-classes/postcss.config.js create mode 100644 packages/mui-codemod/src/deprecations/step-connector-classes/step-connector-classes.js create mode 100644 packages/mui-codemod/src/deprecations/step-connector-classes/step-connector-classes.test.js create mode 100644 packages/mui-codemod/src/deprecations/step-connector-classes/test-cases/actual.css create mode 100644 packages/mui-codemod/src/deprecations/step-connector-classes/test-cases/actual.js create mode 100644 packages/mui-codemod/src/deprecations/step-connector-classes/test-cases/expected.css create mode 100644 packages/mui-codemod/src/deprecations/step-connector-classes/test-cases/expected.js diff --git a/docs/data/material/migration/migrating-from-deprecated-apis/migrating-from-deprecated-apis.md b/docs/data/material/migration/migrating-from-deprecated-apis/migrating-from-deprecated-apis.md index cc2eba3ce351e0..8a6d4342762ab6 100644 --- a/docs/data/material/migration/migrating-from-deprecated-apis/migrating-from-deprecated-apis.md +++ b/docs/data/material/migration/migrating-from-deprecated-apis/migrating-from-deprecated-apis.md @@ -1012,3 +1012,43 @@ The StepLabel's `componentsProps` was deprecated in favor of `slotProps`: + slotProps={{ label: labelProps }} /> ``` + +## StepConnector + +Use the [codemod](https://github.com/mui/material-ui/tree/HEAD/packages/mui-codemod#step-connector-classes) below to migrate the code as described in the following sections: + +```bash +npx @mui/codemod@next deprecations/step-connector-classes +``` + +### Composed CSS classes + +The CSS classes that composed the `line` CSS class and `orientation` prop values were removed. + +Here's how to migrate: + +```diff +- .MuiStepConnector-lineHorizontal ++.MuiStepConnector-horizontal > .MuiStepConnector-line +- .MuiStepConnector-lineVertical ++.MuiStepConnector-vertical > .MuiStepConnector-line +``` + +```diff + import { stepConnectorClasses } from '@mui/material/StepConnector'; + + MuiStepConnector: { + styleOverrides: { + root: { +- [`& .${stepConnectorClasses.lineHorizontal}`]: { ++ [`&.${stepConnectorClasses.horizontal} > .${stepConnectorClasses.line}`]: { + color: 'red', + }, +- [`& .${stepConnectorClasses.lineVertical}`]: { ++ [`&.${stepConnectorClasses.vertical} > .${stepConnectorClasses.line}`]: { + color: 'red', + }, + }, + }, + }, +``` diff --git a/docs/pages/material-ui/api/step-connector.json b/docs/pages/material-ui/api/step-connector.json index c0d62fb18711f6..ddfa3bb8b11fef 100644 --- a/docs/pages/material-ui/api/step-connector.json +++ b/docs/pages/material-ui/api/step-connector.json @@ -54,14 +54,16 @@ { "key": "lineHorizontal", "className": "MuiStepConnector-lineHorizontal", - "description": "Styles applied to the root element if `orientation=\"horizontal\"`.", - "isGlobal": false + "description": "Styles applied to the line element if `orientation=\"horizontal\"`.", + "isGlobal": false, + "isDeprecated": true }, { "key": "lineVertical", "className": "MuiStepConnector-lineVertical", - "description": "Styles applied to the root element if `orientation=\"vertical\"`.", - "isGlobal": false + "description": "Styles applied to the line element if `orientation=\"vertical\"`.", + "isGlobal": false, + "isDeprecated": true }, { "key": "root", diff --git a/docs/translations/api-docs/step-connector/step-connector.json b/docs/translations/api-docs/step-connector/step-connector.json index 87eacce62772ef..650f2fd5e5c8dd 100644 --- a/docs/translations/api-docs/step-connector/step-connector.json +++ b/docs/translations/api-docs/step-connector/step-connector.json @@ -35,13 +35,15 @@ "line": { "description": "Styles applied to {{nodeName}}.", "nodeName": "the line element" }, "lineHorizontal": { "description": "Styles applied to {{nodeName}} if {{conditions}}.", - "nodeName": "the root element", - "conditions": "orientation=\"horizontal\"" + "nodeName": "the line element", + "conditions": "orientation=\"horizontal\"", + "deprecationInfo": "Combine the .MuiStepConnector-horizontal and .MuiStepConnector-line classes instead. How to migrate" }, "lineVertical": { "description": "Styles applied to {{nodeName}} if {{conditions}}.", - "nodeName": "the root element", - "conditions": "orientation=\"vertical\"" + "nodeName": "the line element", + "conditions": "orientation=\"vertical\"", + "deprecationInfo": "Combine the .MuiStepConnector-vertical and .MuiStepConnector-line classes instead. How to migrate" }, "root": { "description": "Styles applied to the root element." }, "vertical": { diff --git a/packages/mui-codemod/README.md b/packages/mui-codemod/README.md index b37418902a5182..20f6078ca6c131 100644 --- a/packages/mui-codemod/README.md +++ b/packages/mui-codemod/README.md @@ -1008,6 +1008,42 @@ npx @mui/codemod@latest deprecations/step-label-props ``` +#### `step-connector-classes` + +JS transforms: + +```diff + import { stepConnectorClasses } from '@mui/material/StepConnector'; + + MuiStepConnector: { + styleOverrides: { + root: { +- [`& .${stepConnectorClasses.lineHorizontal}`]: { ++ [`&.${stepConnectorClasses.horizontal} > .${stepConnectorClasses.line}`]: { + color: 'red', + }, +- [`& .${stepConnectorClasses.lineVertical}`]: { ++ [`&.${stepConnectorClasses.vertical} > .${stepConnectorClasses.line}`]: { + color: 'red', + }, + }, + }, + }, +``` + +CSS transforms: + +```diff +- .MuiStepConnector-lineHorizontal ++.MuiStepConnector-horizontal > .MuiStepConnector-line +- .MuiStepConnector-lineVertical ++.MuiStepConnector-vertical > .MuiStepConnector-line +``` + +```bash +npx @mui/codemod@next deprecations/step-connector-classes +``` + ### v6.0.0 ### v5.0.0 diff --git a/packages/mui-codemod/src/deprecations/all/deprecations-all.js b/packages/mui-codemod/src/deprecations/all/deprecations-all.js index ba6e2f1fefd130..3a6ca47eeb1072 100644 --- a/packages/mui-codemod/src/deprecations/all/deprecations-all.js +++ b/packages/mui-codemod/src/deprecations/all/deprecations-all.js @@ -10,6 +10,7 @@ import transformAlertClasses from '../alert-classes'; import transformToggleButtonGroupClasses from '../toggle-button-group-classes'; import transformStepLabelProps from '../step-label-props'; import transformBackdropProps from '../backdrop-props'; +import transformStepConnectorClasses from '../step-connector-classes'; /** * @param {import('jscodeshift').FileInfo} file @@ -28,6 +29,7 @@ export default function deprecationsAll(file, api, options) { file.source = transformToggleButtonGroupClasses(file, api, options); file.source = transformStepLabelProps(file, api, options); file.source = transformBackdropProps(file, api, options); + file.source = transformStepConnectorClasses(file, api, options); return file.source; } diff --git a/packages/mui-codemod/src/deprecations/all/postcss.config.js b/packages/mui-codemod/src/deprecations/all/postcss.config.js index 14ce015edeafdb..8e72e2f8b9b462 100644 --- a/packages/mui-codemod/src/deprecations/all/postcss.config.js +++ b/packages/mui-codemod/src/deprecations/all/postcss.config.js @@ -8,6 +8,7 @@ const { plugin: chipClassesPlugin } = require('../chip-classes/postcss-plugin'); const { plugin: paginationItemClassesPlugin, } = require('../pagination-item-classes/postcss-plugin'); +const { plugin: stepConnectorClassesPlugin } = require('../step-connector-classes/postcss-plugin'); const { plugin: toggleButtonGroupClassesPlugin, } = require('../toggle-button-group-classes/postcss-plugin'); @@ -20,6 +21,7 @@ module.exports = { buttonGroupClassesPlugin, chipClassesPlugin, paginationItemClassesPlugin, + stepConnectorClassesPlugin, toggleButtonGroupClassesPlugin, ], }; diff --git a/packages/mui-codemod/src/deprecations/step-connector-classes/index.js b/packages/mui-codemod/src/deprecations/step-connector-classes/index.js new file mode 100644 index 00000000000000..c4b340dac5ae6d --- /dev/null +++ b/packages/mui-codemod/src/deprecations/step-connector-classes/index.js @@ -0,0 +1 @@ +export { default } from './step-connector-classes'; diff --git a/packages/mui-codemod/src/deprecations/step-connector-classes/postcss-plugin.js b/packages/mui-codemod/src/deprecations/step-connector-classes/postcss-plugin.js new file mode 100644 index 00000000000000..71e512e0678a8c --- /dev/null +++ b/packages/mui-codemod/src/deprecations/step-connector-classes/postcss-plugin.js @@ -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, +}; diff --git a/packages/mui-codemod/src/deprecations/step-connector-classes/postcss.config.js b/packages/mui-codemod/src/deprecations/step-connector-classes/postcss.config.js new file mode 100644 index 00000000000000..23bebc1125be6e --- /dev/null +++ b/packages/mui-codemod/src/deprecations/step-connector-classes/postcss.config.js @@ -0,0 +1,5 @@ +const { plugin } = require('./postcss-plugin'); + +module.exports = { + plugins: [plugin], +}; diff --git a/packages/mui-codemod/src/deprecations/step-connector-classes/step-connector-classes.js b/packages/mui-codemod/src/deprecations/step-connector-classes/step-connector-classes.js new file mode 100644 index 00000000000000..00f08a6c9b49f9 --- /dev/null +++ b/packages/mui-codemod/src/deprecations/step-connector-classes/step-connector-classes.js @@ -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); +} diff --git a/packages/mui-codemod/src/deprecations/step-connector-classes/step-connector-classes.test.js b/packages/mui-codemod/src/deprecations/step-connector-classes/step-connector-classes.test.js new file mode 100644 index 00000000000000..ea3ace7ef9d7e4 --- /dev/null +++ b/packages/mui-codemod/src/deprecations/step-connector-classes/step-connector-classes.test.js @@ -0,0 +1,78 @@ +import path from 'path'; +import { expect } from 'chai'; +import postcss from 'postcss'; +import { jscodeshift } from '../../../testUtils'; +import jsTransform from './step-connector-classes'; +import { plugin as postcssPlugin } from './postcss-plugin'; +import readFile from '../../util/readFile'; + +function read(fileName) { + return readFile(path.join(__dirname, fileName)); +} + +const postcssProcessor = postcss([postcssPlugin]); + +describe('@mui/codemod', () => { + describe('deprecations', () => { + describe('step-connector-classes', () => { + describe('js-transform', () => { + it('transforms props as needed', () => { + const actual = jsTransform( + { source: read('./test-cases/actual.js') }, + { jscodeshift }, + { printOptions: { quote: 'double', trailingComma: true } }, + ); + + const expected = read('./test-cases/expected.js'); + expect(actual).to.equal(expected, 'The transformed version should be correct'); + }); + + it('should be idempotent', () => { + const actual = jsTransform( + { source: read('./test-cases/expected.js') }, + { jscodeshift }, + {}, + ); + + const expected = read('./test-cases/expected.js'); + expect(actual).to.equal(expected, 'The transformed version should be correct'); + }); + }); + + describe('css-transform', () => { + it('transforms classes as needed', async () => { + const actual = await postcssProcessor.process(read('./test-cases/actual.css'), { + from: undefined, + }); + + const expected = read('./test-cases/expected.css'); + expect(actual.css).to.equal(expected, 'The transformed version should be correct'); + }); + + it('should be idempotent', async () => { + const actual = await postcssProcessor.process(read('./test-cases/expected.css'), { + from: undefined, + }); + + const expected = read('./test-cases/expected.css'); + expect(actual.css).to.equal(expected, 'The transformed version should be correct'); + }); + }); + + describe('test-cases', () => { + it('should not be the same', () => { + const actualJS = read('./test-cases/actual.js'); + const expectedJS = read('./test-cases/expected.js'); + expect(actualJS).not.to.equal(expectedJS, 'The actual and expected should be different'); + + const actualCSS = read('./test-cases/actual.css'); + const expectedCSS = read('./test-cases/expected.css'); + expect(actualCSS).not.to.equal( + expectedCSS, + 'The actual and expected should be different', + ); + }); + }); + }); + }); +}); diff --git a/packages/mui-codemod/src/deprecations/step-connector-classes/test-cases/actual.css b/packages/mui-codemod/src/deprecations/step-connector-classes/test-cases/actual.css new file mode 100644 index 00000000000000..a6803c62d2dd52 --- /dev/null +++ b/packages/mui-codemod/src/deprecations/step-connector-classes/test-cases/actual.css @@ -0,0 +1,7 @@ +.MuiStepConnector-root .MuiStepConnector-lineHorizontal { + color: red; +} + +.MuiStepConnector-root .MuiStepConnector-lineVertical { + color: red; +} diff --git a/packages/mui-codemod/src/deprecations/step-connector-classes/test-cases/actual.js b/packages/mui-codemod/src/deprecations/step-connector-classes/test-cases/actual.js new file mode 100644 index 00000000000000..2a3d38d0081cbf --- /dev/null +++ b/packages/mui-codemod/src/deprecations/step-connector-classes/test-cases/actual.js @@ -0,0 +1,6 @@ +import { stepConnectorClasses } from '@mui/material/StepConnector'; + +('& .MuiStepConnector-lineHorizontal'); +('& .MuiStepConnector-lineVertical'); +`& .${stepConnectorClasses.lineHorizontal}`; +`& .${stepConnectorClasses.lineVertical}`; diff --git a/packages/mui-codemod/src/deprecations/step-connector-classes/test-cases/expected.css b/packages/mui-codemod/src/deprecations/step-connector-classes/test-cases/expected.css new file mode 100644 index 00000000000000..e57ec5b0540422 --- /dev/null +++ b/packages/mui-codemod/src/deprecations/step-connector-classes/test-cases/expected.css @@ -0,0 +1,7 @@ +.MuiStepConnector-root.MuiStepConnector-horizontal > .MuiStepConnector-line { + color: red; +} + +.MuiStepConnector-root.MuiStepConnector-vertical > .MuiStepConnector-line { + color: red; +} diff --git a/packages/mui-codemod/src/deprecations/step-connector-classes/test-cases/expected.js b/packages/mui-codemod/src/deprecations/step-connector-classes/test-cases/expected.js new file mode 100644 index 00000000000000..5779ddf485b802 --- /dev/null +++ b/packages/mui-codemod/src/deprecations/step-connector-classes/test-cases/expected.js @@ -0,0 +1,6 @@ +import { stepConnectorClasses } from '@mui/material/StepConnector'; + +("&.MuiStepConnector-horizontal > .MuiStepConnector-line"); +("&.MuiStepConnector-vertical > .MuiStepConnector-line"); +`&.${stepConnectorClasses.horizontal} > .${stepConnectorClasses.line}`; +`&.${stepConnectorClasses.vertical} > .${stepConnectorClasses.line}`; diff --git a/packages/mui-material/src/StepConnector/stepConnectorClasses.ts b/packages/mui-material/src/StepConnector/stepConnectorClasses.ts index 30f6fb2f3fa54f..a7f46ca2880c9f 100644 --- a/packages/mui-material/src/StepConnector/stepConnectorClasses.ts +++ b/packages/mui-material/src/StepConnector/stepConnectorClasses.ts @@ -18,9 +18,13 @@ export interface StepConnectorClasses { disabled: string; /** Styles applied to the line element. */ line: string; - /** Styles applied to the root element if `orientation="horizontal"`. */ + /** Styles applied to the line element if `orientation="horizontal"`. + * @deprecated Combine the [.MuiStepConnector-horizontal](/material-ui/api/step-connector/#step-connector-classes-horizontal) and [.MuiStepConnector-line](/material-ui/api/step-connector/#step-connector-classes-line) classes instead. [How to migrate](/material-ui/migration/migrating-from-deprecated-apis/) + */ lineHorizontal: string; - /** Styles applied to the root element if `orientation="vertical"`. */ + /** Styles applied to the line element if `orientation="vertical"`. + * @deprecated Combine the [.MuiStepConnector-vertical](/material-ui/api/step-connector/#step-connector-classes-vertical) and [.MuiStepConnector-line](/material-ui/api/step-connector/#step-connector-classes-line) classes instead. [How to migrate](/material-ui/migration/migrating-from-deprecated-apis/) + */ lineVertical: string; }