-
-
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][Tab] Deprecate iconWrapper class for icon class (#42647)
- Loading branch information
Showing
18 changed files
with
368 additions
and
9 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
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 './tab-classes'; |
29 changes: 29 additions & 0 deletions
29
packages/mui-codemod/src/deprecations/tab-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,29 @@ | ||
const classes = [ | ||
{ | ||
deprecatedClass: ' .MuiTab-iconWrapper', | ||
replacementSelector: ' .MuiTab-icon', | ||
}, | ||
]; | ||
|
||
const plugin = () => { | ||
return { | ||
postcssPlugin: `Replace deprecated Tab 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/tab-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], | ||
}; |
80 changes: 80 additions & 0 deletions
80
packages/mui-codemod/src/deprecations/tab-classes/tab-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,80 @@ | ||
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\/Tab$/)) | ||
.forEach((path) => { | ||
path.node.specifiers.forEach((specifier) => { | ||
if (specifier.type === 'ImportSpecifier' && specifier.imported.name === 'tabClasses') { | ||
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 = ['icon']; | ||
|
||
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); | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
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); | ||
} |
78 changes: 78 additions & 0 deletions
78
packages/mui-codemod/src/deprecations/tab-classes/tab-classes.test.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,78 @@ | ||
import path from 'path'; | ||
import { expect } from 'chai'; | ||
import postcss from 'postcss'; | ||
import { jscodeshift } from '../../../testUtils'; | ||
import jsTransform from './tab-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('toggle-button-group-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', | ||
); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
4 changes: 4 additions & 0 deletions
4
packages/mui-codemod/src/deprecations/tab-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,4 @@ | ||
.MuiTab-root .MuiTab-iconWrapper { | ||
color: red; | ||
} | ||
|
4 changes: 4 additions & 0 deletions
4
packages/mui-codemod/src/deprecations/tab-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,4 @@ | ||
import { tabClasses } from '@mui/material/Tab'; | ||
|
||
('& .MuiTab-iconWrapper'); | ||
`& .${tabClasses.iconWrapper}`; |
3 changes: 3 additions & 0 deletions
3
packages/mui-codemod/src/deprecations/tab-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,3 @@ | ||
.MuiTab-root .MuiTab-icon { | ||
color: red; | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/mui-codemod/src/deprecations/tab-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,4 @@ | ||
import { tabClasses } from '@mui/material/Tab'; | ||
|
||
("& .MuiTab-icon"); | ||
`& .${tabClasses.icon}`; |
Oops, something went wrong.