Skip to content

Commit adc02a2

Browse files
authored
Implement checkAllStrings option
1 parent edadec4 commit adc02a2

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,22 @@ const ruleTester = new RuleTester({
2323

2424
ruleTester.run('no-deprecated-colors', rule, {
2525
valid: [
26-
// `import {Box} from '@other/design-system'; <Box color="text.primary">Hello</Box>`,
26+
`import {Box} from '@other/design-system'; <Box color="text.primary">Hello</Box>`,
2727
`import {Box} from "@primer/components"; <Box color="fg.default">Hello</Box>`,
2828
`import {hello} from "@primer/components"; hello("colors.text.primary")`,
2929
`import {themeGet} from "@primer/components"; themeGet("space.text.primary")`,
3030
`import {themeGet} from "@other/design-system"; themeGet("colors.text.primary")`,
3131
`import {get} from "@other/constants"; get("space.text.primary")`,
3232
`import {Box} from '@primer/components'; <Box sx={styles}>Hello</Box>`,
3333
`import {Box} from '@primer/components'; <Box sx={{color: text.primary}}>Hello</Box>`,
34-
`import {Box} from '@primer/components'; <Box sx={{color: "fg.default"}}>Hello</Box>`
34+
`import {Box} from '@primer/components'; <Box sx={{color: "fg.default"}}>Hello</Box>`,
35+
`{color: 'text.primary'}`
3536
],
3637
invalid: [
3738
{
3839
code: `{color: 'text.primary'}`,
3940
output: `{color: "fg.default"}`,
41+
options: [{checkAllStrings: true}],
4042
errors: [
4143
{
4244
message: '"text.primary" is deprecated. Use "fg.default" instead.'

src/rules/no-deprecated-colors.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ module.exports = {
1313
properties: {
1414
skipImportCheck: {
1515
type: 'boolean'
16+
},
17+
checkAllStrings: {
18+
type: 'boolean'
1619
}
1720
},
1821
additionalProperties: false
@@ -24,9 +27,14 @@ module.exports = {
2427
// used in any components (not just ones that are imported from `@primer/components`).
2528
const skipImportCheck = context.options[0] ? context.options[0].skipImportCheck : false
2629

30+
const checkAllStrings = context.options[0] ? context.options[0].checkAllStrings : false
31+
32+
// Track visited string literals to avoid reporting the same string multiple times
33+
const visitedStrings = new Set()
34+
2735
return {
2836
Literal(node) {
29-
if (Object.keys(deprecations).includes(node.value)) {
37+
if (checkAllStrings && Object.keys(deprecations).includes(node.value) && !visitedStrings.has(node)) {
3038
replaceDeprecatedColor(context, node, node.value)
3139
}
3240
},
@@ -48,15 +56,16 @@ module.exports = {
4856
if (propName === 'sx' && attribute.value.expression.type === 'ObjectExpression') {
4957
// Search all properties of the sx object (even nested properties)
5058
traverse(context, attribute.value, path => {
51-
// if (path.node.type === 'Property' && path.node.value.type === 'Literal') {
52-
// const prop = path.node
53-
// const propName = prop.key.name
54-
// const propValue = prop.value.value
55-
56-
// if (styledSystemColorProps.includes(propName) && Object.keys(deprecations).includes(propValue)) {
57-
// replaceDeprecatedColor(context, prop.value, propValue)
58-
// }
59-
// }
59+
if (path.node.type === 'Property' && path.node.value.type === 'Literal') {
60+
const prop = path.node
61+
const propName = prop.key.name
62+
const propValue = prop.value.value
63+
64+
if (styledSystemColorProps.includes(propName) && Object.keys(deprecations).includes(propValue)) {
65+
replaceDeprecatedColor(context, prop.value, propValue)
66+
visitedStrings.add(prop.value)
67+
}
68+
}
6069

6170
// Check functions passed to sx object properties
6271
// (e.g. boxShadow: theme => `0 1px 2px ${theme.colors.text.primary}` )
@@ -89,9 +98,10 @@ module.exports = {
8998
}
9099

91100
// Check if styled-system color prop is using a deprecated color
92-
// if (styledSystemColorProps.includes(propName) && Object.keys(deprecations).includes(propValue)) {
93-
// replaceDeprecatedColor(context, attribute.value, propValue)
94-
// }
101+
if (styledSystemColorProps.includes(propName) && Object.keys(deprecations).includes(propValue)) {
102+
replaceDeprecatedColor(context, attribute.value, propValue)
103+
visitedStrings.add(attribute.value)
104+
}
95105
}
96106
},
97107
CallExpression(node) {

0 commit comments

Comments
 (0)