-
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
6 changed files
with
150 additions
and
13 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-invalid-nesting` 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { isIdentifier, isLiteral, isObjectExpression, isTemplateLiteral } from '../utils/nodes' | ||
import { type Rule, createRule } from '../utils' | ||
import { isInJSXProp, isInPandaFunction } from '../utils/helpers' | ||
|
||
export const RULE_NAME = 'no-invalid-nesting' | ||
|
||
const rule: Rule = createRule({ | ||
name: RULE_NAME, | ||
meta: { | ||
docs: { | ||
description: "Warn against invalid nesting. i.e. nested styles that don't contain the `&` character.", | ||
}, | ||
messages: { | ||
nesting: 'Invalid style nesting. Nested styles must contain the `&` character.', | ||
}, | ||
type: 'suggestion', | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
Property(node) { | ||
if (!isObjectExpression(node.value) || isIdentifier(node.key)) return | ||
if (!isInPandaFunction(node, context) && !isInJSXProp(node, context)) return | ||
|
||
const invalidLiteral = | ||
isLiteral(node.key) && typeof node.key.value === 'string' && !node.key.value.includes('&') | ||
const invalidTemplateLiteral = isTemplateLiteral(node.key) && !node.key.quasis[0].value.raw.includes('&') | ||
|
||
if (invalidLiteral || invalidTemplateLiteral) { | ||
context.report({ | ||
node: node.key, | ||
messageId: 'nesting', | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
}) | ||
|
||
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,68 @@ | ||
import { tester } from '../test-utils' | ||
import rule, { RULE_NAME } from '../src/rules/no-invalid-nesting' | ||
|
||
const javascript = String.raw | ||
|
||
const valids = [ | ||
{ | ||
code: javascript` | ||
import { css } from './panda/css'; | ||
const styles = css({ '&:hover': { marginLeft: '4px' } })`, | ||
}, | ||
|
||
{ | ||
code: javascript` | ||
import { css } from './panda/css'; | ||
function App(){ | ||
return <div className={css({ '.dark &': { background: 'red.100' } })} />; | ||
}`, | ||
}, | ||
|
||
{ | ||
code: javascript` | ||
import { Circle } from './panda/jsx'; | ||
function App(){ | ||
return <Circle css={{ '&[data-focus]': { position: 'absolute' } }} />; | ||
}`, | ||
}, | ||
] | ||
|
||
const invalids = [ | ||
{ | ||
code: javascript` | ||
import { css } from './panda/css'; | ||
const styles = css({ ':hover': { marginLeft: '4px' } })`, | ||
}, | ||
|
||
{ | ||
code: javascript` | ||
import { css } from './panda/css'; | ||
function App(){ | ||
return <div className={css({ '.dark ': { background: 'red.100' } })} />; | ||
}`, | ||
}, | ||
|
||
{ | ||
code: javascript` | ||
import { Circle } from './panda/jsx'; | ||
function App(){ | ||
return <Circle css={{ '[data-focus]': { position: 'absolute' } }} />; | ||
}`, | ||
}, | ||
] | ||
|
||
tester.run(RULE_NAME, rule, { | ||
valid: valids.map(({ code }) => ({ | ||
code, | ||
})), | ||
invalid: invalids.map(({ code }) => ({ | ||
code, | ||
errors: 1, | ||
})), | ||
}) |