Skip to content

Commit

Permalink
no-dynamic-styling rule should report the dynamic properties (#137)
Browse files Browse the repository at this point in the history
Co-authored-by: anubra266 <[email protected]>
  • Loading branch information
nusuke and anubra266 authored Sep 14, 2024
1 parent 1b1fd10 commit ac21839
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-points-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@pandacss/eslint-plugin': patch
---

Let `no-dynamic-styling` rule, report dynamic properties.
22 changes: 22 additions & 0 deletions docs/rules/no-dynamic-styling.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ import { styled } from './panda/jsx';
function App(){
const color = 'red.100';
return <styled.div color={color} />;
};
```
```js

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

const property = 'background';
const styles = css({ [property]: 'red.100' });
```
```js

import { cva,sva } from './panda/css';

function App(){
const computedValue = "value"
const heading = cva({
variants: {
[computedValue]: {
color: "red.100",
}
}
});
}
```

Expand Down
14 changes: 13 additions & 1 deletion plugin/src/rules/no-dynamic-styling.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TSESTree } from '@typescript-eslint/utils'
import { type Rule, createRule } from '../utils'
import { isPandaAttribute, isPandaProp } from '../utils/helpers'
import { isInPandaFunction, isPandaAttribute, isPandaProp, isRecipeVariant } from '../utils/helpers'
import {
isIdentifier,
isJSXExpressionContainer,
Expand All @@ -19,6 +20,8 @@ const rule: Rule = createRule({
},
messages: {
dynamic: 'Remove dynamic value. Prefer static styles',
dynamicProperty: 'Remove dynamic property. Prefer static style property',
dynamicRecipeVariant: 'Remove dynamic variant. Prefer static variant definition',
},
type: 'suggestion',
schema: [],
Expand Down Expand Up @@ -50,6 +53,15 @@ const rule: Rule = createRule({
})
},

'Property[computed=true]'(node: TSESTree.Property) {
if (!isInPandaFunction(node, context)) return

context.report({
node: node.key,
messageId: isRecipeVariant(node, context) ? 'dynamicRecipeVariant' : 'dynamicProperty',
})
},

Property(node) {
if (!isIdentifier(node.key)) return
if (isLiteral(node.value)) return
Expand Down
24 changes: 24 additions & 0 deletions plugin/tests/no-dynamic-styling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,30 @@ import { styled } from './panda/jsx';
function App(){
const color = 'red.100';
return <styled.div color={color} />;
}`,
},

{
code: javascript`
import { css } from './panda/css';
const property = 'background';
const styles = css({ [property]: 'red.100' })`,
},

{
code: javascript`
import { cva,sva } from './panda/css';
function App(){
const computedValue = "value"
const heading = cva({
variants: {
[computedValue]: {
color: "red.100",
}
}
});
}`,
},
]
Expand Down

0 comments on commit ac21839

Please sign in to comment.