Skip to content

Commit

Permalink
Add noOpacity option to no-hardcoded-color rule
Browse files Browse the repository at this point in the history
  • Loading branch information
anubra266 committed Feb 20, 2024
1 parent 1f90781 commit 22cc4ac
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-pumas-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pandacss/eslint-plugin": patch
---

Add `noOpacity` option to `no-hardcoded-color` rule
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ Or enable all rules in extends:
## Rules

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

| Rule | `recommended` |
| ---------------------------------------------------------------------------------------- | ------------- |
Expand All @@ -84,7 +85,7 @@ Where rules are included in the configs `recommended`, or `all` it is indicated
| [`@pandacss/no-debug`](docs/rules/no-debug.md) | ✔️ |
| [`@pandacss/no-dynamic-styling`](docs/rules/no-dynamic-styling.md) | ✔️ |
| [`@pandacss/no-escape-hatch`](docs/rules/no-escape-hatch.md) | |
| [`@pandacss/no-hardcoded-color`](docs/rules/no-hardcoded-color.md) | |
| [`@pandacss/no-hardcoded-color`](docs/rules/no-hardcoded-color.md) ⚙️ | |
| [`@pandacss/no-important`](docs/rules/no-important.md) | |
| [`@pandacss/no-invalid-token-paths`](docs/rules/no-invalid-token-paths.md) | ✔️ |
| [`@pandacss/no-invalid-nesting`](docs/rules/no-invalid-nesting.md) | ✔️ |
Expand Down
1 change: 1 addition & 0 deletions README.md.template
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Or enable all rules in extends:

## Rules

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

<!-- rules -->
Expand Down
7 changes: 7 additions & 0 deletions docs/rules/no-hardcoded-color.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ function App(){
}
```

❌ Examples of **incorrect** code with `[{"noOpacity":true}]` options:
```js
import { css } from './panda/css';

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

## Resources

* [Rule source](/plugin/src/rules/no-hardcoded-color.ts)
Expand Down
2 changes: 2 additions & 0 deletions docs/templates/optionsAndSettings.ejs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<% if ( data.options || data.settings ) { _%>
with<% if ( data.options ) { %> `<%- data.options %>` options<% } _%>
<% if ( data.settings ) { %><% if ( data.options ) { %> and<% } %> `<%- data.settings %>` settings<% } _%>
<% } _%>
<% if ( data.filename ) { _%>
for a file named `<%- data.filename %>`<% } %>
23 changes: 20 additions & 3 deletions plugin/src/rules/no-hardcoded-color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,25 @@ const rule: Rule = createRule({
invalidColor: '`{{color}}` is not a valid color token.',
},
type: 'suggestion',
schema: [],
schema: [
{
type: 'object',
properties: {
noOpacity: {
type: 'boolean',
},
},
},
],
},
defaultOptions: [],
defaultOptions: [
{
noOpacity: false,
},
],
create(context) {
const noOpacity = context.options[0]?.noOpacity

const isTokenFn = (value?: string) => {
if (!value) return false
const tokens = extractTokens(value)
Expand All @@ -27,7 +42,9 @@ const rule: Rule = createRule({
const testColorToken = (value?: string) => {
if (!value) return false
const color = value?.split('/')
return isColorToken(color[0], context)
const isOpacityToken = !!color[1]?.length
const isValidToken = isColorToken(color[0], context)
return noOpacity ? isValidToken && !isOpacityToken : isValidToken
}

return {
Expand Down
22 changes: 18 additions & 4 deletions plugin/tests/no-hardcoded-color.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { css } from './panda/css';
const styles = css({ color: 'red.100' })`,
},

{
code: javascript`
import { css } from './panda/css';
Expand Down Expand Up @@ -44,6 +45,14 @@ import { css } from './panda/css';
const styles = css({ color: '#FEE2E2' })`,
},

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

{
code: javascript`
import { css } from './panda/css';
Expand All @@ -67,8 +76,13 @@ tester.run(RULE_NAME, rule, {
valid: valids.map(({ code }) => ({
code,
})),
invalid: invalids.map(({ code }) => ({
code,
errors: 1,
})),
invalid: invalids.map(({ code, options }) =>
options
? {
code,
options,
errors: 1,
}
: { code, errors: 1 },
),
})
2 changes: 1 addition & 1 deletion sandbox/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ module.exports = {
//* Panda rules overrides
'@pandacss/no-debug': 'off',
'@pandacss/no-margin-properties': 'warn',
'@pandacss/no-hardcoded-color': 'error',
'@pandacss/no-hardcoded-color': ['error', { noOpacity: true }],
},
}

0 comments on commit 22cc4ac

Please sign in to comment.