-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[codemod] Add accordion props deprecation #40771
Merged
Merged
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7a85173
init deprecations codemod
siriwatknp 5c4c9a2
update readme
siriwatknp 39bf946
add accordion-props
siriwatknp 78e70aa
add missing index file
siriwatknp a05e8a2
fix jscodeshift import for test
siriwatknp 0db96f0
added contributing guide
siriwatknp e54092c
fix logic
siriwatknp 280ece1
update test cases
siriwatknp 09b3225
Merge branch 'master' of https://github.com/mui/material-ui into code…
siriwatknp 46f1f12
fix guide
siriwatknp 8a3e4ba
fix assignObject logic
siriwatknp 4655232
update CONTRIBUTING
siriwatknp f00b718
Merge branch 'master' of https://github.com/mui/material-ui into code…
siriwatknp 3410a5e
trigger build
siriwatknp 22fbadb
Merge branch 'master' of https://github.com/mui/material-ui into code…
siriwatknp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
112 changes: 112 additions & 0 deletions
112
packages/mui-codemod/src/deprecations/accordion-props/accordion-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,112 @@ | ||
/** | ||
* @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.JSXAttribute, { name: { name: 'TransitionComponent' } }).forEach((path) => { | ||
const slotsNode = /** @type import('jscodeshift').JSXOpeningElement */ ( | ||
path.parent.node | ||
).attributes.find((attr) => attr.name?.name === 'slots'); | ||
|
||
if (slotsNode) { | ||
const expContainer = /** @type import('jscodeshift').JSXExpressionContainer */ ( | ||
slotsNode.value | ||
); | ||
if (expContainer.expression.type === 'ObjectExpression') { | ||
// case `slots={{ ... }}` | ||
expContainer.expression.properties.push( | ||
j.objectProperty(j.identifier('transition'), path.node.value.expression), | ||
); | ||
} else if (expContainer.expression.type === 'Identifier') { | ||
// case `slots={outerSlots} | ||
expContainer.expression = j.objectExpression([ | ||
j.spreadElement(j.identifier(expContainer.expression.name)), | ||
j.objectProperty(j.identifier('transition'), path.node.value.expression), | ||
]); | ||
} | ||
} else { | ||
path.insertAfter( | ||
j.jsxAttribute( | ||
j.jsxIdentifier('slots'), | ||
j.jsxExpressionContainer( | ||
j.objectExpression([ | ||
j.objectProperty(j.identifier('transition'), path.node.value.expression), | ||
]), | ||
), | ||
), | ||
); | ||
} | ||
|
||
// remove `TransitionComponent` prop | ||
path.replace(); | ||
}); | ||
|
||
root.find(j.JSXAttribute, { name: { name: 'TransitionProps' } }).forEach((path) => { | ||
const slotPropsNode = /** @type import('jscodeshift').JSXOpeningElement */ ( | ||
path.parent.node | ||
).attributes.find((attr) => attr.name?.name === 'slotProps'); | ||
|
||
if (slotPropsNode) { | ||
// insert to `slotProps` prop | ||
const expContainer = /** @type import('jscodeshift').JSXExpressionContainer */ ( | ||
slotPropsNode.value | ||
); | ||
if (expContainer.expression.type === 'ObjectExpression') { | ||
// case `slotProps={{ ... }}` | ||
expContainer.expression.properties.push( | ||
j.objectProperty(j.identifier('transition'), path.node.value.expression), | ||
); | ||
} else if (expContainer.expression.type === 'Identifier') { | ||
// case `slotProps={outerSlotProps} | ||
expContainer.expression = j.objectExpression([ | ||
j.spreadElement(j.identifier(expContainer.expression.name)), | ||
j.objectProperty(j.identifier('transition'), path.node.value.expression), | ||
]); | ||
} | ||
} else { | ||
path.insertAfter( | ||
j.jsxAttribute( | ||
j.jsxIdentifier('slotProps'), | ||
j.jsxExpressionContainer( | ||
j.objectExpression([ | ||
j.objectProperty(j.identifier('transition'), path.node.value.expression), | ||
]), | ||
), | ||
), | ||
); | ||
} | ||
|
||
// remove `TransitionProps` prop | ||
path.replace(); | ||
}); | ||
|
||
root.find(j.Property, { key: { name: 'TransitionComponent' } }).forEach((path) => { | ||
if (path.parent?.parent?.parent?.parent?.node.key?.name === 'MuiAccordion') { | ||
path.replace( | ||
j.property( | ||
'init', | ||
j.identifier('slots'), | ||
j.objectExpression([j.objectProperty(j.identifier('transition'), path.node.value)]), | ||
), | ||
); | ||
} | ||
}); | ||
|
||
root.find(j.Property, { key: { name: 'TransitionProps' } }).forEach((path) => { | ||
if (path.parent?.parent?.parent?.parent?.node.key?.name === 'MuiAccordion') { | ||
path.replace( | ||
j.property( | ||
'init', | ||
j.identifier('slotProps'), | ||
j.objectExpression([j.objectProperty(j.identifier('transition'), path.node.value)]), | ||
), | ||
); | ||
} | ||
}); | ||
|
||
return root.toSource(printOptions); | ||
} |
73 changes: 73 additions & 0 deletions
73
packages/mui-codemod/src/deprecations/accordion-props/accordion-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,73 @@ | ||
import path from 'path'; | ||
import { expect } from 'chai'; | ||
import jscodeshift from 'jscodeshift'; | ||
import transform from './accordion-props'; | ||
import readFile from '../../util/readFile'; | ||
|
||
function read(fileName) { | ||
return readFile(path.join(__dirname, fileName)); | ||
} | ||
|
||
describe('@mui/codemod', () => { | ||
describe('deprecations', () => { | ||
describe('accordion-props', () => { | ||
it('transforms props as needed', () => { | ||
const actual = transform( | ||
{ | ||
source: read('./test-cases/actual.js'), | ||
path: require.resolve('./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'), | ||
path: require.resolve('./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] accordion-props', () => { | ||
it('transforms props as needed', () => { | ||
const actual = transform( | ||
{ | ||
source: read('./test-cases/theme.actual.js'), | ||
path: require.resolve('./test-cases/theme.actual.js'), | ||
}, | ||
{ jscodeshift }, | ||
{}, | ||
); | ||
|
||
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'), | ||
path: require.resolve('./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 './accordion-props'; |
14 changes: 14 additions & 0 deletions
14
packages/mui-codemod/src/deprecations/accordion-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,14 @@ | ||
<Accordion TransitionComponent={CustomTransition} TransitionProps={{ unmountOnExit: true }} />; | ||
<Accordion TransitionComponent={CustomTransition} TransitionProps={transitionVars} />; | ||
<Accordion | ||
slots={{ root: 'div' }} | ||
slotProps={{ root: { className: 'foo' } }} | ||
TransitionComponent={CustomTransition} | ||
TransitionProps={{ unmountOnExit: true }} | ||
/>; | ||
<Accordion | ||
slots={outerSlots} | ||
slotProps={outerSlotProps} | ||
TransitionComponent={CustomTransition} | ||
TransitionProps={{ unmountOnExit: true }} | ||
/>; |
28 changes: 28 additions & 0 deletions
28
packages/mui-codemod/src/deprecations/accordion-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,28 @@ | ||
<Accordion slots={{ | ||
transition: CustomTransition | ||
}} slotProps={{ | ||
transition: { unmountOnExit: true } | ||
}} />; | ||
<Accordion slots={{ | ||
transition: CustomTransition | ||
}} slotProps={{ | ||
transition: transitionVars | ||
}} />; | ||
<Accordion | ||
slots={{ | ||
root: 'div', | ||
transition: CustomTransition | ||
}} | ||
slotProps={{ | ||
root: { className: 'foo' }, | ||
transition: { unmountOnExit: true } | ||
}} />; | ||
<Accordion | ||
slots={{ | ||
...outerSlots, | ||
transition: CustomTransition | ||
}} | ||
slotProps={{ | ||
...outerSlotProps, | ||
transition: { unmountOnExit: true } | ||
}} />; |
8 changes: 8 additions & 0 deletions
8
packages/mui-codemod/src/deprecations/accordion-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,8 @@ | ||
fn({ | ||
MuiAccordion: { | ||
defaultProps: { | ||
TransitionComponent: CustomTransition, | ||
TransitionProps: { unmountOnExit: true }, | ||
}, | ||
}, | ||
}); |
12 changes: 12 additions & 0 deletions
12
packages/mui-codemod/src/deprecations/accordion-props/test-cases/theme.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,12 @@ | ||
fn({ | ||
MuiAccordion: { | ||
defaultProps: { | ||
slots: { | ||
transition: CustomTransition | ||
}, | ||
slotProps: { | ||
transition: { unmountOnExit: true } | ||
}, | ||
}, | ||
}, | ||
}); |
11 changes: 11 additions & 0 deletions
11
packages/mui-codemod/src/deprecations/all/deprecations-all.js
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file should be |
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,11 @@ | ||
import transformAccordionProps from '../accordion-props/accordion-props'; | ||
|
||
/** | ||
* @param {import('jscodeshift').FileInfo} file | ||
* @param {import('jscodeshift').API} api | ||
*/ | ||
export default function deprecationsAll(file, api, options) { | ||
file.source = transformAccordionProps(file, api, options); | ||
|
||
return file.source; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the difference between the
source
and thepath
properties? I couldn't find any reference topath
inside thetransform
function.