Skip to content

Commit a38be53

Browse files
committed
cleanup
1 parent e127f22 commit a38be53

File tree

4 files changed

+73
-139
lines changed

4 files changed

+73
-139
lines changed

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

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,21 @@ const ruleTester = new RuleTester({
1111
}
1212
})
1313

14-
// report all strings that start with 'var(--color-'
15-
// autofix strings: 'var(--color-fg-default)' -> 'fg.default'
16-
// color, backgroundColor, bg,
17-
1814
ruleTester.run('no-color-css-vars', rule, {
19-
valid: [`{color: 'fg.default'}`],
15+
valid: [
16+
{
17+
code: `{color: 'fg.default'}`
18+
},
19+
{
20+
code: `<circle stroke="var(--color-border-default)" strokeWidth="2" />`
21+
},
22+
{
23+
code: `<circle fill="var(--color-border-default)" strokeWidth="2" />`
24+
},
25+
{
26+
code: `<div style={{ color: 'var(--color-border-default)' }}></div>`
27+
}
28+
],
2029
invalid: [
2130
{
2231
code: `<Button sx={{color: 'var(--color-fg-muted)'}}>Test</Button>`,
@@ -27,21 +36,21 @@ ruleTester.run('no-color-css-vars', rule, {
2736
}
2837
]
2938
},
30-
{
31-
code: `<Box sx={{'&:hover [data-component="copy-link"] button, &:focus [data-component="copy-link"] button': {
32-
color: 'var(--color-accent-fg)',
33-
},
34-
}}></Box>`,
35-
output: `<Box sx={{'&:hover [data-component="copy-link"] button, &:focus [data-component="copy-link"] button': {
36-
color: 'accent.fg',
37-
},
38-
}}></Box>`,
39-
errors: [
40-
{
41-
message: 'Replace var(--color-accent-fg) with accent.fg'
42-
}
43-
]
44-
},
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+
// },
4554
{
4655
code: `<Box sx={{boxShadow: '0 0 0 2px var(--color-canvas-subtle)'}} />`,
4756
output: `<Box sx={{boxShadow: '0 0 0 2px canvas.subtle'}} />`,
@@ -95,8 +104,8 @@ ruleTester.run('no-color-css-vars', rule, {
95104
message: 'Replace var(--color-canvas-default) with canvas.default'
96105
}
97106
]
98-
},
99-
// {
107+
}
108+
//{
100109
// code: `import {sx, SxProp} from '@primer/react' export const HighlightToken = styled.span < SxProp > \`color: var(--color-accent-emphasis); ${sx}\` const ClickableTokenSpan = styled(HighlightToken)\` &:hover, &:focus { background-color: accent.muted;}\``,
101110
// output: `import {sx, SxProp} from '@primer/react' export const HighlightToken = styled.span < SxProp > \`color: accent.emphasis; ${sx}\` const ClickableTokenSpan = styled(HighlightToken)\` &:hover, &:focus { background-color: accent.muted;}\``,
102111
// errors: [
@@ -105,14 +114,5 @@ ruleTester.run('no-color-css-vars', rule, {
105114
// }
106115
// ]
107116
// }
108-
{
109-
code: `<circle stroke="var(--color-border-default)" strokeWidth="2" />`,
110-
output: `<circle stroke="var(--color-border-default)" strokeWidth="2" />`,
111-
errors: [
112-
{
113-
message: 'Replace var(--color-border-default) with border.default'
114-
}
115-
]
116-
}
117117
]
118118
})

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

Lines changed: 0 additions & 43 deletions
This file was deleted.

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

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,52 @@ module.exports = {
2424
]
2525
},
2626
create(context) {
27-
return {
28-
Literal(node) {
29-
const stringValue = node.value
27+
const styledSystemProps = [
28+
'bg',
29+
'backgroundColor',
30+
'color',
31+
'borderColor',
32+
'borderTopColor',
33+
'borderRightColor',
34+
'borderBottomColor',
35+
'borderLeftColor',
36+
'border',
37+
'boxShadow',
38+
'caretColor'
39+
]
3040

31-
if (typeof stringValue !== 'string') {
32-
return
41+
return {
42+
JSXAttribute(node) {
43+
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+
})
49+
} else if (
50+
styledSystemProps.includes(node.name.name) &&
51+
node.value.type === 'Literal' &&
52+
typeof node.value.value === 'string'
53+
) {
54+
checkStringLiteral(node.value, context)
3355
}
56+
}
57+
}
3458

35-
Object.keys(cssVars).forEach(cssVar => {
36-
if (stringValue.includes(`var(${cssVar}`)) {
37-
const fixedString = stringValue.replace(`var(${cssVar})`, cssVars[cssVar])
59+
function checkStringLiteral(node, context) {
60+
Object.keys(cssVars).forEach(cssVar => {
61+
if (node.value.includes(`var(${cssVar}`)) {
62+
const fixedString = node.value.replace(`var(${cssVar})`, cssVars[cssVar])
3863

39-
context.report({
40-
node,
41-
message: `Replace var(${cssVar}) with ${cssVars[cssVar]}`,
42-
fix: function(fixer) {
43-
return fixer.replaceText(node, `'${fixedString}'`)
44-
}
45-
})
46-
}
47-
})
48-
}
64+
context.report({
65+
node,
66+
message: `Replace var(${cssVar}) with ${cssVars[cssVar]}`,
67+
fix: function(fixer) {
68+
return fixer.replaceText(node, `'${fixedString}'`)
69+
}
70+
})
71+
}
72+
})
4973
}
5074
}
5175
}

src/rules/no-css-vars.js

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)