-
Notifications
You must be signed in to change notification settings - Fork 5
/
no-dynamic-styling.ts
116 lines (98 loc) · 3.32 KB
/
no-dynamic-styling.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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,
isObjectExpression,
isTemplateLiteral,
} from '../utils/nodes'
export const RULE_NAME = 'no-dynamic-styling'
const rule: Rule = createRule({
name: RULE_NAME,
meta: {
docs: {
description:
"Ensure users don't use dynamic styling. Prefer static styles, leverage CSS variables, or recipes for known dynamic styles.",
},
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: 'problem',
schema: [],
},
defaultOptions: [],
create(context) {
// Helper function to determine if a node represents a static value
function isStaticValue(node: TSESTree.Node | null | undefined): boolean {
if (!node) return false
if (isLiteral(node)) return true
if (isTemplateLiteral(node) && node.expressions.length === 0) return true
if (isObjectExpression(node)) return true // Conditions are acceptable
return false
}
// Function to check array elements for dynamic values
function checkArrayElements(array: TSESTree.ArrayExpression) {
array.elements.forEach((element) => {
if (!element) return
if (isStaticValue(element)) return
context.report({
node: element,
messageId: 'dynamic',
})
})
}
return {
// JSX Attributes
JSXAttribute(node: TSESTree.JSXAttribute) {
if (!node.value) return
if (isLiteral(node.value)) return
// Check if it's a Panda prop early to avoid unnecessary processing
if (!isPandaProp(node, context)) return
if (isJSXExpressionContainer(node.value)) {
const expr = node.value.expression
if (isStaticValue(expr)) return
if (isArrayExpression(expr)) {
checkArrayElements(expr)
return
}
}
// Report dynamic value usage
context.report({
node: node.value,
messageId: 'dynamic',
})
},
// Dynamic properties with computed keys
'Property[computed=true]': (node: TSESTree.Property) => {
if (!isInPandaFunction(node, context)) return
context.report({
node: node.key,
messageId: isRecipeVariant(node, context) ? 'dynamicRecipeVariant' : 'dynamicProperty',
})
},
// Object Properties
Property(node: TSESTree.Property) {
if (!isIdentifier(node.key)) return
// Check if it's a Panda attribute early to avoid unnecessary processing
if (!isPandaAttribute(node, context)) return
if (isRecipeVariant(node, context)) return
if (isStaticValue(node.value)) return
if (isArrayExpression(node.value)) {
checkArrayElements(node.value)
return
}
// Report dynamic value usage
context.report({
node: node.value,
messageId: 'dynamic',
})
},
}
},
})
export default rule