Skip to content

Commit 6a0a63a

Browse files
committed
use sourceCode to check for variables
1 parent a38be53 commit 6a0a63a

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

src/rules/__tests__/no-color-css-vars.test.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,27 @@ ruleTester.run('no-color-css-vars', rule, {
3636
}
3737
]
3838
},
39-
// {
40-
// code: `<Box sx={{'&:hover [data-component="copy-link"] button, &:focus [data-component="copy-link"] button': {
41-
// color: 'var(--color-accent-fg)',
42-
// },
43-
// }}></Box>`,
44-
// output: `<Box sx={{'&:hover [data-component="copy-link"] button, &:focus [data-component="copy-link"] button': {
45-
// color: 'accent.fg',
46-
// },
47-
// }}></Box>`,
48-
// errors: [
49-
// {
50-
// message: 'Replace var(--color-accent-fg) with accent.fg'
51-
// }
52-
// ]
53-
// },
39+
{
40+
code: `
41+
<Box sx={{
42+
'&:hover [data-component="copy-link"] button, &:focus [data-component="copy-link"] button': {
43+
color: 'var(--color-accent-fg)'
44+
}
45+
}}>
46+
</Box>`,
47+
output: `
48+
<Box sx={{
49+
'&:hover [data-component="copy-link"] button, &:focus [data-component="copy-link"] button': {
50+
color: 'accent.fg'
51+
}
52+
}}>
53+
</Box>`,
54+
errors: [
55+
{
56+
message: 'Replace var(--color-accent-fg) with accent.fg'
57+
}
58+
]
59+
},
5460
{
5561
code: `<Box sx={{boxShadow: '0 0 0 2px var(--color-canvas-subtle)'}} />`,
5662
output: `<Box sx={{boxShadow: '0 0 0 2px canvas.subtle'}} />`,
@@ -88,17 +94,17 @@ ruleTester.run('no-color-css-vars', rule, {
8894
]
8995
},
9096
{
91-
code: `<Box backgroundColor='var(--color-canvas-default)' />`,
92-
output: `<Box backgroundColor='canvas.default' />`,
97+
code: `<Box backgroundColor="var(--color-canvas-default)" />`,
98+
output: `<Box backgroundColor="canvas.default" />`,
9399
errors: [
94100
{
95101
message: 'Replace var(--color-canvas-default) with canvas.default'
96102
}
97103
]
98104
},
99105
{
100-
code: `<Box bg='var(--color-canvas-default)' />`,
101-
output: `<Box bg='canvas.default' />`,
106+
code: `<Box bg="var(--color-canvas-default)" />`,
107+
output: `<Box bg="canvas.default" />`,
102108
errors: [
103109
{
104110
message: 'Replace var(--color-canvas-default) with canvas.default'

src/rules/no-color-css-vars.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,31 @@ module.exports = {
4141
return {
4242
JSXAttribute(node) {
4343
if (node.name.name === 'sx') {
44-
node.value.expression.properties.forEach(property => {
45-
if (property.value.type === 'Literal' && typeof property.value.value === 'string') {
46-
checkStringLiteral(property.value, context)
47-
}
48-
})
44+
const rawText = context.getSourceCode().getText(node.value)
45+
checkForVariables(node.value, rawText)
4946
} else if (
5047
styledSystemProps.includes(node.name.name) &&
5148
node.value.type === 'Literal' &&
5249
typeof node.value.value === 'string'
5350
) {
54-
checkStringLiteral(node.value, context)
51+
checkForVariables(node.value, node.value.value)
5552
}
5653
}
5754
}
5855

59-
function checkStringLiteral(node, context) {
56+
function checkForVariables(node, rawText) {
57+
// performance optimisation: exit early
58+
if (!rawText.includes('var')) return
59+
6060
Object.keys(cssVars).forEach(cssVar => {
61-
if (node.value.includes(`var(${cssVar}`)) {
62-
const fixedString = node.value.replace(`var(${cssVar})`, cssVars[cssVar])
61+
if (rawText.includes(`var(${cssVar}`)) {
62+
const fixedString = rawText.replace(`var(${cssVar})`, cssVars[cssVar])
6363

6464
context.report({
6565
node,
6666
message: `Replace var(${cssVar}) with ${cssVars[cssVar]}`,
6767
fix: function(fixer) {
68-
return fixer.replaceText(node, `'${fixedString}'`)
68+
return fixer.replaceText(node, node.type === 'Literal' ? `"${fixedString}"` : fixedString)
6969
}
7070
})
7171
}

0 commit comments

Comments
 (0)