|
| 1 | +import { isPandaAttribute, isPandaProp, resolveLonghand } from '../utils/helpers' |
| 2 | +import { type Rule, createRule } from '../utils' |
| 3 | +import { isIdentifier, isJSXIdentifier } from '../utils/nodes' |
| 4 | +import { physicalProperties } from '../utils/physical-properties' |
| 5 | + |
| 6 | +export const RULE_NAME = 'no-physical-properties' |
| 7 | + |
| 8 | +const rule: Rule = createRule({ |
| 9 | + name: RULE_NAME, |
| 10 | + meta: { |
| 11 | + docs: { |
| 12 | + description: |
| 13 | + 'Encourage the use of [logical properties](https://mdn.io/logical-properties-basic-concepts) over physical proeprties, to foster a responsive and adaptable user interface.', |
| 14 | + }, |
| 15 | + messages: { |
| 16 | + physical: 'Use logical property of {{physical}} instead. Prefer `{{logical}}`', |
| 17 | + replace: 'Replace `{{physical}}` with `{{logical}}`', |
| 18 | + }, |
| 19 | + type: 'suggestion', |
| 20 | + hasSuggestions: true, |
| 21 | + schema: [], |
| 22 | + }, |
| 23 | + defaultOptions: [], |
| 24 | + create(context) { |
| 25 | + const getLonghand = (name: string) => resolveLonghand(name, context) ?? name |
| 26 | + |
| 27 | + const sendReport = (node: any, name: string) => { |
| 28 | + const logical = physicalProperties[getLonghand(name)] |
| 29 | + const longhand = resolveLonghand(name, context) |
| 30 | + |
| 31 | + return context.report({ |
| 32 | + node, |
| 33 | + messageId: 'physical', |
| 34 | + data: { |
| 35 | + physical: `\`${name}\`${longhand ? ` - \`${longhand}\`` : ''}`, |
| 36 | + logical, |
| 37 | + }, |
| 38 | + suggest: [ |
| 39 | + { |
| 40 | + messageId: 'replace', |
| 41 | + data: { |
| 42 | + physical: name, |
| 43 | + logical, |
| 44 | + }, |
| 45 | + fix: (fixer) => { |
| 46 | + return fixer.replaceTextRange(node.range, logical) |
| 47 | + }, |
| 48 | + }, |
| 49 | + ], |
| 50 | + }) |
| 51 | + } |
| 52 | + |
| 53 | + return { |
| 54 | + JSXAttribute(node) { |
| 55 | + if (!isJSXIdentifier(node.name)) return |
| 56 | + if (!isPandaProp(node, context)) return |
| 57 | + |
| 58 | + if (getLonghand(node.name.name) in physicalProperties) { |
| 59 | + sendReport(node.name, node.name.name) |
| 60 | + } |
| 61 | + }, |
| 62 | + |
| 63 | + Property(node) { |
| 64 | + if (!isIdentifier(node.key)) return |
| 65 | + if (!isPandaAttribute(node, context)) return |
| 66 | + |
| 67 | + if (getLonghand(node.key.name) in physicalProperties) { |
| 68 | + sendReport(node.key, node.key.name) |
| 69 | + } |
| 70 | + }, |
| 71 | + } |
| 72 | + }, |
| 73 | +}) |
| 74 | + |
| 75 | +export default rule |
0 commit comments