-
-
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][PaginationItem] Add codemod for deprecated classes (#41145
- Loading branch information
Showing
15 changed files
with
592 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
const { | ||
plugin: accordionSummaryClassesPlugin, | ||
} = require('../accordion-summary-classes/postcss-plugin'); | ||
const { | ||
plugin: paginationItemClassesPlugin, | ||
} = require('../pagination-item-classes/postcss-plugin'); | ||
|
||
module.exports = { | ||
plugins: [accordionSummaryClassesPlugin], | ||
plugins: [accordionSummaryClassesPlugin, paginationItemClassesPlugin], | ||
}; |
1 change: 1 addition & 0 deletions
1
packages/mui-codemod/src/deprecations/pagination-item-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 './pagination-item-classes'; |
84 changes: 84 additions & 0 deletions
84
packages/mui-codemod/src/deprecations/pagination-item-classes/pagination-item-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,84 @@ | ||
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 }) => { | ||
root | ||
.find(j.ImportDeclaration) | ||
.filter((path) => path.node.source.value.match(/^@mui\/material\/PaginationItem$/)) | ||
.forEach((path) => { | ||
path.node.specifiers.forEach((specifier) => { | ||
if ( | ||
specifier.type === 'ImportSpecifier' && | ||
specifier.imported.name === 'paginationItemClasses' | ||
) { | ||
const deprecatedAtomicClass = deprecatedClass.replace('.MuiPaginationItem-', ''); | ||
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('MuiPaginationItem-', '') | ||
.replaceAll('&.', '') | ||
.split('.') | ||
.filter(Boolean); | ||
|
||
if (precedingTemplateElement.value.raw.endsWith('&.')) { | ||
parent.expressions.splice( | ||
memberExpressionIndex, | ||
1, | ||
j.memberExpression( | ||
memberExpression.value.object, | ||
j.identifier(atomicClasses[0]), | ||
), | ||
j.memberExpression( | ||
memberExpression.value.object, | ||
j.identifier(atomicClasses[1]), | ||
), | ||
); | ||
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(`^&${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, `&${replacementSelector}`))); | ||
}); | ||
}); | ||
return root.toSource(printOptions); | ||
} |
78 changes: 78 additions & 0 deletions
78
...ages/mui-codemod/src/deprecations/pagination-item-classes/pagination-item-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 './pagination-item-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('pagination-item-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', | ||
); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
packages/mui-codemod/src/deprecations/pagination-item-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,41 @@ | ||
const classes = [ | ||
{ | ||
deprecatedClass: '.MuiPaginationItem-textPrimary', | ||
replacementSelector: '.MuiPaginationItem-text.MuiPaginationItem-colorPrimary', | ||
}, | ||
{ | ||
deprecatedClass: '.MuiPaginationItem-textSecondary', | ||
replacementSelector: '.MuiPaginationItem-text.MuiPaginationItem-colorSecondary', | ||
}, | ||
{ | ||
deprecatedClass: '.MuiPaginationItem-outlinedPrimary', | ||
replacementSelector: '.MuiPaginationItem-outlined.MuiPaginationItem-colorPrimary', | ||
}, | ||
{ | ||
deprecatedClass: '.MuiPaginationItem-outlinedSecondary', | ||
replacementSelector: '.MuiPaginationItem-outlined.MuiPaginationItem-colorSecondary', | ||
}, | ||
]; | ||
|
||
const plugin = () => { | ||
return { | ||
postcssPlugin: `Replace deperecated PaginationItem 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, | ||
}; |
Oops, something went wrong.