-
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.
- Loading branch information
Showing
7 changed files
with
249 additions
and
4 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-important` 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-important | ||
|
||
Disallow usage of important keyword. Prioroitize specificity for a maintainable and predictable styling structure. | ||
|
||
📋 This rule is enabled in `plugin:@pandacss/all`. | ||
|
||
## Rule details | ||
|
||
❌ Examples of **incorrect** code: | ||
```js | ||
import { css } from './panda/css'; | ||
|
||
const styles = css({ marginLeft: '4px!' }); | ||
``` | ||
```js | ||
|
||
import { css } from './panda/css'; | ||
|
||
function App(){ | ||
return <div className={css({ background: '#111 !important' })} />; | ||
}; | ||
``` | ||
```js | ||
|
||
import { Circle } from './panda/jsx'; | ||
|
||
function App(){ | ||
return <Circle _hover={{ position: 'absolute !' }} />; | ||
} | ||
``` | ||
|
||
✔️ Examples of **correct** code: | ||
```js | ||
import { css } from './panda/css'; | ||
|
||
const styles = css({ marginLeft: '4' }); | ||
``` | ||
```js | ||
|
||
import { css } from './panda/css'; | ||
|
||
function App(){ | ||
return <div className={css({ background: 'red.100' })} />; | ||
}; | ||
``` | ||
```js | ||
|
||
import { Circle } from './panda/jsx'; | ||
|
||
function App(){ | ||
return <Circle _hover={{ position: 'absolute' }} />; | ||
} | ||
``` | ||
|
||
## Resources | ||
|
||
* [Rule source](/plugin/src/rules/no-important.ts) | ||
* [Test source](/tests/no-important.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,109 @@ | ||
import { isPandaAttribute, isPandaProp } from '../utils/helpers' | ||
import { type Rule, createRule } from '../utils' | ||
import { isIdentifier, isJSXExpressionContainer, isLiteral, isTemplateLiteral, type Node } from '../utils/nodes' | ||
|
||
// Check if the string ends with '!' with optional whitespace before it | ||
const exclamationRegex = /\s*!$/ | ||
// Check if the string ends with '!important' with optional whitespace before it and after, but not within '!important' | ||
const importantRegex = /\s*!important\s*$/ | ||
|
||
export const RULE_NAME = 'no-important' | ||
|
||
const rule: Rule = createRule({ | ||
name: RULE_NAME, | ||
meta: { | ||
docs: { | ||
description: | ||
'Disallow usage of important keyword. Prioroitize specificity for a maintainable and predictable styling structure.', | ||
}, | ||
messages: { | ||
important: | ||
'Avoid using the !important keyword. Refactor your code to prioritize specificity for predictable styling.', | ||
remove: 'Remove the `{{keyword}}` keyword.', | ||
}, | ||
type: 'suggestion', | ||
hasSuggestions: true, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
const removeQuotes = ([start, end]: readonly [number, number]) => [start + 1, end - 1] as const | ||
|
||
const hasImportantKeyword = (value?: string) => { | ||
if (!value) return false | ||
return exclamationRegex.test(value) || importantRegex.test(value) | ||
} | ||
|
||
const removeImportantKeyword = (input: string) => { | ||
if (exclamationRegex.test(input)) { | ||
// Remove trailing '!' | ||
return { fixed: input.replace(exclamationRegex, ''), keyword: '!' } | ||
} else if (importantRegex.test(input)) { | ||
// Remove '!important' with optional whitespace | ||
return { fixed: input.replace(importantRegex, ''), keyword: '!important' } | ||
} else { | ||
// No match, return the original string | ||
return { fixed: input, keyword: null } | ||
} | ||
} | ||
|
||
const handleLiteral = (node: Node) => { | ||
if (!isLiteral(node)) return | ||
if (!hasImportantKeyword(node.value?.toString())) return | ||
|
||
sendReport(node) | ||
} | ||
|
||
const handleTemplateLiteral = (node: Node) => { | ||
if (!isTemplateLiteral(node)) return | ||
if (node.expressions.length > 0) return | ||
if (!hasImportantKeyword(node.quasis[0].value.raw)) return | ||
|
||
sendReport(node.quasis[0], node.quasis[0].value.raw) | ||
} | ||
|
||
const sendReport = (node: any, _value?: string) => { | ||
const value = _value ?? node.value?.toString() | ||
const { keyword, fixed } = removeImportantKeyword(value) | ||
|
||
return context.report({ | ||
node, | ||
messageId: 'important', | ||
suggest: [ | ||
{ | ||
messageId: 'remove', | ||
data: { keyword }, | ||
fix: (fixer) => { | ||
return fixer.replaceTextRange(removeQuotes(node.range), fixed) | ||
}, | ||
}, | ||
], | ||
}) | ||
} | ||
|
||
return { | ||
JSXAttribute(node) { | ||
if (!node.value) return | ||
if (!isPandaProp(node, context)) return | ||
|
||
handleLiteral(node.value) | ||
|
||
if (!isJSXExpressionContainer(node.value)) return | ||
|
||
handleLiteral(node.value.expression) | ||
handleTemplateLiteral(node.value.expression) | ||
}, | ||
|
||
Property(node) { | ||
if (!isIdentifier(node.key)) return | ||
if (!isLiteral(node.value) && !isTemplateLiteral(node.value)) return | ||
if (!isPandaAttribute(node, context)) return | ||
|
||
handleLiteral(node.value) | ||
handleTemplateLiteral(node.value) | ||
}, | ||
} | ||
}, | ||
}) | ||
|
||
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
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-important' | ||
|
||
const javascript = String.raw | ||
|
||
const valids = [ | ||
{ | ||
code: javascript` | ||
import { css } from './panda/css'; | ||
const styles = css({ marginLeft: '4' })`, | ||
}, | ||
|
||
{ | ||
code: javascript` | ||
import { css } from './panda/css'; | ||
function App(){ | ||
return <div className={css({ background: 'red.100' })} />; | ||
}`, | ||
}, | ||
|
||
{ | ||
code: javascript` | ||
import { Circle } from './panda/jsx'; | ||
function App(){ | ||
return <Circle _hover={{ position: 'absolute' }} />; | ||
}`, | ||
}, | ||
] | ||
|
||
const invalids = [ | ||
{ | ||
code: javascript` | ||
import { css } from './panda/css'; | ||
const styles = css({ marginLeft: '4px!' })`, | ||
}, | ||
|
||
{ | ||
code: javascript` | ||
import { css } from './panda/css'; | ||
function App(){ | ||
return <div className={css({ background: '#111 !important' })} />; | ||
}`, | ||
}, | ||
|
||
{ | ||
code: javascript` | ||
import { Circle } from './panda/jsx'; | ||
function App(){ | ||
return <Circle _hover={{ position: 'absolute !' }} />; | ||
}`, | ||
}, | ||
] | ||
|
||
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