-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
prefer-unified-property-style
rule
- Loading branch information
Showing
9 changed files
with
160 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@pandacss/eslint-plugin": patch | ||
--- | ||
|
||
Add `prefer-unified-property-style` rule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { isPandaAttribute, isPandaProp, isValidProperty, resolveLonghand } from '../utils/helpers' | ||
import { type Rule, createRule } from '../utils' | ||
import { compositeProperties } from '../utils/composite-properties' | ||
import { isIdentifier, isJSXIdentifier, isJSXOpeningElement, isObjectExpression } from '../utils/nodes' | ||
|
||
export const RULE_NAME = 'prefer-unified-property-style' | ||
|
||
const rule: Rule = createRule({ | ||
name: RULE_NAME, | ||
meta: { | ||
docs: { | ||
description: | ||
'Discourage against mixing atomic and composite forms of the same property in a style declaration. Atomic styles give more consistent results', | ||
}, | ||
messages: { | ||
unify: | ||
"You're mixing atomic {{atomicProperties}} with composite property {{composite}}. \nPrefer atomic styling to mixing atomic and composite properties. \nRemove `{{composite}}` and use one or more of {{atomics}} instead", | ||
}, | ||
type: 'suggestion', | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
const getLonghand = (name: string) => resolveLonghand(name, context) ?? name | ||
|
||
const resolveCompositeProperty = (name: string) => { | ||
if (Object.hasOwn(compositeProperties, name)) return name | ||
|
||
const longhand = getLonghand(name) | ||
if (isValidProperty(longhand, context) && Object.hasOwn(compositeProperties, longhand)) return longhand | ||
} | ||
|
||
const sendReport = (node: any, cmp: string, siblings: string[]) => { | ||
const _atomicProperties = siblings | ||
.filter((prop) => compositeProperties[cmp].includes(getLonghand(prop))) | ||
.map((prop) => `\`${prop}\``) | ||
if (!_atomicProperties.length) return | ||
|
||
const atomicProperties = _atomicProperties.join(', ') + (_atomicProperties.length === 1 ? ' style' : ' styles') | ||
const atomics = compositeProperties[cmp].map((name) => `\`${name}\``).join(', ') | ||
|
||
context.report({ | ||
node, | ||
messageId: 'unify', | ||
data: { | ||
composite: cmp, | ||
atomicProperties, | ||
atomics, | ||
}, | ||
}) | ||
} | ||
|
||
return { | ||
JSXAttribute(node) { | ||
if (!isJSXIdentifier(node.name)) return | ||
if (!isPandaProp(node, context)) return | ||
const cmp = resolveCompositeProperty(node.name.name) | ||
if (!cmp) return | ||
if (!isJSXOpeningElement(node.parent)) return | ||
|
||
const siblings = node.parent.attributes.map((attr: any) => attr.name.name) | ||
sendReport(node, cmp, siblings) | ||
}, | ||
|
||
Property(node) { | ||
if (!isIdentifier(node.key)) return | ||
if (!isPandaAttribute(node, context)) return | ||
const cmp = resolveCompositeProperty(node.key.name) | ||
if (!cmp) return | ||
if (!isObjectExpression(node.parent)) return | ||
|
||
const siblings = node.parent.properties.map((prop: any) => isIdentifier(prop.key) && prop.key.name) | ||
sendReport(node.key, cmp, siblings) | ||
}, | ||
} | ||
}, | ||
}) | ||
|
||
export default rule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { tester } from '../test-utils' | ||
import rule, { RULE_NAME } from '../src/rules/prefer-unified-property-style' | ||
|
||
const javascript = String.raw | ||
|
||
const valids = [ | ||
{ | ||
code: javascript` | ||
import { css } from './panda/css'; | ||
const styles = css({ borderColor: 'gray.900', borderWidth: '1px' })`, | ||
}, | ||
|
||
{ | ||
code: javascript` | ||
import { Circle } from './panda/jsx'; | ||
function App(){ | ||
return <Circle marginTop="2" marginRight="3" />; | ||
}`, | ||
}, | ||
] | ||
|
||
const invalids = [ | ||
{ | ||
code: javascript` | ||
import { css } from './panda/css'; | ||
const color = 'red.100'; | ||
const styles = css({ borderRadius:"lg", borderTopRightRadius: "0" })`, | ||
}, | ||
|
||
{ | ||
code: javascript` | ||
import { Circle } from './panda/jsx'; | ||
function App(){ | ||
const bool = true; | ||
return <Circle border="solid 1px" borderColor="gray.800" />; | ||
}`, | ||
}, | ||
] | ||
|
||
tester.run(RULE_NAME, rule, { | ||
valid: valids.map(({ code }) => ({ | ||
code, | ||
})), | ||
invalid: invalids.map(({ code }) => ({ | ||
code, | ||
errors: 1, | ||
})), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters