Skip to content

Commit 3950fb4

Browse files
authored
Check nested properties of sx objects
1 parent 49bf748 commit 3950fb4

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

package-lock.json

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,8 @@
3232
"eslint": ">=4.19.0",
3333
"@primer/primitives": ">=4.6.2"
3434
},
35-
"prettier": "@github/prettier-config"
35+
"prettier": "@github/prettier-config",
36+
"dependencies": {
37+
"eslint-traverse": "^1.0.0"
38+
}
3639
}

src/rules/__tests__/no-deprecated-colors.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ ruleTester.run('no-deprecated-colors', rule, {
7979
}
8080
]
8181
},
82+
{
83+
code: `import {Box} from "@primer/components"; <Box sx={{"&:hover": {bg: "bg.primary"}}} />`,
84+
output: `import {Box} from "@primer/components"; <Box sx={{"&:hover": {bg: "canvas.default"}}} />`,
85+
errors: [
86+
{
87+
message: '"bg.primary" is deprecated. Use "canvas.default" instead.'
88+
}
89+
]
90+
},
8291
{
8392
code: `import {Box} from "@primer/components"; <Box color="auto.green.5" />`,
8493
errors: [

src/rules/no-deprecated-colors.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const deprecations = require('@primer/primitives/dist/deprecations/colors')
2+
const traverse = require('eslint-traverse')
23

34
const styledSystemColorProps = ['color', 'bg', 'backgroundColor', 'borderColor', 'textShadow', 'boxShadow']
45

@@ -25,19 +26,18 @@ module.exports = {
2526

2627
// Check for the sx prop
2728
if (propName === 'sx' && attribute.value.expression.type === 'ObjectExpression') {
28-
// Ignore non-literal properties
29-
const sxProperties = attribute.value.expression.properties.filter(
30-
property => property.type === 'Property' && property.value.type === 'Literal'
31-
)
32-
33-
for (const prop of sxProperties) {
34-
const propName = prop.key.name
35-
const propValue = prop.value.value
36-
37-
if (styledSystemColorProps.includes(propName) && Object.keys(deprecations).includes(propValue)) {
38-
replaceDeprecatedColor(context, prop.value, propValue)
29+
// Search all properties of the sx object (even nested properties)
30+
traverse(context, attribute.value, path => {
31+
if (path.node.type === 'Property' && path.node.value.type === 'Literal') {
32+
const prop = path.node
33+
const propName = prop.key.name
34+
const propValue = prop.value.value
35+
36+
if (styledSystemColorProps.includes(propName) && Object.keys(deprecations).includes(propValue)) {
37+
replaceDeprecatedColor(context, prop.value, propValue)
38+
}
3939
}
40-
}
40+
})
4141
}
4242

4343
// Check if styled-system color prop is using a deprecated color

0 commit comments

Comments
 (0)