Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix array syntax in no-dynamic-styling #143

Merged
merged 1 commit into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tender-chefs-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@pandacss/eslint-plugin': patch
---

Fix array syntax in `no-dynamic-styling`
13 changes: 13 additions & 0 deletions docs/rules/no-dynamic-styling.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ const styles = css({ bg: color });
```
```js

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

const size = '8';
const styles = css({ padding: ['4', size] });
```
```js

import { stack } from './panda/patterns';

const align = 'center';
Expand Down Expand Up @@ -74,6 +81,12 @@ const styles = css({ bg: 'gray.900' });
```
```js

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

const styles = css({ padding: ['4', '8'] });
```
```js

import { Circle } from './panda/jsx';

function App(){
Expand Down
21 changes: 21 additions & 0 deletions plugin/src/rules/no-dynamic-styling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { TSESTree } from '@typescript-eslint/utils'
import { type Rule, createRule } from '../utils'
import { isInPandaFunction, isPandaAttribute, isPandaProp, isRecipeVariant } from '../utils/helpers'
import {
isArrayExpression,
isIdentifier,
isJSXExpressionContainer,
isLiteral,
Expand Down Expand Up @@ -44,6 +45,9 @@ const rule: Rule = createRule({

// Don't warn for objects. Those are conditions
if (isObjectExpression(node.value.expression)) return
if (isArrayExpression(node.value.expression)) {
return checkElements(node.value.expression, context)
}

if (!isPandaProp(node, context)) return

Expand All @@ -53,6 +57,7 @@ const rule: Rule = createRule({
})
},

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

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

// Don't warn for objects. Those are conditions
if (isObjectExpression(node.value)) return
if (isArrayExpression(node.value)) {
return checkElements(node.value, context)
}

if (!isPandaAttribute(node, context)) return

Expand All @@ -83,4 +91,17 @@ const rule: Rule = createRule({
},
})

function checkElements(array: TSESTree.ArrayExpression, context: Parameters<(typeof rule)['create']>[0]) {
array.elements.forEach((node) => {
if (!node) return
if (isLiteral(node)) return
if (isTemplateLiteral(node) && node.expressions.length === 0) return

context.report({
node: node,
messageId: 'dynamic',
})
})
}

export default rule
2 changes: 2 additions & 0 deletions plugin/src/utils/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export const isLiteral = isNodeOfType(AST_NODE_TYPES.Literal)

export const isTemplateLiteral = isNodeOfType(AST_NODE_TYPES.TemplateLiteral)

export const isArrayExpression = isNodeOfType(AST_NODE_TYPES.ArrayExpression)

export const isObjectExpression = isNodeOfType(AST_NODE_TYPES.ObjectExpression)

export const isMemberExpression = isNodeOfType(AST_NODE_TYPES.MemberExpression)
Expand Down
15 changes: 15 additions & 0 deletions plugin/tests/no-dynamic-styling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import { css } from './panda/css';
const styles = css({ bg: 'gray.900' })`,
},

{
code: javascript`
import { css } from './panda/css';

const styles = css({ padding: ['4', '8'] })`,
},

{
code: javascript`
import { Circle } from './panda/jsx';
Expand Down Expand Up @@ -39,6 +46,14 @@ const color = 'red.100';
const styles = css({ bg: color })`,
},

{
code: javascript`
import { css } from './panda/css';

const size = '8';
const styles = css({ padding: ['4', size] })`,
},

{
code: javascript`
import { stack } from './panda/patterns';
Expand Down
3 changes: 3 additions & 0 deletions sandbox/v9/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ console.log(keyframes, literal)

const LocalFactoryComp = panda('button')

const pbe = '4'
function App() {
const className = css({
bg: 'red.100',
Expand All @@ -33,6 +34,7 @@ function App() {
marginTop: '{spacings.4} token(spacing.600)',
margin: '4',
pt: token('sizes.4'),
paddingBlockEnd: ['4', pbe],
})

const color = 'red'
Expand Down Expand Up @@ -69,6 +71,7 @@ function App() {
bg="red.200"
borderColor="red.500"
borderTopColor={'#111'}
paddingBlockEnd={['4', color]}
>
Element 2
</panda.div>
Expand Down
Loading