Skip to content

Commit 7dbd78e

Browse files
authored
chore: fix array syntax in no-dynamic-styling (#143)
1 parent 0371d96 commit 7dbd78e

File tree

6 files changed

+59
-0
lines changed

6 files changed

+59
-0
lines changed

.changeset/tender-chefs-help.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@pandacss/eslint-plugin': patch
3+
---
4+
5+
Fix array syntax in `no-dynamic-styling`

docs/rules/no-dynamic-styling.md

+13
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ const styles = css({ bg: color });
2020
```
2121
```js
2222

23+
import { css } from './panda/css';
24+
25+
const size = '8';
26+
const styles = css({ padding: ['4', size] });
27+
```
28+
```js
29+
2330
import { stack } from './panda/patterns';
2431

2532
const align = 'center';
@@ -74,6 +81,12 @@ const styles = css({ bg: 'gray.900' });
7481
```
7582
```js
7683

84+
import { css } from './panda/css';
85+
86+
const styles = css({ padding: ['4', '8'] });
87+
```
88+
```js
89+
7790
import { Circle } from './panda/jsx';
7891

7992
function App(){

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

+21
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { TSESTree } from '@typescript-eslint/utils'
22
import { type Rule, createRule } from '../utils'
33
import { isInPandaFunction, isPandaAttribute, isPandaProp, isRecipeVariant } from '../utils/helpers'
44
import {
5+
isArrayExpression,
56
isIdentifier,
67
isJSXExpressionContainer,
78
isLiteral,
@@ -44,6 +45,9 @@ const rule: Rule = createRule({
4445

4546
// Don't warn for objects. Those are conditions
4647
if (isObjectExpression(node.value.expression)) return
48+
if (isArrayExpression(node.value.expression)) {
49+
return checkElements(node.value.expression, context)
50+
}
4751

4852
if (!isPandaProp(node, context)) return
4953

@@ -53,6 +57,7 @@ const rule: Rule = createRule({
5357
})
5458
},
5559

60+
// Dynamic properties
5661
'Property[computed=true]'(node: TSESTree.Property) {
5762
if (!isInPandaFunction(node, context)) return
5863

@@ -71,6 +76,9 @@ const rule: Rule = createRule({
7176

7277
// Don't warn for objects. Those are conditions
7378
if (isObjectExpression(node.value)) return
79+
if (isArrayExpression(node.value)) {
80+
return checkElements(node.value, context)
81+
}
7482

7583
if (!isPandaAttribute(node, context)) return
7684

@@ -83,4 +91,17 @@ const rule: Rule = createRule({
8391
},
8492
})
8593

94+
function checkElements(array: TSESTree.ArrayExpression, context: Parameters<(typeof rule)['create']>[0]) {
95+
array.elements.forEach((node) => {
96+
if (!node) return
97+
if (isLiteral(node)) return
98+
if (isTemplateLiteral(node) && node.expressions.length === 0) return
99+
100+
context.report({
101+
node: node,
102+
messageId: 'dynamic',
103+
})
104+
})
105+
}
106+
86107
export default rule

plugin/src/utils/nodes.ts

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export const isLiteral = isNodeOfType(AST_NODE_TYPES.Literal)
1111

1212
export const isTemplateLiteral = isNodeOfType(AST_NODE_TYPES.TemplateLiteral)
1313

14+
export const isArrayExpression = isNodeOfType(AST_NODE_TYPES.ArrayExpression)
15+
1416
export const isObjectExpression = isNodeOfType(AST_NODE_TYPES.ObjectExpression)
1517

1618
export const isMemberExpression = isNodeOfType(AST_NODE_TYPES.MemberExpression)

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

+15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ import { css } from './panda/css';
1111
const styles = css({ bg: 'gray.900' })`,
1212
},
1313

14+
{
15+
code: javascript`
16+
import { css } from './panda/css';
17+
18+
const styles = css({ padding: ['4', '8'] })`,
19+
},
20+
1421
{
1522
code: javascript`
1623
import { Circle } from './panda/jsx';
@@ -39,6 +46,14 @@ const color = 'red.100';
3946
const styles = css({ bg: color })`,
4047
},
4148

49+
{
50+
code: javascript`
51+
import { css } from './panda/css';
52+
53+
const size = '8';
54+
const styles = css({ padding: ['4', size] })`,
55+
},
56+
4257
{
4358
code: javascript`
4459
import { stack } from './panda/patterns';

sandbox/v9/src/App.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ console.log(keyframes, literal)
2424

2525
const LocalFactoryComp = panda('button')
2626

27+
const pbe = '4'
2728
function App() {
2829
const className = css({
2930
bg: 'red.100',
@@ -33,6 +34,7 @@ function App() {
3334
marginTop: '{spacings.4} token(spacing.600)',
3435
margin: '4',
3536
pt: token('sizes.4'),
37+
paddingBlockEnd: ['4', pbe],
3638
})
3739

3840
const color = 'red'
@@ -69,6 +71,7 @@ function App() {
6971
bg="red.200"
7072
borderColor="red.500"
7173
borderTopColor={'#111'}
74+
paddingBlockEnd={['4', color]}
7275
>
7376
Element 2
7477
</panda.div>

0 commit comments

Comments
 (0)