Skip to content

Commit

Permalink
Closes #31
Browse files Browse the repository at this point in the history
Allow opacity color tokens
  • Loading branch information
anubra266 committed Feb 20, 2024
1 parent e7f6f02 commit 36414a9
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-elephants-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pandacss/eslint-plugin": patch
---

Allow color opacity in `no-hardcoded-color` rule
6 changes: 6 additions & 0 deletions docs/rules/no-hardcoded-color.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ const styles = css({ color: 'red.100' });

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

const styles = css({ color: 'red.100/30' });
```
```js

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

function App(){
return <div className={css({ background: 'green.300' })} />;
};
Expand Down
12 changes: 9 additions & 3 deletions plugin/src/rules/no-hardcoded-color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ const rule: Rule = createRule({
return tokens.length > 0
}

const testColorToken = (value?: string) => {
if (!value) return false
const color = value?.split('/')
return isColorToken(color[0], context)
}

return {
JSXAttribute(node) {
if (!isJSXIdentifier(node.name)) return
Expand All @@ -33,7 +39,7 @@ const rule: Rule = createRule({
isLiteral(node.value) &&
isColorAttribute(node.name.name, context) &&
!isTokenFn(node.value.value?.toString()) &&
!isColorToken(node.value.value?.toString(), context)
!testColorToken(node.value.value?.toString())
) {
context.report({
node: node.value,
Expand All @@ -50,7 +56,7 @@ const rule: Rule = createRule({
isLiteral(node.value.expression) &&
isColorAttribute(node.name.name, context) &&
!isTokenFn(node.value.expression.value?.toString()) &&
!isColorToken(node.value.expression.value?.toString(), context)
!testColorToken(node.value.expression.value?.toString())
) {
context.report({
node: node.value.expression,
Expand All @@ -69,7 +75,7 @@ const rule: Rule = createRule({
if (!isPandaAttribute(node, context)) return
if (!isColorAttribute(node.key.name, context)) return
if (isTokenFn(node.value.value?.toString())) return
if (isColorToken(node.value.value?.toString(), context)) return
if (testColorToken(node.value.value?.toString())) return

context.report({
node: node.value,
Expand Down
8 changes: 6 additions & 2 deletions plugin/tests/no-hardcoded-color.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ import rule, { RULE_NAME } from '../src/rules/no-hardcoded-color'

const javascript = String.raw

// TODO Watch out for color opacity in the future

const valids = [
{
code: javascript`
import { css } from './panda/css';
const styles = css({ color: 'red.100' })`,
},
{
code: javascript`
import { css } from './panda/css';
const styles = css({ color: 'red.100/30' })`,
},

{
code: javascript`
Expand Down
1 change: 1 addition & 0 deletions sandbox/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ module.exports = {
//* Panda rules overrides
'@pandacss/no-debug': 'off',
'@pandacss/no-margin-properties': 'warn',
'@pandacss/no-hardcoded-color': 'error',
},
}
1 change: 1 addition & 0 deletions sandbox/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function App() {
textAlign: ta,
justify: justify,
_hover: {
color: 'green.300/40',
backgroundColor: 'green.300',
},
})}
Expand Down

0 comments on commit 36414a9

Please sign in to comment.