-
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
no-physical-properties
rule
- Loading branch information
Showing
13 changed files
with
254 additions
and
3 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 `no-physical-properties` 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,60 @@ | ||
[//]: # (This file is generated by eslint-docgen. Do not edit it directly.) | ||
|
||
# no-physical-properties | ||
|
||
Encourage the use of [logical properties](https://mdn.io/logical-properties-basic-concepts) over physical proeprties, to foster a responsive and adaptable user interface. | ||
|
||
📋 This rule is enabled in `plugin:@pandacss/all`. | ||
|
||
## Rule details | ||
|
||
❌ Examples of **incorrect** code: | ||
```js | ||
import { css } from './panda/css'; | ||
|
||
const styles = css({ left: '0' }); | ||
``` | ||
```js | ||
|
||
import { css } from './panda/css'; | ||
|
||
function App(){ | ||
return <div className={css({ marginLeft: '4' })} />; | ||
}; | ||
``` | ||
```js | ||
|
||
import { Circle } from './panda/jsx'; | ||
|
||
function App(){ | ||
return <Circle _hover={{ borderBottom: 'solid 1px' }} />; | ||
} | ||
``` | ||
|
||
✔️ Examples of **correct** code: | ||
```js | ||
import { css } from './panda/css'; | ||
|
||
const styles = css({ insetInlineStart: '0' }); | ||
``` | ||
```js | ||
|
||
import { css } from './panda/css'; | ||
|
||
function App(){ | ||
return <div className={css({ marginInlineStart: '4' })} />; | ||
}; | ||
``` | ||
```js | ||
|
||
import { Circle } from './panda/jsx'; | ||
|
||
function App(){ | ||
return <Circle _hover={{ borderBlockEnd: 'solid 1px' }} />; | ||
} | ||
``` | ||
|
||
## Resources | ||
|
||
* [Rule source](/plugin/src/rules/no-physical-properties.ts) | ||
* [Test source](/tests/no-physical-properties.test.ts) |
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,75 @@ | ||
import { isPandaAttribute, isPandaProp, resolveLonghand } from '../utils/helpers' | ||
import { type Rule, createRule } from '../utils' | ||
import { isIdentifier, isJSXIdentifier } from '../utils/nodes' | ||
import { physicalProperties } from '../utils/physical-properties' | ||
|
||
export const RULE_NAME = 'no-physical-properties' | ||
|
||
const rule: Rule = createRule({ | ||
name: RULE_NAME, | ||
meta: { | ||
docs: { | ||
description: | ||
'Encourage the use of [logical properties](https://mdn.io/logical-properties-basic-concepts) over physical proeprties, to foster a responsive and adaptable user interface.', | ||
}, | ||
messages: { | ||
physical: 'Use logical property of {{physical}} instead. Prefer `{{logical}}`', | ||
replace: 'Replace `{{physical}}` with `{{logical}}`', | ||
}, | ||
type: 'suggestion', | ||
hasSuggestions: true, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
const getLonghand = (name: string) => resolveLonghand(name, context) ?? name | ||
|
||
const sendReport = (node: any, name: string) => { | ||
const logical = physicalProperties[getLonghand(name)] | ||
const longhand = resolveLonghand(name, context) | ||
|
||
return context.report({ | ||
node, | ||
messageId: 'physical', | ||
data: { | ||
physical: `\`${name}\`${longhand ? ` - \`${longhand}\`` : ''}`, | ||
logical, | ||
}, | ||
suggest: [ | ||
{ | ||
messageId: 'replace', | ||
data: { | ||
physical: name, | ||
logical, | ||
}, | ||
fix: (fixer) => { | ||
return fixer.replaceTextRange(node.range, logical) | ||
}, | ||
}, | ||
], | ||
}) | ||
} | ||
|
||
return { | ||
JSXAttribute(node) { | ||
if (!isJSXIdentifier(node.name)) return | ||
if (!isPandaProp(node, context)) return | ||
|
||
if (getLonghand(node.name.name) in physicalProperties) { | ||
sendReport(node.name, node.name.name) | ||
} | ||
}, | ||
|
||
Property(node) { | ||
if (!isIdentifier(node.key)) return | ||
if (!isPandaAttribute(node, context)) return | ||
|
||
if (getLonghand(node.key.name) in physicalProperties) { | ||
sendReport(node.key, node.key.name) | ||
} | ||
}, | ||
} | ||
}, | ||
}) | ||
|
||
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
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,34 @@ | ||
export const physicalProperties: Record<string, string> = { | ||
borderBottom: 'borderBlockEnd', | ||
borderBottomColor: 'borderBlockEndColor', | ||
borderBottomStyle: 'borderBlockEndStyle', | ||
borderBottomWidth: 'borderBlockEndWidth', | ||
borderTop: 'borderBlockStart', | ||
borderTopColor: 'borderBlockStartColor', | ||
borderTopStyle: 'borderBlockStartStyle', | ||
borderTopWidth: 'borderBlockStartWidth', | ||
borderRight: 'borderInlineEnd', | ||
borderRightColor: 'borderInlineEndColor', | ||
borderRightStyle: 'borderInlineEndStyle', | ||
borderRightWidth: 'borderInlineEndWidth', | ||
borderLeft: 'borderInlineStart', | ||
borderLeftColor: 'borderInlineStartColor', | ||
borderLeftStyle: 'borderInlineStartStyle', | ||
borderLeftWidth: 'borderInlineStartWidth', | ||
borderTopLeftRadius: 'borderStartStartRadius', | ||
borderBottomLeftRadius: 'borderEndStartRadius', | ||
borderTopRightRadius: 'borderStartEndRadius', | ||
borderBottomRightRadius: 'borderEndEndRadius', | ||
marginBottom: 'marginBlockEnd', | ||
marginTop: 'marginBlockStart', | ||
marginRight: 'marginInlineEnd', | ||
marginLeft: 'marginInlineStart', | ||
paddingBottom: 'paddingBlockEnd', | ||
paddingTop: 'paddingBlockStart', | ||
paddingRight: 'paddingInlineEnd', | ||
paddingLeft: 'paddingInlineStart', | ||
left: 'insetInlineStart', | ||
right: 'insetInlineEnd', | ||
top: 'insetBlockStart', | ||
bottom: 'insetBlockEnd', | ||
} |
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,68 @@ | ||
import { tester } from '../test-utils' | ||
import rule, { RULE_NAME } from '../src/rules/no-physical-properties' | ||
|
||
const javascript = String.raw | ||
|
||
const valids = [ | ||
{ | ||
code: javascript` | ||
import { css } from './panda/css'; | ||
const styles = css({ insetInlineStart: '0' })`, | ||
}, | ||
|
||
{ | ||
code: javascript` | ||
import { css } from './panda/css'; | ||
function App(){ | ||
return <div className={css({ marginInlineStart: '4' })} />; | ||
}`, | ||
}, | ||
|
||
{ | ||
code: javascript` | ||
import { Circle } from './panda/jsx'; | ||
function App(){ | ||
return <Circle _hover={{ borderBlockEnd: 'solid 1px' }} />; | ||
}`, | ||
}, | ||
] | ||
|
||
const invalids = [ | ||
{ | ||
code: javascript` | ||
import { css } from './panda/css'; | ||
const styles = css({ left: '0' })`, | ||
}, | ||
|
||
{ | ||
code: javascript` | ||
import { css } from './panda/css'; | ||
function App(){ | ||
return <div className={css({ marginLeft: '4' })} />; | ||
}`, | ||
}, | ||
|
||
{ | ||
code: javascript` | ||
import { Circle } from './panda/jsx'; | ||
function App(){ | ||
return <Circle _hover={{ borderBottom: 'solid 1px' }} />; | ||
}`, | ||
}, | ||
] | ||
|
||
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