Skip to content

Commit a2514d8

Browse files
committed
Fix linting on no panda attributes
1 parent 71dc947 commit a2514d8

File tree

7 files changed

+42
-21
lines changed

7 files changed

+42
-21
lines changed

plugin/src/rules/no-config-function-in-source.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ const rule: Rule = createRule({
2121
create(context) {
2222
return {
2323
CallExpression(node) {
24+
if (!isValidFile(context)) return
2425
if (!isIdentifier(node.callee)) return
2526
if (!CONFIG_FUNCTIONS.includes(node.callee.name)) return
2627

27-
if (!isValidFile(context)) return
28-
2928
context.report({
3029
node,
3130
messageId: 'configFunction',

plugin/src/rules/no-debug.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isIdentifier } from '../utils/nodes'
1+
import { isIdentifier, isJSXIdentifier } from '../utils/nodes'
22
import { type Rule, createRule } from '../utils'
33
import { isPandaAttribute, isPandaProp } from '../utils/helpers'
44

@@ -20,20 +20,19 @@ const rule: Rule = createRule({
2020
defaultOptions: [],
2121
create(context) {
2222
return {
23-
JSXIdentifier(node) {
24-
if (node.name !== 'debug') return
23+
JSXAttribute(node) {
24+
if (!isJSXIdentifier(node.name) || node.name.name !== 'debug') return
2525
if (!isPandaProp(node, context)) return
2626

2727
context.report({
2828
node,
2929
messageId: 'debug',
30-
fix: (fixer) => fixer.remove(node.parent),
30+
fix: (fixer) => fixer.remove(node),
3131
})
3232
},
3333

3434
Property(node) {
3535
if (!isIdentifier(node.key) || node.key.name !== 'debug') return
36-
3736
if (!isPandaAttribute(node, context)) return
3837

3938
context.report({

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const rule: Rule = createRule({
3333
)
3434
return
3535

36-
if (!isPandaProp(node.name, context)) return
36+
if (!isPandaProp(node, context)) return
3737

3838
context.report({
3939
node: node.value,

plugin/src/rules/no-shorthand-prop.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { isPandaAttribute, isPandaProp, resolveLonghand } from '../utils/helpers'
22
import { type Rule, createRule } from '../utils'
3-
import { isIdentifier } from '../utils/nodes'
3+
import { isIdentifier, isJSXIdentifier } from '../utils/nodes'
44

55
export const RULE_NAME = 'no-shorthand-prop'
66

@@ -37,12 +37,14 @@ const rule: Rule = createRule({
3737
}
3838

3939
return {
40-
JSXIdentifier(node) {
40+
JSXAttribute(node) {
41+
if (!isJSXIdentifier(node.name)) return
4142
if (!isPandaProp(node, context)) return
42-
const longhand = resolveLonghand(node.name, context)
43+
44+
const longhand = resolveLonghand(node.name.name, context)
4345
if (!longhand) return
4446

45-
sendReport(node, node.name)
47+
sendReport(node.name, node.name.name)
4648
},
4749

4850
Property(node) {

plugin/src/rules/prefer-atomic-properties.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { isPandaAttribute, isPandaProp, isValidProperty, resolveShorthand } from '../utils/helpers'
22
import { type Rule, createRule } from '../utils'
33
import { shorthandProperties } from '../utils/shorthand-properties'
4-
import { isIdentifier } from '../utils/nodes'
4+
import { isIdentifier, isJSXIdentifier } from '../utils/nodes'
55

66
export const RULE_NAME = 'prefer-atomic-properties'
77

@@ -42,12 +42,14 @@ const rule: Rule = createRule({
4242
}
4343

4444
return {
45-
JSXIdentifier(node) {
45+
JSXAttribute(node) {
46+
if (!isJSXIdentifier(node.name)) return
4647
if (!isPandaProp(node, context)) return
47-
const cpd = resolveCompositeProperty(node.name)
48+
49+
const cpd = resolveCompositeProperty(node.name.name)
4850
if (!cpd) return
4951

50-
sendReport(node, node.name)
52+
sendReport(node, node.name.name)
5153
},
5254

5355
Property(node) {

plugin/src/utils/helpers.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ export const isValidFile = (context: RuleContext<any, any>) => {
125125
return syncAction('isValidFile', getSyncOpts(context))
126126
}
127127

128+
export const isConfigFile = (context: RuleContext<any, any>) => {
129+
return syncAction('isConfigFile', getSyncOpts(context))
130+
}
131+
128132
export const isValidProperty = (name: string, context: RuleContext<any, any>) => {
129133
return syncAction('isValidProperty', getSyncOpts(context), name)
130134
}
@@ -134,7 +138,11 @@ export const isPandaImport = (node: TSESTree.ImportDeclaration, context: RuleCon
134138
return imports.some((imp) => imp.mod === node.source.value)
135139
}
136140

137-
export const isPandaProp = <T extends Node>(node: T, context: RuleContext<any, any>) => {
141+
export const isPandaProp = (node: TSESTree.JSXAttribute, context: RuleContext<any, any>) => {
142+
// Ensure prop is a styled prop
143+
const prop = node.name.name
144+
if (typeof prop !== 'string' || !isValidProperty(prop, context)) return
145+
138146
const jsxAncestor = getAncestor(isJSXOpeningElement, node)
139147

140148
if (!jsxAncestor) return
@@ -149,10 +157,11 @@ export const isPandaProp = <T extends Node>(node: T, context: RuleContext<any, a
149157

150158
// Ensure component is a panda component
151159
if (!isPandaIsh(jsxAncestor.name.name, context) && !isLocalStyledFactory(jsxAncestor, context)) return
160+
152161
return true
153162
}
154163

155-
export const isPandaAttribute = <T extends Node>(node: T, context: RuleContext<any, any>) => {
164+
export const isPandaAttribute = (node: TSESTree.Property, context: RuleContext<any, any>) => {
156165
const callAncestor = getAncestor(isCallExpression, node)
157166

158167
// Object could be in JSX prop value i.e css prop or a pseudo
@@ -161,12 +170,17 @@ export const isPandaAttribute = <T extends Node>(node: T, context: RuleContext<a
161170
const jsxAttrAncestor = getAncestor(isJSXAttribute, node)
162171

163172
if (!jsxExprAncestor || !jsxAttrAncestor) return
164-
if (!isPandaProp(jsxAttrAncestor.name, context)) return
173+
if (!isPandaProp(jsxAttrAncestor, context)) return
165174
if (!isValidStyledProp(jsxAttrAncestor.name, context)) return
166175

167176
return true
168177
}
169178

179+
// Ensure attribute is a styled attribute
180+
if (!isIdentifier(node.key)) return
181+
const attr = node.key.name
182+
if (!isValidProperty(attr, context)) return
183+
170184
// E.g. css({...})
171185
if (isIdentifier(callAncestor.callee)) {
172186
return isPandaIsh(callAncestor.callee.name, context)
@@ -224,6 +238,10 @@ export const getInvalidTokens = (value: string, context: RuleContext<any, any>)
224238
return syncAction('filterInvalidTokenz', getSyncOpts(context), tokens)
225239
}
226240

241+
export const getExtendWarnings = (context: RuleContext<any, any>) => {
242+
return syncAction('getExtendWarnings', getSyncOpts(context))
243+
}
244+
227245
export const getTokenImport = (context: RuleContext<any, any>) => {
228246
const imports = _getImports(context)
229247
return imports.find((imp) => imp.name === 'token')

sandbox/src/App.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function App() {
3636

3737
const color = 'red'
3838
const circleSize = '4'
39-
const text = '4px'
39+
const ta = 'left'
4040

4141
return (
4242
<div
@@ -48,9 +48,10 @@ function App() {
4848
color: '#111',
4949
backgroundColor: color,
5050
content: "['escape hatch']",
51-
textStyle: text,
51+
textAlign: ta,
5252
})}
5353
>
54+
<panda.a href={`mailto:${1}`} />
5455
<Circle size={circleSize} />
5556
<HStack gap="40px" debug>
5657
<div className={className}>Element 1</div>

0 commit comments

Comments
 (0)