Skip to content

Commit 0e05c98

Browse files
authored
Only update components imported from Primer
1 parent a3ec1f1 commit 0e05c98

File tree

2 files changed

+80
-12
lines changed

2 files changed

+80
-12
lines changed

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

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,57 +12,92 @@ jest.mock('@primer/primitives/dist/deprecations/colors_v2', () => testDeprecatio
1212

1313
const ruleTester = new RuleTester({
1414
parserOptions: {
15+
ecmaVersion: 'latest',
16+
sourceType: 'module',
1517
ecmaFeatures: {
1618
jsx: true
1719
}
1820
}
1921
})
2022

2123
ruleTester.run('no-deprecated-colors', rule, {
22-
valid: [],
24+
valid: [
25+
`import {Box} from '@other/design-system'; <Box color="text.primary">Hello</Box>`,
26+
`import {Box} from '@primer/components'; <Box color="fg.default">Hello</Box>`
27+
],
2328
invalid: [
2429
{
25-
code: '<Box color="text.primary" />',
26-
output: '<Box color="fg.default" />',
30+
code: `import {Box} from '@primer/components'; function Example() { return <Box color="text.primary">Hello</Box> }`,
31+
output: `import {Box} from '@primer/components'; function Example() { return <Box color="fg.default">Hello</Box> }`,
2732
errors: [
2833
{
2934
message: '"text.primary" is deprecated. Use "fg.default" instead.'
3035
}
3136
]
3237
},
3338
{
34-
code: '<Box bg="bg.primary" />',
35-
output: '<Box bg="canvas.default" />',
39+
code: `import Box from '@primer/components/lib-esm/Box'; function Example() { return <Box color="text.primary">Hello</Box> }`,
40+
output: `import Box from '@primer/components/lib-esm/Box'; function Example() { return <Box color="fg.default">Hello</Box> }`,
41+
errors: [
42+
{
43+
message: '"text.primary" is deprecated. Use "fg.default" instead.'
44+
}
45+
]
46+
},
47+
{
48+
code: `import {Box} from '@primer/components'; const Example = () => <Box color="text.primary">Hello</Box>`,
49+
output: `import {Box} from '@primer/components'; const Example = () => <Box color="fg.default">Hello</Box>`,
50+
errors: [
51+
{
52+
message: '"text.primary" is deprecated. Use "fg.default" instead.'
53+
}
54+
]
55+
},
56+
{
57+
code: `import {Box} from '@primer/components'; <Box bg="bg.primary" />`,
58+
output: `import {Box} from '@primer/components'; <Box bg="canvas.default" />`,
3659
errors: [
3760
{
3861
message: '"bg.primary" is deprecated. Use "canvas.default" instead.'
3962
}
4063
]
4164
},
4265
{
43-
code: '<Box color="auto.green.5" />',
66+
code: `import {Box} from '@primer/components'; <Box color="auto.green.5" />`,
4467
errors: [
4568
{
4669
message: '"auto.green.5" is deprecated.',
4770
suggestions: [
4871
{
4972
desc: 'Use "success.fg" instead.',
50-
output: '<Box color="success.fg" />'
73+
output: `import {Box} from '@primer/components'; <Box color="success.fg" />`
5174
},
5275
{
5376
desc: 'Use "success.emphasis" instead.',
54-
output: '<Box color="success.emphasis" />'
77+
output: `import {Box} from '@primer/components'; <Box color="success.emphasis" />`
5578
}
5679
]
5780
}
5881
]
5982
},
6083
{
61-
code: '<Box color="fade.fg10" />',
84+
code: `import {Box} from '@primer/components'; <Box color="fade.fg10" />`,
6285
errors: [
6386
{
6487
message:
65-
'"fade.fg10" is deprecated. See https://primer.style/primitives or reach out in the #primer Slack channel to find a suitable replacement.'
88+
'"fade.fg10" is deprecated. Go to https://primer.style/primitives or reach out in the #primer channel on Slack to find a suitable replacement.'
89+
}
90+
]
91+
},
92+
{
93+
code: `import {Box, Text} from '@primer/components'; <Box bg="bg.primary"><Text color="text.primary">Hello</Text></Box>`,
94+
output: `import {Box, Text} from '@primer/components'; <Box bg="canvas.default"><Text color="fg.default">Hello</Text></Box>`,
95+
errors: [
96+
{
97+
message: '"bg.primary" is deprecated. Use "canvas.default" instead.'
98+
},
99+
{
100+
message: '"text.primary" is deprecated. Use "fg.default" instead.'
66101
}
67102
]
68103
}

src/rules/no-deprecated-colors.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@ module.exports = {
88
},
99
create(context) {
1010
return {
11-
// TODO: check get() and themeGet()
1211
JSXOpeningElement(node) {
13-
// TODO: check if jsx element was imported from @primer/components
12+
// Skip if component was not imported from @primer/components
13+
if (!isImportedFrom(/^@primer\/components/, node.name, context.getScope(node))) {
14+
return
15+
}
1416

1517
for (const attribute of node.attributes) {
18+
if (!attribute.name || !attribute.value) {
19+
continue
20+
}
21+
1622
const propName = attribute.name.name
1723
const propValue = attribute.value.value
1824

@@ -54,3 +60,30 @@ module.exports = {
5460
}
5561
}
5662
}
63+
64+
/**
65+
* Get the variable declaration for the given identifier
66+
*/
67+
function getVariableDeclaration(scope, identifier) {
68+
if (scope === null) {
69+
return null
70+
}
71+
72+
for (const variable of scope.variables) {
73+
if (variable.name === identifier.name) {
74+
return variable.defs[0]
75+
}
76+
}
77+
78+
return getVariableDeclaration(scope.upper, identifier)
79+
}
80+
81+
/**
82+
* Check if the given identifier is imported from the given module
83+
*/
84+
function isImportedFrom(moduleRegex, identifier, scope) {
85+
const definition = getVariableDeclaration(scope, identifier)
86+
87+
// Return true if the variable was imported from the given module
88+
return definition && definition.type == 'ImportBinding' && moduleRegex.test(definition.parent.source.value)
89+
}

0 commit comments

Comments
 (0)