-
-
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.
Add AccordionSummary classes deprecation codemods
- Loading branch information
1 parent
7917d2a
commit 75f5ff3
Showing
16 changed files
with
464 additions
and
18 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
59 changes: 59 additions & 0 deletions
59
packages/mui-codemod/src/deprecations/accordion-classes/accordion-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,59 @@ | ||
import { deprecatedClass, replacementSelector } 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; | ||
|
||
root.find(j.ObjectProperty, { key: { name: 'contentGutters' } }).forEach((path) => { | ||
if ( | ||
path.parent?.parent?.node.key?.name === 'styleOverrides' && | ||
path.parent?.parent?.parent?.parent?.node.key?.name === 'MuiAccordionSummary' | ||
) { | ||
path.replace( | ||
j.property( | ||
'init', | ||
j.identifier('gutters'), | ||
j.objectExpression([ | ||
j.objectProperty(j.stringLiteral('& .MuiAccordionSummary-content'), path.node.value), | ||
]), | ||
), | ||
); | ||
} | ||
}); | ||
|
||
root | ||
.find(j.Literal, (literal) => literal.value.match(/^.MuiAccordionSummary-contentGutters/)) | ||
.forEach((path) => { | ||
path.replace( | ||
j.literal( | ||
path.value.value.replace( | ||
/^.MuiAccordionSummary-contentGutters/, | ||
'.MuiAccordionSummary-gutters .MuiAccordionSummary-content', | ||
), | ||
), | ||
); | ||
}); | ||
|
||
const directRegex = new RegExp(`^${deprecatedClass}`); | ||
root | ||
.find(j.Literal, (literal) => literal.value.match(directRegex)) | ||
.forEach((path) => { | ||
path.replace(j.literal(path.value.value.replace(directRegex, replacementSelector))); | ||
}); | ||
|
||
// this is a special case for contentGutters as it's applied to the content child | ||
// but gutters is applied to the parent element, so the gutter class needs to go on the parent | ||
const childSelectorRegex = new RegExp(`^& ${deprecatedClass}`); | ||
root | ||
.find(j.Literal, (literal) => literal.value.match(childSelectorRegex)) | ||
.forEach((path) => { | ||
path.replace(j.literal(path.value.value.replace(childSelectorRegex, replacementSelector))); | ||
}); | ||
|
||
return root.toSource(printOptions); | ||
} |
78 changes: 78 additions & 0 deletions
78
packages/mui-codemod/src/deprecations/accordion-classes/accordion-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 './accordion-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('accordion-classes', () => { | ||
describe('js-transform', () => { | ||
it('transforms props as needed', () => { | ||
const actual = jsTransform( | ||
{ source: read('./test-cases/actual.js') }, | ||
{ jscodeshift }, | ||
{ printOptions: { quote: 'single', 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', | ||
); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
1 change: 1 addition & 0 deletions
1
packages/mui-codemod/src/deprecations/accordion-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 './accordion-classes'; |
28 changes: 28 additions & 0 deletions
28
packages/mui-codemod/src/deprecations/accordion-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,28 @@ | ||
const deprecatedClass = '.MuiAccordionSummary-contentGutters'; | ||
const replacementSelector = '.MuiAccordionSummary-gutters .MuiAccordionSummary-content'; | ||
|
||
const plugin = () => { | ||
return { | ||
postcssPlugin: `Replace ${deprecatedClass} with ${replacementSelector}`, | ||
Rule(rule) { | ||
const { selector } = rule; | ||
const directRegex = new RegExp(`^${deprecatedClass}`); | ||
const childSelectorRegex = new RegExp(` ${deprecatedClass}`); | ||
|
||
if (selector.match(directRegex)) { | ||
rule.selector = selector.replace(directRegex, replacementSelector); | ||
} else if (selector.match(childSelectorRegex)) { | ||
// this is a special case for contentGutters as it's applied to the content child | ||
// but gutters is applied to the parent element, so the gutter class needs to go on the parent | ||
rule.selector = selector.replace(childSelectorRegex, replacementSelector); | ||
} | ||
}, | ||
}; | ||
}; | ||
plugin.postcss = true; | ||
|
||
module.exports = { | ||
plugin, | ||
deprecatedClass, | ||
replacementSelector, | ||
}; |
5 changes: 5 additions & 0 deletions
5
packages/mui-codemod/src/deprecations/accordion-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/accordion-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 @@ | ||
.MuiAccordionSummary-contentGutters { | ||
color: red; | ||
} | ||
|
||
.MuiAccordionSummary-root .MuiAccordionSummary-contentGutters { | ||
color: red; | ||
} |
43 changes: 43 additions & 0 deletions
43
packages/mui-codemod/src/deprecations/accordion-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,43 @@ | ||
fn({ | ||
MuiAccordionSummary: { | ||
styleOverrides: { | ||
contentGutters: { | ||
color: 'red', | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
styled(Component)(() => { | ||
return { | ||
'.MuiAccordionSummary-contentGutters': { | ||
color: 'red', | ||
}, | ||
}; | ||
}); | ||
|
||
styled(Component)(() => { | ||
return { | ||
'& .MuiAccordionSummary-contentGutters': { | ||
color: 'red', | ||
}, | ||
}; | ||
}); | ||
|
||
<> | ||
<AccordionSummary | ||
sx={{ | ||
'.MuiAccordionSummary-contentGutters': { | ||
color: 'red', | ||
}, | ||
}} | ||
/> | ||
|
||
<AccordionSummary | ||
sx={{ | ||
'& .MuiAccordionSummary-contentGutters': { | ||
color: 'red', | ||
}, | ||
}} | ||
/> | ||
</>; |
7 changes: 7 additions & 0 deletions
7
packages/mui-codemod/src/deprecations/accordion-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 @@ | ||
.MuiAccordionSummary-gutters .MuiAccordionSummary-content { | ||
color: red; | ||
} | ||
|
||
.MuiAccordionSummary-root.MuiAccordionSummary-gutters .MuiAccordionSummary-content { | ||
color: red; | ||
} |
Oops, something went wrong.