Skip to content

Commit 22cc4ac

Browse files
committed
Add noOpacity option to no-hardcoded-color rule
1 parent 1f90781 commit 22cc4ac

File tree

8 files changed

+57
-10
lines changed

8 files changed

+57
-10
lines changed

.changeset/quiet-pumas-tap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@pandacss/eslint-plugin": patch
3+
---
4+
5+
Add `noOpacity` option to `no-hardcoded-color` rule

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ Or enable all rules in extends:
7575
7676
## Rules
7777

78-
Where rules are included in the configs `recommended`, or `all` it is indicated below.
78+
Rules with ⚙️ have options. Click on the rule to see the options. Where rules are included in the configs `recommended`,
79+
or `all` it is indicated below.
7980

8081
| Rule | `recommended` |
8182
| ---------------------------------------------------------------------------------------- | ------------- |
@@ -84,7 +85,7 @@ Where rules are included in the configs `recommended`, or `all` it is indicated
8485
| [`@pandacss/no-debug`](docs/rules/no-debug.md) | ✔️ |
8586
| [`@pandacss/no-dynamic-styling`](docs/rules/no-dynamic-styling.md) | ✔️ |
8687
| [`@pandacss/no-escape-hatch`](docs/rules/no-escape-hatch.md) | |
87-
| [`@pandacss/no-hardcoded-color`](docs/rules/no-hardcoded-color.md) | |
88+
| [`@pandacss/no-hardcoded-color`](docs/rules/no-hardcoded-color.md) ⚙️ | |
8889
| [`@pandacss/no-important`](docs/rules/no-important.md) | |
8990
| [`@pandacss/no-invalid-token-paths`](docs/rules/no-invalid-token-paths.md) | ✔️ |
9091
| [`@pandacss/no-invalid-nesting`](docs/rules/no-invalid-nesting.md) | ✔️ |

README.md.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Or enable all rules in extends:
7474

7575
## Rules
7676

77+
Rules with ⚙️ have options. Click on the rule to see the options.
7778
Where rules are included in the configs `recommended`, or `all` it is indicated below.
7879

7980
<!-- rules -->

docs/rules/no-hardcoded-color.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ function App(){
6060
}
6161
```
6262

63+
❌ Examples of **incorrect** code with `[{"noOpacity":true}]` options:
64+
```js
65+
import { css } from './panda/css';
66+
67+
const styles = css({ color: 'red.100/30' });
68+
```
69+
6370
## Resources
6471

6572
* [Rule source](/plugin/src/rules/no-hardcoded-color.ts)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<% if ( data.options || data.settings ) { _%>
2+
with<% if ( data.options ) { %> `<%- data.options %>` options<% } _%>
3+
<% if ( data.settings ) { %><% if ( data.options ) { %> and<% } %> `<%- data.settings %>` settings<% } _%>
24
<% } _%>
35
<% if ( data.filename ) { _%>
46
for a file named `<%- data.filename %>`<% } %>

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,25 @@ const rule: Rule = createRule({
1414
invalidColor: '`{{color}}` is not a valid color token.',
1515
},
1616
type: 'suggestion',
17-
schema: [],
17+
schema: [
18+
{
19+
type: 'object',
20+
properties: {
21+
noOpacity: {
22+
type: 'boolean',
23+
},
24+
},
25+
},
26+
],
1827
},
19-
defaultOptions: [],
28+
defaultOptions: [
29+
{
30+
noOpacity: false,
31+
},
32+
],
2033
create(context) {
34+
const noOpacity = context.options[0]?.noOpacity
35+
2136
const isTokenFn = (value?: string) => {
2237
if (!value) return false
2338
const tokens = extractTokens(value)
@@ -27,7 +42,9 @@ const rule: Rule = createRule({
2742
const testColorToken = (value?: string) => {
2843
if (!value) return false
2944
const color = value?.split('/')
30-
return isColorToken(color[0], context)
45+
const isOpacityToken = !!color[1]?.length
46+
const isValidToken = isColorToken(color[0], context)
47+
return noOpacity ? isValidToken && !isOpacityToken : isValidToken
3148
}
3249

3350
return {

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { css } from './panda/css';
1010
1111
const styles = css({ color: 'red.100' })`,
1212
},
13+
1314
{
1415
code: javascript`
1516
import { css } from './panda/css';
@@ -44,6 +45,14 @@ import { css } from './panda/css';
4445
const styles = css({ color: '#FEE2E2' })`,
4546
},
4647

48+
{
49+
options: [{ noOpacity: true }],
50+
code: javascript`
51+
import { css } from './panda/css';
52+
53+
const styles = css({ color: 'red.100/30' })`,
54+
},
55+
4756
{
4857
code: javascript`
4958
import { css } from './panda/css';
@@ -67,8 +76,13 @@ tester.run(RULE_NAME, rule, {
6776
valid: valids.map(({ code }) => ({
6877
code,
6978
})),
70-
invalid: invalids.map(({ code }) => ({
71-
code,
72-
errors: 1,
73-
})),
79+
invalid: invalids.map(({ code, options }) =>
80+
options
81+
? {
82+
code,
83+
options,
84+
errors: 1,
85+
}
86+
: { code, errors: 1 },
87+
),
7488
})

sandbox/.eslintrc.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +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',
20+
'@pandacss/no-hardcoded-color': ['error', { noOpacity: true }],
2121
},
2222
}

0 commit comments

Comments
 (0)