Skip to content

Commit ac21839

Browse files
nusukeanubra266
andauthored
no-dynamic-styling rule should report the dynamic properties (#137)
Co-authored-by: anubra266 <[email protected]>
1 parent 1b1fd10 commit ac21839

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

.changeset/fuzzy-points-invent.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@pandacss/eslint-plugin': patch
3+
---
4+
5+
Let `no-dynamic-styling` rule, report dynamic properties.

docs/rules/no-dynamic-styling.md

+22
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ import { styled } from './panda/jsx';
4141
function App(){
4242
const color = 'red.100';
4343
return <styled.div color={color} />;
44+
};
45+
```
46+
```js
47+
48+
import { css } from './panda/css';
49+
50+
const property = 'background';
51+
const styles = css({ [property]: 'red.100' });
52+
```
53+
```js
54+
55+
import { cva,sva } from './panda/css';
56+
57+
function App(){
58+
const computedValue = "value"
59+
const heading = cva({
60+
variants: {
61+
[computedValue]: {
62+
color: "red.100",
63+
}
64+
}
65+
});
4466
}
4567
```
4668

plugin/src/rules/no-dynamic-styling.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import type { TSESTree } from '@typescript-eslint/utils'
12
import { type Rule, createRule } from '../utils'
2-
import { isPandaAttribute, isPandaProp } from '../utils/helpers'
3+
import { isInPandaFunction, isPandaAttribute, isPandaProp, isRecipeVariant } from '../utils/helpers'
34
import {
45
isIdentifier,
56
isJSXExpressionContainer,
@@ -19,6 +20,8 @@ const rule: Rule = createRule({
1920
},
2021
messages: {
2122
dynamic: 'Remove dynamic value. Prefer static styles',
23+
dynamicProperty: 'Remove dynamic property. Prefer static style property',
24+
dynamicRecipeVariant: 'Remove dynamic variant. Prefer static variant definition',
2225
},
2326
type: 'suggestion',
2427
schema: [],
@@ -50,6 +53,15 @@ const rule: Rule = createRule({
5053
})
5154
},
5255

56+
'Property[computed=true]'(node: TSESTree.Property) {
57+
if (!isInPandaFunction(node, context)) return
58+
59+
context.report({
60+
node: node.key,
61+
messageId: isRecipeVariant(node, context) ? 'dynamicRecipeVariant' : 'dynamicProperty',
62+
})
63+
},
64+
5365
Property(node) {
5466
if (!isIdentifier(node.key)) return
5567
if (isLiteral(node.value)) return

plugin/tests/no-dynamic-styling.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,30 @@ import { styled } from './panda/jsx';
6464
function App(){
6565
const color = 'red.100';
6666
return <styled.div color={color} />;
67+
}`,
68+
},
69+
70+
{
71+
code: javascript`
72+
import { css } from './panda/css';
73+
74+
const property = 'background';
75+
const styles = css({ [property]: 'red.100' })`,
76+
},
77+
78+
{
79+
code: javascript`
80+
import { cva,sva } from './panda/css';
81+
82+
function App(){
83+
const computedValue = "value"
84+
const heading = cva({
85+
variants: {
86+
[computedValue]: {
87+
color: "red.100",
88+
}
89+
}
90+
});
6791
}`,
6892
},
6993
]

0 commit comments

Comments
 (0)