Skip to content

Commit 41eb0b4

Browse files
Copilotanubra266
andcommitted
Allow CSS variables in no-hardcoded-color rule
Co-authored-by: anubra266 <[email protected]>
1 parent 98ae5b3 commit 41eb0b4

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

plugin/src/rules/no-hardcoded-color.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ const rule: Rule = createRule({
8282
return tokens.length > 0
8383
}
8484

85+
const isCssVariable = (value: string): boolean => {
86+
if (!value) return false
87+
return value.trim().startsWith('var(')
88+
}
89+
8590
const isValidColorToken = (value: string): boolean => {
8691
if (!value) return false
8792
const [colorToken, opacity] = value.split('/')
@@ -104,6 +109,7 @@ const rule: Rule = createRule({
104109
const checkColorValue = (node: TSESTree.Node, value: string, attributeName: string) => {
105110
if (!isColorAttribute(attributeName)) return
106111
if (isTokenFunctionUsed(value)) return
112+
if (isCssVariable(value)) return
107113
if (isValidColorToken(value)) return
108114

109115
reportInvalidColor(node, value)

plugin/tests/no-hardcoded-color.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,38 @@ import { Circle } from './panda/jsx';
3333
3434
function App(){
3535
return <Circle _hover={{ borderColor: 'gray.100' }} />;
36+
}`,
37+
},
38+
39+
{
40+
code: javascript`
41+
import { css } from './panda/css';
42+
43+
const styles = css({ color: 'var(--my-color)' })`,
44+
},
45+
46+
{
47+
code: javascript`
48+
import { css } from './panda/css';
49+
50+
const styles = css({ backgroundColor: 'var(--bg-color)' })`,
51+
},
52+
53+
{
54+
code: javascript`
55+
import { Circle } from './panda/jsx';
56+
57+
function App(){
58+
return <Circle borderColor='var(--border-color)' />;
59+
}`,
60+
},
61+
62+
{
63+
code: javascript`
64+
import { css } from './panda/css';
65+
66+
function App(){
67+
return <div className={css({ color: 'var(--text-primary)' })} />;
3668
}`,
3769
},
3870
]

0 commit comments

Comments
 (0)