Skip to content

Commit f9eb90f

Browse files
committed
only support sx prop in favor of stylelint
1 parent a089bcd commit f9eb90f

File tree

2 files changed

+10
-65
lines changed

2 files changed

+10
-65
lines changed

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

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ ruleTester.run('no-color-css-vars', rule, {
2424
},
2525
{
2626
code: `<div style={{ color: 'var(--color-border-default)' }}></div>`
27+
},
28+
{
29+
code: `<Blankslate border></Blankslate>`
2730
}
2831
],
2932
invalid: [
@@ -124,24 +127,6 @@ ruleTester.run('no-color-css-vars', rule, {
124127
message: 'Replace var(--color-border-default) with var(--borderColor-default, var(--color-border-default))'
125128
}
126129
]
127-
},
128-
{
129-
name: 'variable in styled.component',
130-
code: `
131-
import {sx, SxProp} from '@primer/react'
132-
export const HighlightToken = styled.span\`
133-
color: var(--color-accent-emphasis);
134-
\${sx}
135-
\`
136-
const ClickableTokenSpan = styled(HighlightToken)\`
137-
&:hover, &:focus { background-color: accent.muted;}
138-
\`
139-
`,
140-
errors: [
141-
{
142-
message: 'Replace var(--color-accent-emphasis) with var(--fgColor-accent, var(--color-accent-emphasis))'
143-
}
144-
]
145130
}
146131
]
147132
})

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

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = {
66
hasSuggestions: true,
77
fixable: 'code',
88
docs: {
9-
description: 'Disallow the use of CSS color variables in React'
9+
description: 'Upgrade legacy CSS variables to Primitives v8 in sx prop'
1010
},
1111
schema: [
1212
{
@@ -65,56 +65,12 @@ module.exports = {
6565
}
6666
} else if (
6767
styledSystemProps.includes(node.name.name) &&
68+
node.value &&
6869
node.value.type === 'Literal' &&
6970
typeof node.value.value === 'string'
7071
) {
7172
checkForVariables(node.value, node.value.value)
7273
}
73-
},
74-
TaggedTemplateExpression(node) {
75-
if (node.tag.type !== 'MemberExpression') {
76-
return
77-
}
78-
79-
if (node.tag.object.name !== 'styled') {
80-
return
81-
}
82-
83-
const DECLARATION_REGEX = /(.+): (var\(--color-.+\));/
84-
85-
// const StyledComponent = styled.div`
86-
// color: var(--color-fg-example);
87-
// background: var(--color-bg-example);
88-
// `;
89-
for (const templateElement of node.quasi.quasis) {
90-
const rawValue = templateElement.value.raw
91-
const match = rawValue.match(DECLARATION_REGEX)
92-
if (!match) {
93-
continue
94-
}
95-
96-
const property = match[1].trim()
97-
const value = match[2].trim()
98-
99-
for (const [cssVar, replacements] of Object.entries(cssVars)) {
100-
const regex = new RegExp(`var\\(${cssVar}\\)`, 'g')
101-
102-
for (const {props, replacement} of replacements) {
103-
if (!props.includes(property)) {
104-
continue
105-
}
106-
107-
if (!regex.test(value)) {
108-
continue
109-
}
110-
111-
context.report({
112-
node,
113-
message: `Replace var(${cssVar}) with var(${replacement}, var(${cssVar}))`
114-
})
115-
}
116-
}
117-
}
11874
}
11975
}
12076

@@ -126,7 +82,11 @@ module.exports = {
12682
if (Array.isArray(cssVars[cssVar])) {
12783
cssVars[cssVar].forEach(cssVarObject => {
12884
const regex = new RegExp(`var\\(${cssVar}\\)`, 'g')
129-
if (cssVarObject.props.some(prop => rawText.includes(prop)) && regex.test(rawText)) {
85+
if (
86+
cssVarObject.props.some(prop => rawText.includes(prop)) &&
87+
regex.test(rawText) &&
88+
!rawText.includes(cssVarObject.replacement)
89+
) {
13090
const fixedString = rawText.replace(regex, `var(${cssVarObject.replacement}, var(${cssVar}))`)
13191
if (!rawText.includes(fixedString)) {
13292
context.report({

0 commit comments

Comments
 (0)