Skip to content

Commit 36414a9

Browse files
committed
Closes #31
Allow opacity color tokens
1 parent e7f6f02 commit 36414a9

File tree

6 files changed

+28
-5
lines changed

6 files changed

+28
-5
lines changed

.changeset/four-elephants-grab.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@pandacss/eslint-plugin": patch
3+
---
4+
5+
Allow color opacity in `no-hardcoded-color` rule

docs/rules/no-hardcoded-color.md

+6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ const styles = css({ color: 'red.100' });
4141

4242
import { css } from './panda/css';
4343

44+
const styles = css({ color: 'red.100/30' });
45+
```
46+
```js
47+
48+
import { css } from './panda/css';
49+
4450
function App(){
4551
return <div className={css({ background: 'green.300' })} />;
4652
};

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ const rule: Rule = createRule({
2424
return tokens.length > 0
2525
}
2626

27+
const testColorToken = (value?: string) => {
28+
if (!value) return false
29+
const color = value?.split('/')
30+
return isColorToken(color[0], context)
31+
}
32+
2733
return {
2834
JSXAttribute(node) {
2935
if (!isJSXIdentifier(node.name)) return
@@ -33,7 +39,7 @@ const rule: Rule = createRule({
3339
isLiteral(node.value) &&
3440
isColorAttribute(node.name.name, context) &&
3541
!isTokenFn(node.value.value?.toString()) &&
36-
!isColorToken(node.value.value?.toString(), context)
42+
!testColorToken(node.value.value?.toString())
3743
) {
3844
context.report({
3945
node: node.value,
@@ -50,7 +56,7 @@ const rule: Rule = createRule({
5056
isLiteral(node.value.expression) &&
5157
isColorAttribute(node.name.name, context) &&
5258
!isTokenFn(node.value.expression.value?.toString()) &&
53-
!isColorToken(node.value.expression.value?.toString(), context)
59+
!testColorToken(node.value.expression.value?.toString())
5460
) {
5561
context.report({
5662
node: node.value.expression,
@@ -69,7 +75,7 @@ const rule: Rule = createRule({
6975
if (!isPandaAttribute(node, context)) return
7076
if (!isColorAttribute(node.key.name, context)) return
7177
if (isTokenFn(node.value.value?.toString())) return
72-
if (isColorToken(node.value.value?.toString(), context)) return
78+
if (testColorToken(node.value.value?.toString())) return
7379

7480
context.report({
7581
node: node.value,

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@ import rule, { RULE_NAME } from '../src/rules/no-hardcoded-color'
33

44
const javascript = String.raw
55

6-
// TODO Watch out for color opacity in the future
7-
86
const valids = [
97
{
108
code: javascript`
119
import { css } from './panda/css';
1210
1311
const styles = css({ color: 'red.100' })`,
1412
},
13+
{
14+
code: javascript`
15+
import { css } from './panda/css';
16+
17+
const styles = css({ color: 'red.100/30' })`,
18+
},
1519

1620
{
1721
code: javascript`

sandbox/.eslintrc.cjs

+1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ module.exports = {
1717
//* Panda rules overrides
1818
'@pandacss/no-debug': 'off',
1919
'@pandacss/no-margin-properties': 'warn',
20+
'@pandacss/no-hardcoded-color': 'error',
2021
},
2122
}

sandbox/src/App.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ function App() {
5353
textAlign: ta,
5454
justify: justify,
5555
_hover: {
56+
color: 'green.300/40',
5657
backgroundColor: 'green.300',
5758
},
5859
})}

0 commit comments

Comments
 (0)