-
-
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][Backdrop] Deprecate TransitionComponent (#40677)
- Loading branch information
1 parent
e3f4a36
commit fe11e80
Showing
15 changed files
with
379 additions
and
67 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
84 changes: 84 additions & 0 deletions
84
packages/mui-codemod/src/deprecations/backdrop-props/backdrop-props.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 findComponentJSX from '../../util/findComponentJSX'; | ||
import assignObject from '../../util/assignObject'; | ||
import appendAttribute from '../../util/appendAttribute'; | ||
|
||
/** | ||
* @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; | ||
|
||
findComponentJSX(j, { root, componentName: 'Backdrop' }, (elementPath) => { | ||
const index = elementPath.node.openingElement.attributes.findIndex( | ||
(attr) => attr.type === 'JSXAttribute' && attr.name.name === 'TransitionComponent', | ||
); | ||
|
||
if (index !== -1) { | ||
const removed = elementPath.node.openingElement.attributes.splice(index, 1); | ||
let hasNode = false; | ||
elementPath.node.openingElement.attributes.forEach((attr) => { | ||
if (attr.name?.name === 'slots') { | ||
hasNode = true; | ||
assignObject(j, { | ||
target: attr, | ||
key: 'transition', | ||
expression: removed[0].value.expression, | ||
}); | ||
} | ||
}); | ||
|
||
if (!hasNode) { | ||
appendAttribute(j, { | ||
target: elementPath.node, | ||
attributeName: 'slots', | ||
expression: j.objectExpression([ | ||
j.objectProperty(j.identifier('transition'), removed[0].value.expression), | ||
]), | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
root.find(j.ObjectProperty, { key: { name: 'TransitionComponent' } }).forEach((path) => { | ||
if (path.parent?.parent?.parent?.parent?.node.key?.name === 'MuiBackdrop') { | ||
const { properties: defaultPropsProperties } = path.parent.value; | ||
|
||
const existingSlots = defaultPropsProperties.find((prop) => prop.key.name === 'slots'); | ||
const slots = existingSlots | ||
? existingSlots.value.properties.reduce((acc, prop) => { | ||
return { ...acc, [prop.key.name]: prop.value }; | ||
}, {}) | ||
: {}; | ||
|
||
const transitionComponent = | ||
defaultPropsProperties.find((prop) => prop.key.name === 'TransitionComponent') ?? {}; | ||
|
||
const updatedSlots = j.objectExpression( | ||
Object.entries({ | ||
transition: transitionComponent?.value, | ||
...slots, | ||
}).map(([slot, value]) => { | ||
return j.objectProperty(j.identifier(slot), value); | ||
}), | ||
); | ||
|
||
if (existingSlots) { | ||
existingSlots.value = updatedSlots; | ||
path.prune(); | ||
} else { | ||
path.replace( | ||
j.property( | ||
'init', | ||
j.identifier('slots'), | ||
j.objectExpression([j.objectProperty(j.identifier('transition'), path.node.value)]), | ||
), | ||
); | ||
} | ||
} | ||
}); | ||
|
||
return root.toSource(printOptions); | ||
} |
53 changes: 53 additions & 0 deletions
53
packages/mui-codemod/src/deprecations/backdrop-props/backdrop-props.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,53 @@ | ||
import path from 'path'; | ||
import { expect } from 'chai'; | ||
import { jscodeshift } from '../../../testUtils'; | ||
import transform from './backdrop-props'; | ||
import readFile from '../../util/readFile'; | ||
|
||
function read(fileName) { | ||
return readFile(path.join(__dirname, fileName)); | ||
} | ||
|
||
describe('@mui/codemod', () => { | ||
describe('deprecations', () => { | ||
describe('backdrop-props', () => { | ||
it('transforms props as needed', () => { | ||
const actual = transform({ source: read('./test-cases/actual.js') }, { jscodeshift }, {}); | ||
|
||
const expected = read('./test-cases/expected.js'); | ||
expect(actual).to.equal(expected, 'The transformed version should be correct'); | ||
}); | ||
|
||
it('should be idempotent', () => { | ||
const actual = transform({ 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('[theme] backdrop-props', () => { | ||
it('transforms props as needed', () => { | ||
const actual = transform( | ||
{ source: read('./test-cases/theme.actual.js') }, | ||
{ jscodeshift }, | ||
{ printOptions: { trailingComma: true } }, | ||
); | ||
|
||
const expected = read('./test-cases/theme.expected.js'); | ||
expect(actual).to.equal(expected, 'The transformed version should be correct'); | ||
}); | ||
|
||
it('should be idempotent', () => { | ||
const actual = transform( | ||
{ source: read('./test-cases/theme.expected.js') }, | ||
{ jscodeshift }, | ||
{}, | ||
); | ||
|
||
const expected = read('./test-cases/theme.expected.js'); | ||
expect(actual).to.equal(expected, 'The transformed version should be correct'); | ||
}); | ||
}); | ||
}); | ||
}); |
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 './backdrop-props'; |
25 changes: 25 additions & 0 deletions
25
packages/mui-codemod/src/deprecations/backdrop-props/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,25 @@ | ||
import Backdrop from '@mui/material/Backdrop'; | ||
import { Backdrop as MyBackdrop } from '@mui/material'; | ||
|
||
<Backdrop TransitionComponent={CustomTransition} />; | ||
<MyBackdrop TransitionComponent={CustomTransition} />; | ||
<Backdrop | ||
TransitionComponent={CustomTransition} | ||
slots={{ | ||
root: 'div', | ||
}} | ||
slotProps={{ | ||
root: { className: 'foo' }, | ||
}} | ||
/>; | ||
<MyBackdrop | ||
TransitionComponent={CustomTransition} | ||
slots={{ | ||
...outerSlots, | ||
}} | ||
slotProps={{ | ||
...outerSlotProps, | ||
}} | ||
/>; | ||
// should skip non MUI components | ||
<NonMuiBackdrop TransitionComponent={CustomTransition} />; |
27 changes: 27 additions & 0 deletions
27
packages/mui-codemod/src/deprecations/backdrop-props/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,27 @@ | ||
import Backdrop from '@mui/material/Backdrop'; | ||
import { Backdrop as MyBackdrop } from '@mui/material'; | ||
|
||
<Backdrop slots={{ | ||
transition: CustomTransition | ||
}} />; | ||
<MyBackdrop slots={{ | ||
transition: CustomTransition | ||
}} />; | ||
<Backdrop | ||
slots={{ | ||
root: 'div', | ||
transition: CustomTransition | ||
}} | ||
slotProps={{ | ||
root: { className: 'foo' }, | ||
}} />; | ||
<MyBackdrop | ||
slots={{ | ||
...outerSlots, | ||
transition: CustomTransition | ||
}} | ||
slotProps={{ | ||
...outerSlotProps, | ||
}} />; | ||
// should skip non MUI components | ||
<NonMuiBackdrop TransitionComponent={CustomTransition} />; |
18 changes: 18 additions & 0 deletions
18
packages/mui-codemod/src/deprecations/backdrop-props/test-cases/theme.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,18 @@ | ||
fn({ | ||
MuiBackdrop: { | ||
defaultProps: { | ||
TransitionComponent: CustomTransition, | ||
}, | ||
}, | ||
}); | ||
|
||
fn({ | ||
MuiBackdrop: { | ||
defaultProps: { | ||
TransitionComponent: CustomTransition, | ||
slots: { | ||
root: 'div', | ||
}, | ||
}, | ||
}, | ||
}); |
Oops, something went wrong.