Skip to content

Commit 340c87a

Browse files
authored
Merge pull request #22 from primer/dmarcey/ignore-non-literal-non-string-args
Ignore non-literal, non-string arguments to `themeGet` in `no-deprecated-colors` rule
2 parents b086a62 + 88a62e6 commit 340c87a

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

.changeset/friendly-months-hope.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-primer-react": patch
3+
---
4+
5+
Ignore non-literal, non-string arguments to `themeGet` in `no-deprecated-colors` rule

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ ruleTester.run('no-deprecated-colors', rule, {
3131
`import {Box} from "@primer/components"; <Box color="fg.default">Hello</Box>`,
3232
`import {hello} from "@primer/components"; hello("colors.text.primary")`,
3333
`import {themeGet} from "@primer/components"; themeGet("space.text.primary")`,
34+
`import {themeGet} from "@primer/components"; themeGet(props.backgroundColorThemeValue)`,
35+
`import {themeGet} from "@primer/components"; themeGet(2)`,
3436
`import {themeGet} from "@other/design-system"; themeGet("colors.text.primary")`,
3537
`import {get} from "@other/constants"; get("space.text.primary")`,
3638
`import {Box} from '@primer/components'; <Box sx={styles}>Hello</Box>`,

src/rules/no-deprecated-colors.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,18 @@ module.exports = {
118118
return
119119
}
120120

121-
const [key, ...path] = node.arguments[0].value.split('.')
121+
const argument = node.arguments[0]
122+
// Skip if the argument is not a Literal (themeGet(props.backgroundColor))
123+
// or a string themeGet(2)
124+
if (argument.type !== 'Literal' || typeof argument.value !== 'string') {
125+
return
126+
}
127+
128+
const [key, ...path] = argument.value.split('.')
122129
const name = path.join('.')
123130

124131
if (['colors', 'shadows'].includes(key) && Object.keys(deprecations).includes(name)) {
125-
replaceDeprecatedColor(context, node.arguments[0], name, str => [key, str].join('.'))
132+
replaceDeprecatedColor(context, argument, name, str => [key, str].join('.'))
126133
}
127134
}
128135
}

0 commit comments

Comments
 (0)