Skip to content

Commit b7a836d

Browse files
committed
new attempt
1 parent fc6d725 commit b7a836d

File tree

5 files changed

+115
-2
lines changed

5 files changed

+115
-2
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const rule = require('../no-color-css-vars')
2+
const {RuleTester} = require('eslint')
3+
4+
const ruleTester = new RuleTester({
5+
parserOptions: {
6+
ecmaVersion: 'latest',
7+
sourceType: 'module',
8+
ecmaFeatures: {
9+
jsx: true
10+
}
11+
}
12+
})
13+
14+
// report all strings that start with 'var(--color-'
15+
// autofix strings: 'var(--color-fg-default)' -> 'fg.default'
16+
17+
{
18+
/* <Button sx={{zIndex: 1, color: 'var(--color-fg-muted)'}}> */
19+
}
20+
21+
{
22+
/* <Box sx={{
23+
'&:hover [data-component="copy-link"] button, &:focus [data-component="copy-link"] button': {
24+
color: 'var(--color-accent-fg)',
25+
},
26+
}}
27+
></Box> */
28+
}
29+
30+
ruleTester.run('no-color-css-vars', rule, {
31+
valid: [`{color: 'fg.default'}`],
32+
invalid: [
33+
{
34+
code: `{color: 'var(--color-fg-default)'}`,
35+
output: `{color: 'fg.default'}`,
36+
errors: [
37+
{
38+
message: 'Replace var(--color-fg-default) with fg.default'
39+
}
40+
]
41+
}
42+
]
43+
})

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ const ruleTester = new RuleTester({
1414
// report all strings that start with 'var(--color-'
1515
// autofix strings: 'var(--color-fg-default)' -> 'fg.default'
1616

17+
{
18+
/* <Button sx={{zIndex: 1, color: 'var(--color-fg-muted)'}}> */
19+
}
20+
21+
{
22+
/* <Box sx={{
23+
'&:hover [data-component="copy-link"] button, &:focus [data-component="copy-link"] button': {
24+
color: 'var(--color-accent-fg)',
25+
},
26+
}}
27+
></Box> */
28+
}
29+
1730
ruleTester.run('no-deprecated-colors', rule, {
1831
valid: [`{color: 'text.primary'}`],
1932
invalid: [

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const cssVars = require('../temp-fix.json')
2+
3+
module.exports = {
4+
meta: {
5+
type: 'suggestion',
6+
hasSuggestions: true,
7+
fixable: 'code',
8+
docs: {
9+
description: 'Disallow the use of CSS color variables in React'
10+
},
11+
schema: [
12+
{
13+
type: 'object',
14+
properties: {
15+
skipImportCheck: {
16+
type: 'boolean'
17+
},
18+
checkAllStrings: {
19+
type: 'boolean'
20+
}
21+
},
22+
additionalProperties: false
23+
}
24+
]
25+
},
26+
create(context) {
27+
return {
28+
Literal(node) {
29+
const stringValue = node.value
30+
31+
if (typeof stringValue !== 'string') {
32+
return
33+
}
34+
35+
Object.keys(cssVars).forEach(cssVar => {
36+
if (stringValue.includes(`var(${cssVar}`)) {
37+
const fixedString = stringValue.replace(`var(${cssVar})`, cssVars[cssVar])
38+
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+
}
49+
}
50+
}
51+
}

src/temp-fix.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"--color-fg-default": "fg.default",
3+
"--color-fg-muted": "fg.muted",
4+
"--color-fg-subtle": "fg.subtle",
5+
"--color-fg-onEmphasis": "fg.onEmphasis"
6+
}

0 commit comments

Comments
 (0)