-
-
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.
[system] Fix all use of styled(Box) (#40449)
Signed-off-by: Olivier Tassinari <[email protected]>
- Loading branch information
1 parent
48a2922
commit ea16771
Showing
16 changed files
with
155 additions
and
29 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
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
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
55 changes: 55 additions & 0 deletions
55
packages/eslint-plugin-material-ui/src/rules/no-styled-box.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,55 @@ | ||
// Copy from https://github.com/eslint/eslint/blob/95075251fb3ce35aaf7eadbd1d0a737106c13ec6/lib/rules/utils/ast-utils.js#L299 | ||
// Why is this not exported by ESLint? | ||
/** | ||
* Retrieve `ChainExpression#expression` value if the given node a `ChainExpression` node. Otherwise, pass through it. | ||
* @param {ASTNode} node The node to address. | ||
* @returns {ASTNode} The `ChainExpression#expression` value if the node is a `ChainExpression` node. Otherwise, the node. | ||
*/ | ||
function skipChainExpression(node) { | ||
return node && node.type === 'ChainExpression' ? node.expression : node; | ||
} | ||
|
||
/** | ||
* @type {import('eslint').Rule.RuleModule} | ||
*/ | ||
const rule = { | ||
meta: { | ||
docs: { | ||
description: 'Disallow use of styled(Box), we prefer the sx prop over system props.', | ||
}, | ||
messages: { | ||
noBox: "The use of styled(Box) is not allowed, use styled('div') instead.", | ||
}, | ||
type: 'suggestion', | ||
fixable: 'code', | ||
schema: [], | ||
}, | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
const callee = skipChainExpression(node.callee); | ||
if (callee.type !== 'Identifier') { | ||
return; | ||
} | ||
if (callee.name !== 'styled') { | ||
return; | ||
} | ||
if (!node.arguments[0]) { | ||
return; | ||
} | ||
|
||
if (node.arguments[0].type === 'Identifier' && node.arguments[0].name === 'Box') { | ||
context.report({ | ||
node, | ||
messageId: 'noBox', | ||
fix: (fixer) => { | ||
return fixer.replaceText(node.arguments[0], "'div'"); | ||
}, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; | ||
|
||
module.exports = rule; |
72 changes: 72 additions & 0 deletions
72
packages/eslint-plugin-material-ui/src/rules/no-styled-box.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,72 @@ | ||
const eslint = require('eslint'); | ||
const rule = require('./no-styled-box'); | ||
|
||
const ruleTester = new eslint.RuleTester({ | ||
parser: require.resolve('@typescript-eslint/parser'), | ||
parserOptions: { | ||
ecmaFeatures: { jsx: true }, | ||
}, | ||
}); | ||
|
||
ruleTester.run('no-styled-box', rule, { | ||
valid: [ | ||
` | ||
import { styled } from '@mui/system'; | ||
styled('div'); | ||
`, | ||
` | ||
import { styled } from '@mui/system'; | ||
styled('div', {}); | ||
`, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
import { styled } from '@mui/system'; | ||
import Box from '@mui/material/Box'; | ||
const foo = styled(Box)({ | ||
color: 'red', | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'noBox', | ||
type: 'CallExpression', | ||
}, | ||
], | ||
output: ` | ||
import { styled } from '@mui/system'; | ||
import Box from '@mui/material/Box'; | ||
const foo = styled('div')({ | ||
color: 'red', | ||
}); | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
import { styled } from '@mui/system'; | ||
import Box from '@mui/material/Box'; | ||
const foo = styled(Box, {})({ | ||
color: 'red', | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'noBox', | ||
type: 'CallExpression', | ||
}, | ||
], | ||
output: ` | ||
import { styled } from '@mui/system'; | ||
import Box from '@mui/material/Box'; | ||
const foo = styled('div', {})({ | ||
color: 'red', | ||
}); | ||
`, | ||
}, | ||
], | ||
}); |
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