Skip to content

Commit 722368b

Browse files
committed
apply pr feedback
1 parent 25a81d9 commit 722368b

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

packages/eslint-config-widen-emotion/src/rules/no-conditional-css-prop.ts

+42-15
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
12
import { Rule } from 'eslint'
23

4+
function detectConditionalExpression(
5+
expression: any,
6+
context: Rule.RuleContext,
7+
node: any,
8+
) {
9+
if (expression) {
10+
if (
11+
expression.type === 'ConditionalExpression' ||
12+
(expression.type === 'LogicalExpression' &&
13+
(expression.operator === '&&' || expression.operator === '||'))
14+
) {
15+
context.report({
16+
message:
17+
'Avoid using conditionals within the css prop, move them to style.',
18+
node: node,
19+
})
20+
}
21+
}
22+
}
23+
324
export default {
425
create(context) {
526
return {
6-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
727
JSXAttribute(node: any) {
8-
if (
9-
node.name.name === 'css' &&
10-
node.value &&
11-
node.value.type === 'JSXExpressionContainer' &&
12-
(node.value.expression.type === 'ConditionalExpression' ||
13-
(node.value.expression.type === 'LogicalExpression' &&
14-
(node.value.expression.operator === '&&' ||
15-
node.value.expression.operator === '||')))
16-
) {
17-
context.report({
18-
message:
19-
'Avoid using conditionals within the css prop, move them to style.',
20-
node,
21-
})
28+
if (node.name.name === 'css') {
29+
if (node.value && node.value.type === 'JSXExpressionContainer') {
30+
const expression = node.value.expression
31+
32+
// Check if it's an array
33+
if (expression.type === 'ArrayExpression') {
34+
expression.elements.forEach((element: any) => {
35+
detectConditionalExpression(element, context, node)
36+
})
37+
} else {
38+
// Check single expressions
39+
detectConditionalExpression(expression, context, node)
40+
}
41+
}
2242
}
2343
},
2444
}
@@ -27,6 +47,13 @@ export default {
2747
docs: {
2848
category: 'Best Practices',
2949
description: 'Disallow conditionals within the css prop in components',
50+
example: `
51+
// Before
52+
<div css={[randomDivStyle, isRed ? {color: "red"} : null]} />
53+
54+
// After
55+
<div css={randomDivStyle} style={isRed ? {color: "red"}} />
56+
`,
3057
recommended: false,
3158
},
3259
schema: [],

packages/eslint-config-widen-emotion/test/no-conditional-css-prop.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ ruleTester.run('no-conditional-css-prop', rule, {
4242
},
4343
],
4444
},
45+
{
46+
code: '<div css={[{color: `red`}, true && {background: `black`}]} />',
47+
errors: [
48+
{
49+
message:
50+
'Avoid using conditionals within the css prop, move them to style.',
51+
},
52+
],
53+
},
4554
],
4655
valid: [
4756
{
@@ -53,5 +62,8 @@ ruleTester.run('no-conditional-css-prop', rule, {
5362
{
5463
code: '<div css={`color: ${someVariable}`} />',
5564
},
65+
{
66+
code: '<div css={[{color: `red`}, {background: `black`}]} />',
67+
},
5668
],
5769
})

0 commit comments

Comments
 (0)