Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Emotion styles / styled components bans into a custom rule #126

Open
karlhorky opened this issue May 11, 2022 · 0 comments
Open
Assignees

Comments

@karlhorky
Copy link
Member

karlhorky commented May 11, 2022

Currently, we are using no-restricted-syntax config blocks to ban creation of Emotion styles or styled components within components. One of the issues with this is the warning messages don't allow us to add clickable links to the description of the error:

One way to solve this Is moving these checks into a new custom ESLint rule:

// Currently it is not possible to use Markdown eg. links in ESLint warnings / error messages
//
// FIXME: Switch to a custom rule
// https://github.com/upleveled/eslint-config-upleveled/issues/126
{
selector:
'FunctionDeclaration VariableDeclaration:has(VariableDeclarator > TaggedTemplateExpression > MemberExpression[object.name="styled"][property]), FunctionDeclaration VariableDeclaration:has(VariableDeclarator > TaggedTemplateExpression[tag.name="css"])',
message:
'Declaring Emotion styles or a styled component within a React component will cause the element to get recreated, causing loss of state and other problems - see the react/no-unstable-nested-components docs for more info https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-unstable-nested-components.md',
},

Additionally, while we are doing this, we should optimize this rule to only trigger on React components (currently it is triggered on any FunctionDeclaration, which includes any function). One way of detecting a React component would be to use the component utilities from eslint-plugin-react:

https://github.com/jsx-eslint/eslint-plugin-react/blob/c8833f301314dab3e79ef7ac4cf863e4d5fa0019/lib/util/Components.js#L436-L444

@karlhorky karlhorky self-assigned this May 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant