-
Notifications
You must be signed in to change notification settings - Fork 1
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
5 changed files
with
100 additions
and
0 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 @@ | ||
--- | ||
'eslint-config-widen-react': minor | ||
--- | ||
|
||
Add a new rule that detects conditional statements used on a css prop. |
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
35 changes: 35 additions & 0 deletions
35
packages/eslint-config-widen-react/src/rules/no-conditional-css-prop.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Rule } from 'eslint' | ||
|
||
export default { | ||
create(context) { | ||
return { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
JSXAttribute(node: any) { | ||
if ( | ||
node.name.name === 'css' && | ||
node.value && | ||
node.value.type === 'JSXExpressionContainer' && | ||
(node.value.expression.type === 'ConditionalExpression' || | ||
(node.value.expression.type === 'LogicalExpression' && | ||
(node.value.expression.operator === '&&' || | ||
node.value.expression.operator === '||'))) | ||
) { | ||
context.report({ | ||
message: | ||
'Avoid using conditionals within the css prop, move them to style.', | ||
node, | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
meta: { | ||
docs: { | ||
category: 'Best Practices', | ||
description: 'Disallow conditionals within the css prop in components', | ||
recommended: false, | ||
}, | ||
schema: [], | ||
type: 'suggestion', | ||
}, | ||
} as Rule.RuleModule |
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
57 changes: 57 additions & 0 deletions
57
packages/eslint-config-widen-react/test/no-conditional-css-prop.test.js
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,57 @@ | ||
import { RuleTester } from 'eslint' | ||
import rule from '../src/rules/no-conditional-css-prop' | ||
import sharedGlobals from '../src/sharedGlobals' | ||
|
||
const ruleTester = new RuleTester({ | ||
languageOptions: { | ||
globals: sharedGlobals, | ||
parserOptions: { | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
ruleTester.run('no-conditional-css-prop', rule, { | ||
invalid: [ | ||
{ | ||
code: '<div css={condition ? { color: "red" } : { color: "blue" }} />', | ||
errors: [ | ||
{ | ||
message: | ||
'Avoid using conditionals within the css prop, move them to style.', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: '<div css={condition && { color: "red" }} />', | ||
errors: [ | ||
{ | ||
message: | ||
'Avoid using conditionals within the css prop, move them to style.', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: '<div css={condition || { color: "blue" }} />', | ||
errors: [ | ||
{ | ||
message: | ||
'Avoid using conditionals within the css prop, move them to style.', | ||
}, | ||
], | ||
}, | ||
], | ||
valid: [ | ||
{ | ||
code: '<div css={{ color: "red" }} />', | ||
}, | ||
{ | ||
code: '<div css={someVariable} />', | ||
}, | ||
{ | ||
code: '<div css={`color: ${someVariable}`} />', | ||
}, | ||
], | ||
}) |