Skip to content

Commit 4eb42d8

Browse files
langermankjoshblack
andcommitted
fix tests
Co-authored-by: Josh Black <[email protected]>
1 parent bf51fb4 commit 4eb42d8

File tree

2 files changed

+64
-78
lines changed

2 files changed

+64
-78
lines changed

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

Lines changed: 19 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const rule = require('../no-color-css-vars')
1+
const rule = require('../new-color-css-vars')
22
const {RuleTester} = require('eslint')
33

44
const ruleTester = new RuleTester({
@@ -124,68 +124,24 @@ ruleTester.run('no-color-css-vars', rule, {
124124
message: 'Replace var(--color-border-default) with var(--borderColor-default, var(--color-border-default))'
125125
}
126126
]
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+
]
127145
}
128-
// broken tests start below
129-
// {
130-
// name: 'variable in styled.component',
131-
// code: `
132-
// import {sx, SxProp} from '@primer/react'
133-
// export const HighlightToken = styled.span<SxProp>\`
134-
// color: var(--color-accent-emphasis);
135-
// \${sx}
136-
// \`
137-
// const ClickableTokenSpan = styled(HighlightToken)\`
138-
// &:hover, &:focus { background-color: accent.muted;}
139-
// \`
140-
// `,
141-
// output: `
142-
// import {sx, SxProp} from '@primer/react'
143-
// export const HighlightToken = styled.span<SxProp>\`
144-
// color: var(--bgColor-accent-emphasis, var(--color-accent-emphasis));
145-
// \${sx}
146-
// \`
147-
// const ClickableTokenSpan = styled(HighlightToken)\`
148-
// &:hover, &:focus { background-color: accent.muted;}
149-
// \`
150-
// `,
151-
// errors: [
152-
// {
153-
// message:
154-
// 'Replace var(--color-accent-emphasis) with var(--bgColor-accent-emphasis, var(--color-accent-emphasis))'
155-
// }
156-
// ]
157-
// }
158-
// {
159-
// name: 'MemberExpression in sx: not handled',
160-
// code: `
161-
// const colors = { muted: 'var(--color-fg-muted)' }
162-
// export const Fixture = <Button sx={colors.muted}>Test</Button>
163-
// `,
164-
// output: `
165-
// const colors = { muted: 'var(--fgColor-muted, var(--color-fg-muted))' }
166-
// export const Fixture = <Button sx={colors.muted}>Test</Button>
167-
// `,
168-
// errors: [
169-
// {
170-
// message: 'Replace var(--color-fg-muted) with var(--fgColor-muted, var(--color-fg-muted))'
171-
// }
172-
// ]
173-
// }
174-
// {
175-
// name: 'CallExpression in sx: not handled',
176-
// code: `
177-
// const getColors = () => 'var(--color-fg-muted)'
178-
// export const Fixture = <Button sx={getColors()}>Test</Button>
179-
// `,
180-
// output: `
181-
// const getColors = () => 'var(--fgColor-muted, var(--color-fg-muted))'
182-
// export const Fixture = <Button sx={getColors()}>Test</Button>
183-
// `,
184-
// errors: [
185-
// {
186-
// message: 'Replace var(--color-fg-muted) with var(--fgColor-muted, var(--color-fg-muted))'
187-
// }
188-
// ]
189-
// }
190146
]
191147
})

src/rules/no-color-css-vars.js renamed to src/rules/new-color-css-vars.js

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,28 +70,58 @@ module.exports = {
7070
) {
7171
checkForVariables(node.value, node.value.value)
7272
}
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+
}
73118
}
74119
}
75120

76121
function checkForVariables(node, rawText) {
77122
// performance optimisation: exit early
78123
if (!rawText.includes('var')) return
79124

80-
// Object.keys(cssVars).forEach(cssVar => {
81-
// if (rawText.includes(`var(${cssVar}`)) {
82-
// const fixedString = rawText.replace(`var(${cssVar})`, cssVars[cssVar])
83-
84-
// context.report({
85-
// node,
86-
// message: `Replace var(${cssVar}) with ${cssVars[cssVar]}`,
87-
// fix: function(fixer) {
88-
// return fixer.replaceText(node, node.type === 'Literal' ? `"${fixedString}"` : fixedString)
89-
// }
90-
// })
91-
// }
92-
// })
93-
94-
console.log('Rule is being executed')
95125
Object.keys(cssVars).forEach(cssVar => {
96126
if (Array.isArray(cssVars[cssVar])) {
97127
cssVars[cssVar].forEach(cssVarObject => {

0 commit comments

Comments
 (0)