-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathno-physical-properties.ts
209 lines (183 loc) · 6.42 KB
/
no-physical-properties.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { isRecipeVariant, isPandaAttribute, isPandaProp, resolveLonghand } from '../utils/helpers'
import { type Rule, createRule } from '../utils'
import { isIdentifier, isJSXIdentifier, isLiteral, isJSXExpressionContainer } from '../utils/nodes'
import { physicalProperties, physicalPropertyValues } from '../utils/physical-properties'
import type { TSESTree, TSESLint } from '@typescript-eslint/utils'
type CacheMap<K extends object, V> = WeakMap<K, V | undefined>
type ValueNode = TSESTree.Property['value'] | TSESTree.JSXAttribute['value']
type IdentifierNode = TSESTree.Identifier | TSESTree.JSXIdentifier
type RuleContextType = TSESLint.RuleContext<keyof typeof MESSAGES, [{ whitelist: string[] }]>
export const RULE_NAME = 'no-physical-properties'
const MESSAGES = {
physical: 'Use logical property instead of {{physical}}. Prefer `{{logical}}`.',
physicalValue: 'Use logical value instead of {{physical}}. Prefer `{{logical}}`.',
replace: 'Replace `{{physical}}` with `{{logical}}`.',
} as const
class PropertyCache {
private longhandCache = new Map<string, string>()
private pandaPropCache: CacheMap<TSESTree.JSXAttribute, boolean> = new WeakMap()
private pandaAttributeCache: CacheMap<TSESTree.Property, boolean> = new WeakMap()
private recipeVariantCache: CacheMap<TSESTree.Property, boolean> = new WeakMap()
getLonghand(name: string, context: RuleContextType): string {
if (this.longhandCache.has(name)) {
return this.longhandCache.get(name)!
}
const longhand = resolveLonghand(name, context) ?? name
this.longhandCache.set(name, longhand)
return longhand
}
isPandaProp(node: TSESTree.JSXAttribute, context: RuleContextType): boolean {
if (this.pandaPropCache.has(node)) {
return this.pandaPropCache.get(node)!
}
const result = isPandaProp(node, context)
this.pandaPropCache.set(node, result)
return !!result
}
isPandaAttribute(node: TSESTree.Property, context: RuleContextType): boolean {
if (this.pandaAttributeCache.has(node)) {
return this.pandaAttributeCache.get(node)!
}
const result = isPandaAttribute(node, context)
this.pandaAttributeCache.set(node, result)
return !!result
}
isRecipeVariant(node: TSESTree.Property, context: RuleContextType): boolean {
if (this.recipeVariantCache.has(node)) {
return this.recipeVariantCache.get(node)!
}
const result = isRecipeVariant(node, context)
this.recipeVariantCache.set(node, result)
return !!result
}
}
const extractStringLiteralValue = (valueNode: ValueNode): string | null => {
if (isLiteral(valueNode) && typeof valueNode.value === 'string') {
return valueNode.value
}
if (
isJSXExpressionContainer(valueNode) &&
isLiteral(valueNode.expression) &&
typeof valueNode.expression.value === 'string'
) {
return valueNode.expression.value
}
return null
}
const createPropertyReport = (
node: IdentifierNode,
longhandName: string,
logical: string,
context: RuleContextType,
) => {
const physicalName = `\`${node.name}\`${longhandName !== node.name ? ` (resolved to \`${longhandName}\`)` : ''}`
context.report({
node,
messageId: 'physical',
data: { physical: physicalName, logical },
suggest: [
{
messageId: 'replace',
data: { physical: node.name, logical },
fix: (fixer: TSESLint.RuleFixer) => fixer.replaceText(node, logical),
},
],
})
}
const createValueReport = (
valueNode: NonNullable<ValueNode>,
valueText: string,
logical: string,
context: RuleContextType,
) => {
context.report({
node: valueNode,
messageId: 'physicalValue',
data: { physical: `"${valueText}"`, logical: `"${logical}"` },
suggest: [
{
messageId: 'replace',
data: { physical: `"${valueText}"`, logical: `"${logical}"` },
fix: (fixer: TSESLint.RuleFixer) => {
if (isLiteral(valueNode)) {
return fixer.replaceText(valueNode, `"${logical}"`)
}
if (isJSXExpressionContainer(valueNode) && isLiteral(valueNode.expression)) {
return fixer.replaceText(valueNode.expression, `"${logical}"`)
}
return null
},
},
],
})
}
const rule: Rule = createRule({
name: RULE_NAME,
meta: {
docs: {
description:
'Encourage the use of logical properties over physical properties to foster a responsive and adaptable user interface.',
},
messages: MESSAGES,
type: 'suggestion',
hasSuggestions: true,
schema: [
{
type: 'object',
properties: {
whitelist: {
type: 'array',
items: {
type: 'string',
minLength: 0,
},
uniqueItems: true,
},
},
additionalProperties: false,
},
],
},
defaultOptions: [{ whitelist: [] }],
create(context) {
const whitelist: string[] = context.options[0]?.whitelist ?? []
const cache = new PropertyCache()
const checkPropertyName = (node: IdentifierNode) => {
if (whitelist.includes(node.name)) return
const longhandName = cache.getLonghand(node.name, context)
if (!(longhandName in physicalProperties)) return
const logical = physicalProperties[longhandName]
createPropertyReport(node, longhandName, logical, context)
}
const checkPropertyValue = (keyNode: IdentifierNode, valueNode: NonNullable<ValueNode>): boolean => {
const propName = keyNode.name
if (!(propName in physicalPropertyValues)) return false
const valueText = extractStringLiteralValue(valueNode)
if (valueText === null) return false
const valueMap = physicalPropertyValues[propName]
if (!valueMap[valueText]) return false
createValueReport(valueNode, valueText, valueMap[valueText], context)
return true
}
return {
JSXAttribute(node: TSESTree.JSXAttribute) {
if (!isJSXIdentifier(node.name)) return
if (!cache.isPandaProp(node, context)) return
checkPropertyName(node.name)
if (node.value) {
checkPropertyValue(node.name, node.value)
}
},
Property(node: TSESTree.Property) {
if (!isIdentifier(node.key)) return
if (!cache.isPandaAttribute(node, context)) return
if (cache.isRecipeVariant(node, context)) return
checkPropertyName(node.key)
if (node.value) {
checkPropertyValue(node.key, node.value)
}
},
}
},
})
export default rule