-
-
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
[material-ui][Select] Deprecate composed classes #42950
Closed
Closed
Changes from 18 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
cf7151b
set up
sai6855 dc2fd50
fix codemod
sai6855 d1ca77c
fix
sai6855 83ab52b
fix
sai6855 ebd6128
fix
sai6855 1ec6244
docs:api
sai6855 35f3031
docs:api
sai6855 32df913
add test
sai6855 2891713
fix import
sai6855 035db6f
fix test
sai6855 374e25c
fix
sai6855 f733b75
Merge branch 'next' into dc-select
sai6855 39f5f2a
log test
sai6855 bbb180b
log test
sai6855 e5072b4
fix test
sai6855 6f039d0
fix tests
sai6855 b1799d9
fix css
sai6855 124d5d5
fix
sai6855 af40fc5
add guides
sai6855 15a02d4
fix expected css
sai6855 6b337b7
Merge branch 'next' into dc-select
sai6855 a0f5081
trigger ci
sai6855 35e2934
Merge branch 'next' into dc-select
sai6855 cd098e7
Merge branch 'master' into dc-select
sai6855 38d6996
apply suggestions
sai6855 18f2adf
Merge branch 'master' into dc-select
sai6855 023f64c
add more tests
sai6855 355367d
Merge branch 'dc-select' of https://github.com/sai6855/material-ui in…
sai6855 02c41bd
trigger ci
sai6855 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
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './select-classes'; |
37 changes: 37 additions & 0 deletions
37
packages/mui-codemod/src/deprecations/select-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,37 @@ | ||
const classes = [ | ||
{ | ||
deprecatedClass: ' .MuiSelect-iconFilled', | ||
replacementSelector: ' .MuiSelect-filled ~ .MuiSelect-icon', | ||
}, | ||
{ | ||
deprecatedClass: ' .MuiSelect-iconOutlined', | ||
replacementSelector: ' .MuiSelect-outlined ~ .MuiSelect-icon', | ||
}, | ||
{ | ||
deprecatedClass: ' .MuiSelect-iconStandard', | ||
replacementSelector: ' .MuiSelect-standard ~ .MuiSelect-icon', | ||
}, | ||
]; | ||
|
||
const plugin = () => { | ||
return { | ||
postcssPlugin: `Replace deprecated Select classes with new classes`, | ||
Rule(rule) { | ||
const { selector } = rule; | ||
|
||
classes.forEach(({ deprecatedClass, replacementSelector }) => { | ||
const selectorRegex = new RegExp(`${deprecatedClass.trim()}$`); | ||
|
||
if (selector.match(selectorRegex)) { | ||
rule.selector = selector.replace(selectorRegex, replacementSelector); | ||
} | ||
}); | ||
}, | ||
}; | ||
}; | ||
plugin.postcss = true; | ||
|
||
module.exports = { | ||
plugin, | ||
classes, | ||
}; |
5 changes: 5 additions & 0 deletions
5
packages/mui-codemod/src/deprecations/select-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], | ||
}; |
125 changes: 125 additions & 0 deletions
125
packages/mui-codemod/src/deprecations/select-classes/select-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,125 @@ | ||
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 }) => { | ||
const replacementSelectorPrefix = '&'; | ||
root | ||
.find(j.ImportDeclaration) | ||
.filter((path) => path.node.source.value.match(/^@mui\/material\/Select$/)) | ||
.forEach((path) => { | ||
path.node.specifiers.forEach((specifier) => { | ||
if (specifier.type === 'ImportSpecifier' && specifier.imported.name === 'selectClasses') { | ||
const deprecatedAtomicClass = deprecatedClass.replace( | ||
`${deprecatedClass.split('-')[0]}-`, | ||
'', | ||
); | ||
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('MuiSelect-', '') | ||
.replaceAll(replacementSelectorPrefix, '') | ||
.replaceAll(' ~ ', '') | ||
.split('.') | ||
.map((className) => className.trim()) | ||
.filter(Boolean); | ||
|
||
if ( | ||
precedingTemplateElement.value.raw.endsWith( | ||
deprecatedClass.startsWith(' ') | ||
? `${replacementSelectorPrefix} .` | ||
: `${replacementSelectorPrefix}.`, | ||
) | ||
) { | ||
const atomicClassesArgs = [ | ||
memberExpressionIndex, | ||
1, | ||
...atomicClasses.map((atomicClass) => | ||
j.memberExpression( | ||
memberExpression.value.object, | ||
j.identifier(atomicClass), | ||
), | ||
), | ||
]; | ||
parent.expressions.splice(...atomicClassesArgs); | ||
|
||
if (replacementSelector.includes(' ~ ')) { | ||
const quasisArgs = [ | ||
memberExpressionIndex, | ||
1, | ||
j.templateElement( | ||
{ | ||
raw: precedingTemplateElement.value.raw, | ||
cooked: precedingTemplateElement.value.cooked.replace(' ', ''), | ||
}, | ||
false, | ||
), | ||
j.templateElement({ raw: ' ~ .', cooked: ' ~ .' }, false), | ||
]; | ||
|
||
if (atomicClasses.length === 3) { | ||
quasisArgs.splice( | ||
3, | ||
0, | ||
j.templateElement({ raw: '.', cooked: '.' }, false), | ||
); | ||
} | ||
|
||
parent.quasis.splice(...quasisArgs); | ||
} else { | ||
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(`${replacementSelectorPrefix}${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, | ||
`${replacementSelectorPrefix}${replacementSelector}`, | ||
), | ||
), | ||
); | ||
}); | ||
}); | ||
return root.toSource(printOptions); | ||
} |
78 changes: 78 additions & 0 deletions
78
packages/mui-codemod/src/deprecations/select-classes/select-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 './select-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('toggle-button-group-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', | ||
); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
11 changes: 11 additions & 0 deletions
11
packages/mui-codemod/src/deprecations/select-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,11 @@ | ||
.MuiSelect-iconFilled { | ||
color: red; | ||
} | ||
|
||
.MuiSelect-iconOutlined { | ||
color: red; | ||
} | ||
|
||
.MuiSelect-iconStandard { | ||
color: red; | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/mui-codemod/src/deprecations/select-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,8 @@ | ||
import { selectClasses } from '@mui/material/Select'; | ||
|
||
('& .MuiSelect-iconFilled'); | ||
('& .MuiSelect-iconOutlined'); | ||
('& .MuiSelect-iconStandard'); | ||
`& .${selectClasses.iconFilled}`; | ||
`& .${selectClasses.iconOutlined}`; | ||
`& .${selectClasses.iconStandard}`; |
11 changes: 11 additions & 0 deletions
11
packages/mui-codemod/src/deprecations/select-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,11 @@ | ||
.MuiSelect-filled ~ .MuiSelect-icon { | ||
color: red; | ||
} | ||
|
||
.MuiSelect-outlined ~ .MuiSelect-icon { | ||
color: red; | ||
} | ||
|
||
.MuiSelect-standard ~ .MuiSelect-icon { | ||
color: red; | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/mui-codemod/src/deprecations/select-classes/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,8 @@ | ||
import { selectClasses } from '@mui/material/Select'; | ||
|
||
("& .MuiSelect-filled ~ .MuiSelect-icon"); | ||
("& .MuiSelect-outlined ~ .MuiSelect-icon"); | ||
("& .MuiSelect-standard ~ .MuiSelect-icon"); | ||
`& .${selectClasses.filled} ~ .${selectClasses.icon}`; | ||
`& .${selectClasses.outlined} ~ .${selectClasses.icon}`; | ||
`& .${selectClasses.standard} ~ .${selectClasses.icon}`; |
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
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.
To make sure new selector is actually working