|
| 1 | +import { isPandaAttribute, isPandaProp, isValidProperty, resolveLonghand } from '../utils/helpers' |
| 2 | +import { type Rule, createRule } from '../utils' |
| 3 | +import { compositeProperties } from '../utils/composite-properties' |
| 4 | +import { isIdentifier, isJSXIdentifier, isJSXOpeningElement, isObjectExpression } from '../utils/nodes' |
| 5 | + |
| 6 | +export const RULE_NAME = 'prefer-unified-property-style' |
| 7 | + |
| 8 | +const rule: Rule = createRule({ |
| 9 | + name: RULE_NAME, |
| 10 | + meta: { |
| 11 | + docs: { |
| 12 | + description: |
| 13 | + 'Discourage against mixing atomic and composite forms of the same property in a style declaration. Atomic styles give more consistent results', |
| 14 | + }, |
| 15 | + messages: { |
| 16 | + unify: |
| 17 | + "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", |
| 18 | + }, |
| 19 | + type: 'suggestion', |
| 20 | + schema: [], |
| 21 | + }, |
| 22 | + defaultOptions: [], |
| 23 | + create(context) { |
| 24 | + const getLonghand = (name: string) => resolveLonghand(name, context) ?? name |
| 25 | + |
| 26 | + const resolveCompositeProperty = (name: string) => { |
| 27 | + if (Object.hasOwn(compositeProperties, name)) return name |
| 28 | + |
| 29 | + const longhand = getLonghand(name) |
| 30 | + if (isValidProperty(longhand, context) && Object.hasOwn(compositeProperties, longhand)) return longhand |
| 31 | + } |
| 32 | + |
| 33 | + const sendReport = (node: any, cmp: string, siblings: string[]) => { |
| 34 | + const _atomicProperties = siblings |
| 35 | + .filter((prop) => compositeProperties[cmp].includes(getLonghand(prop))) |
| 36 | + .map((prop) => `\`${prop}\``) |
| 37 | + if (!_atomicProperties.length) return |
| 38 | + |
| 39 | + const atomicProperties = _atomicProperties.join(', ') + (_atomicProperties.length === 1 ? ' style' : ' styles') |
| 40 | + const atomics = compositeProperties[cmp].map((name) => `\`${name}\``).join(', ') |
| 41 | + |
| 42 | + context.report({ |
| 43 | + node, |
| 44 | + messageId: 'unify', |
| 45 | + data: { |
| 46 | + composite: cmp, |
| 47 | + atomicProperties, |
| 48 | + atomics, |
| 49 | + }, |
| 50 | + }) |
| 51 | + } |
| 52 | + |
| 53 | + return { |
| 54 | + JSXAttribute(node) { |
| 55 | + if (!isJSXIdentifier(node.name)) return |
| 56 | + if (!isPandaProp(node, context)) return |
| 57 | + const cmp = resolveCompositeProperty(node.name.name) |
| 58 | + if (!cmp) return |
| 59 | + if (!isJSXOpeningElement(node.parent)) return |
| 60 | + |
| 61 | + const siblings = node.parent.attributes.map((attr: any) => attr.name.name) |
| 62 | + sendReport(node, cmp, siblings) |
| 63 | + }, |
| 64 | + |
| 65 | + Property(node) { |
| 66 | + if (!isIdentifier(node.key)) return |
| 67 | + if (!isPandaAttribute(node, context)) return |
| 68 | + const cmp = resolveCompositeProperty(node.key.name) |
| 69 | + if (!cmp) return |
| 70 | + if (!isObjectExpression(node.parent)) return |
| 71 | + |
| 72 | + const siblings = node.parent.properties.map((prop: any) => isIdentifier(prop.key) && prop.key.name) |
| 73 | + sendReport(node.key, cmp, siblings) |
| 74 | + }, |
| 75 | + } |
| 76 | + }, |
| 77 | +}) |
| 78 | + |
| 79 | +export default rule |
0 commit comments