-
-
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][Divider] Add codemod for
light
prop removal (#40947)
- Loading branch information
Showing
12 changed files
with
316 additions
and
3 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
82 changes: 82 additions & 0 deletions
82
packages/mui-codemod/src/deprecations/divider-props/divider-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,82 @@ | ||
import appendAttribute from '../../util/appendAttribute'; | ||
import assignObject from '../../util/assignObject'; | ||
import findComponentJSX from '../../util/findComponentJSX'; | ||
|
||
/** | ||
* @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: 'Divider' }, (elementPath) => { | ||
elementPath.node.openingElement.attributes = elementPath.node.openingElement.attributes.filter( | ||
(attr) => { | ||
if (attr.type === 'JSXAttribute' && attr.name.name === 'light') { | ||
return false; | ||
} | ||
return true; | ||
}, | ||
); | ||
|
||
const sxIndex = elementPath.node.openingElement.attributes.findIndex( | ||
(attr) => attr.type === 'JSXAttribute' && attr.name.name === 'sx', | ||
); | ||
if (sxIndex === -1) { | ||
appendAttribute(j, { | ||
target: elementPath.node, | ||
attributeName: 'sx', | ||
expression: j.objectExpression([ | ||
j.objectProperty(j.identifier('opacity'), j.literal('0.6')), | ||
]), | ||
}); | ||
} else { | ||
const opacityIndex = elementPath.node.openingElement.attributes[ | ||
sxIndex | ||
].value.expression.properties.findIndex((key) => key.key.name === 'opacity'); | ||
|
||
if (opacityIndex === -1) { | ||
assignObject(j, { | ||
target: elementPath.node.openingElement.attributes[sxIndex], | ||
key: 'opacity', | ||
expression: j.literal('0.6'), | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
root.find(j.ObjectProperty, { key: { name: 'MuiDivider' } }).forEach((path) => { | ||
const defaultPropsObject = path.value.value.properties.find( | ||
(key) => key.key.name === 'defaultProps', | ||
); | ||
|
||
defaultPropsObject.value.properties = defaultPropsObject.value.properties.filter( | ||
(prop) => !['light'].includes(prop?.key?.name), | ||
); | ||
|
||
const sxIndex = defaultPropsObject.value.properties.findIndex((prop) => prop.key.name === 'sx'); | ||
|
||
if (sxIndex === -1) { | ||
defaultPropsObject.value.properties.push( | ||
j.objectProperty( | ||
j.identifier('sx'), | ||
j.objectExpression([j.objectProperty(j.identifier('opacity'), j.literal('0.6'))]), | ||
), | ||
); | ||
} else { | ||
const opacityIndex = defaultPropsObject.value.properties[sxIndex].value.properties.findIndex( | ||
(key) => key.key.name === 'opacity', | ||
); | ||
|
||
if (opacityIndex === -1) { | ||
defaultPropsObject.value.properties[sxIndex].value.properties.push( | ||
j.objectProperty(j.identifier('opacity'), j.literal('0.6')), | ||
); | ||
} | ||
} | ||
}); | ||
|
||
return root.toSource(printOptions); | ||
} |
65 changes: 65 additions & 0 deletions
65
packages/mui-codemod/src/deprecations/divider-props/divider-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,65 @@ | ||
import path from 'path'; | ||
import { expect } from 'chai'; | ||
import { jscodeshift } from '../../../testUtils'; | ||
import transform from './divider-props'; | ||
import readFile from '../../util/readFile'; | ||
|
||
function read(fileName) { | ||
return readFile(path.join(__dirname, fileName)); | ||
} | ||
|
||
describe('@mui/codemod', () => { | ||
describe('deprecations', () => { | ||
describe('divider-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'); | ||
}); | ||
|
||
it('actual.js should not be equal to expected.js', () => { | ||
const actual = read('./test-cases/actual.js'); | ||
const expected = read('./test-cases/expected.js'); | ||
expect(actual).to.not.equal(expected); | ||
}); | ||
}); | ||
|
||
describe('[theme] divider-props', () => { | ||
it('transforms props as needed', () => { | ||
const actual = transform( | ||
{ source: read('./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') }, | ||
{ jscodeshift }, | ||
{}, | ||
); | ||
|
||
const expected = read('./test-cases/theme.expected.js'); | ||
expect(actual).to.equal(expected, 'The transformed version should be correct'); | ||
}); | ||
|
||
it('theme.actual.js should not be equal to theme.expected.js', () => { | ||
const actual = read('./test-cases/theme.actual.js'); | ||
const expected = read('./test-cases/theme.expected.js'); | ||
expect(actual).to.not.equal(expected); | ||
}); | ||
}); | ||
}); | ||
}); |
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 './divider-props'; |
11 changes: 11 additions & 0 deletions
11
packages/mui-codemod/src/deprecations/divider-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,11 @@ | ||
import Divider from '@mui/material/Divider'; | ||
import { Divider as MyDivider } from '@mui/material'; | ||
|
||
<Divider light className="test" />; | ||
<MyDivider light className="test" />; | ||
<Divider light={false} className="test" />; | ||
<MyDivider light={false} className="test" />; | ||
<Divider light={light} className="test" />; | ||
<MyDivider light={light} className="test" />; | ||
<Divider light sx={{ opacity: '0.7' }} />; | ||
<MyDivider light sx={{ bgcolor: 'black' }} />; |
27 changes: 27 additions & 0 deletions
27
packages/mui-codemod/src/deprecations/divider-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 Divider from '@mui/material/Divider'; | ||
import { Divider as MyDivider } from '@mui/material'; | ||
|
||
<Divider className="test" sx={{ | ||
opacity: "0.6" | ||
}} />; | ||
<MyDivider className="test" sx={{ | ||
opacity: "0.6" | ||
}} />; | ||
<Divider className="test" sx={{ | ||
opacity: "0.6" | ||
}} />; | ||
<MyDivider className="test" sx={{ | ||
opacity: "0.6" | ||
}} />; | ||
<Divider className="test" sx={{ | ||
opacity: "0.6" | ||
}} />; | ||
<MyDivider className="test" sx={{ | ||
opacity: "0.6" | ||
}} />; | ||
<Divider sx={{ opacity: '0.7' }} />; | ||
<MyDivider | ||
sx={{ | ||
bgcolor: 'black', | ||
opacity: "0.6" | ||
}} />; |
47 changes: 47 additions & 0 deletions
47
packages/mui-codemod/src/deprecations/divider-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,47 @@ | ||
fn({ | ||
MuiDivider: { | ||
defaultProps: { | ||
light: true, | ||
}, | ||
}, | ||
}); | ||
fn({ | ||
MuiDivider: { | ||
defaultProps: { | ||
light: true, | ||
className: 'my-class', | ||
}, | ||
}, | ||
}); | ||
fn({ | ||
MuiDivider: { | ||
defaultProps: { | ||
light, | ||
className: 'my-class', | ||
}, | ||
}, | ||
}); | ||
|
||
fn({ | ||
MuiDivider: { | ||
defaultProps: { | ||
light, | ||
className: 'my-class', | ||
sx: { | ||
opacity: '0.7', | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
fn({ | ||
MuiDivider: { | ||
defaultProps: { | ||
light, | ||
className: 'my-class', | ||
sx: { | ||
bgcolor: 'black', | ||
}, | ||
}, | ||
}, | ||
}); |
54 changes: 54 additions & 0 deletions
54
packages/mui-codemod/src/deprecations/divider-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,54 @@ | ||
fn({ | ||
MuiDivider: { | ||
defaultProps: { | ||
sx: { | ||
opacity: "0.6" | ||
}, | ||
}, | ||
}, | ||
}); | ||
fn({ | ||
MuiDivider: { | ||
defaultProps: { | ||
className: 'my-class', | ||
sx: { | ||
opacity: "0.6" | ||
}, | ||
}, | ||
}, | ||
}); | ||
fn({ | ||
MuiDivider: { | ||
defaultProps: { | ||
className: 'my-class', | ||
sx: { | ||
opacity: "0.6" | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
fn({ | ||
MuiDivider: { | ||
defaultProps: { | ||
className: 'my-class', | ||
|
||
sx: { | ||
opacity: '0.7', | ||
} | ||
}, | ||
}, | ||
}); | ||
|
||
fn({ | ||
MuiDivider: { | ||
defaultProps: { | ||
className: 'my-class', | ||
|
||
sx: { | ||
bgcolor: 'black', | ||
opacity: "0.6" | ||
} | ||
}, | ||
}, | ||
}); |
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