Skip to content

Commit 5882532

Browse files
authored
Add checkImport rule
1 parent 49155f2 commit 5882532

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ ruleTester.run('no-deprecated-colors', rule, {
4343
}
4444
]
4545
},
46+
{
47+
code: `import {Box} from "../components"; function Example() { return <Box color="text.primary">Hello</Box> }`,
48+
output: `import {Box} from "../components"; function Example() { return <Box color="fg.default">Hello</Box> }`,
49+
options: [{checkImport: false}],
50+
errors: [
51+
{
52+
message: '"text.primary" is deprecated. Use "fg.default" instead.'
53+
}
54+
]
55+
},
4656
{
4757
code: `import Box from '@primer/components/lib-esm/Box'; function Example() { return <Box color="text.primary">Hello</Box> }`,
4858
output: `import Box from '@primer/components/lib-esm/Box'; function Example() { return <Box color="fg.default">Hello</Box> }`,

src/rules/no-deprecated-colors.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,28 @@ const styledSystemColorProps = ['color', 'bg', 'backgroundColor', 'borderColor',
55
module.exports = {
66
meta: {
77
type: 'suggestion',
8-
fixable: 'code'
8+
fixable: 'code',
9+
schema: [
10+
{
11+
type: 'object',
12+
properties: {
13+
checkImport: {
14+
type: 'boolean'
15+
}
16+
},
17+
additionalProperties: false
18+
}
19+
]
920
},
1021
create(context) {
22+
// If `shouldCheckImport` is true, this rule will only check for deprecated colors
23+
// used in functions and components that are imported from `@primer/components`.
24+
const shouldCheckImport = context.options[0] ? context.options[0].checkImport : true
25+
1126
return {
1227
JSXOpeningElement(node) {
1328
// Skip if component was not imported from @primer/components
14-
if (!isPrimerComponent(node.name, context.getScope(node))) {
29+
if (shouldCheckImport && !isPrimerComponent(node.name, context.getScope(node))) {
1530
return
1631
}
1732

@@ -49,7 +64,10 @@ module.exports = {
4964
CallExpression(node) {
5065
// Skip if not calling the `themeGet` or `get` function
5166
// `get` is the internal version of `themeGet` that's used in the primer/react repository
52-
if (!isThemeGet(node.callee, context.getScope(node)) && !isGet(node.callee, context.getScope(node))) {
67+
if (
68+
!isThemeGet(node.callee, context.getScope(node), shouldCheckImport) &&
69+
!isGet(node.callee, context.getScope(node))
70+
) {
5371
return
5472
}
5573

@@ -95,11 +113,17 @@ function isPrimerComponent(identifier, scope) {
95113
return isImportedFrom(/^@primer\/components/, identifier, scope)
96114
}
97115

98-
function isThemeGet(identifier, scope) {
99-
return isImportedFrom(/^@primer\/components/, identifier, scope) && identifier.name === 'themeGet'
116+
function isThemeGet(identifier, scope, shouldCheckImport = true) {
117+
if (shouldCheckImport) {
118+
return isImportedFrom(/^@primer\/components/, identifier, scope) && identifier.name === 'themeGet'
119+
}
120+
121+
return identifier.name === 'themeGet'
100122
}
101123

124+
// `get` is the internal version of `themeGet` that's used in the primer/react repository.
102125
function isGet(identifier, scope) {
126+
// This is a flaky way to check for the `get` function and should probably be improved.
103127
return isImportedFrom(/^\.\.?\/constants$/, identifier, scope) && identifier.name === 'get'
104128
}
105129

0 commit comments

Comments
 (0)