-
Notifications
You must be signed in to change notification settings - Fork 239
chore(eslint-config): add dynamic css call lint rule COMPASS-9843 #7314
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
64 changes: 64 additions & 0 deletions
64
configs/eslint-plugin-compass/rules/no-inline-emotion-css.js
This file contains hidden or 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,64 @@ | ||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Checks if a node is a css() call from emotion. | ||
| * @param {Object} node - AST node to check. | ||
| * @returns {boolean} - Whether the node is a css() call. | ||
| */ | ||
| function isCssCall(node) { | ||
| return ( | ||
| node && | ||
| node.type === 'CallExpression' && | ||
| node.callee && | ||
| node.callee.type === 'Identifier' && | ||
| node.callee.name === 'css' | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a call is inside a function. | ||
| * @param {Object} context - ESLint context. | ||
| * @returns {boolean} - Whether we're inside a function. | ||
| */ | ||
| function isInsideFunction(context) { | ||
| const ancestors = context.getAncestors(); | ||
|
|
||
| return ancestors.some( | ||
| (ancestor) => | ||
| ancestor.type === 'FunctionDeclaration' || | ||
| ancestor.type === 'FunctionExpression' || | ||
| ancestor.type === 'ArrowFunctionExpression' | ||
| ); | ||
| } | ||
|
|
||
| /** @type {import('eslint').Rule.RuleModule} */ | ||
| module.exports = { | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: 'Disallow dynamic emotion css() calls in render methods', | ||
| }, | ||
| messages: { | ||
| noInlineCSS: | ||
| "Don't use a dynamic css() call in the render method, this creates a new class name every time component updates and is not performant. Static styles can be defined with css outside of render, dynamic should be passed through the style prop.", | ||
| }, | ||
| }, | ||
|
|
||
| create(context) { | ||
| return { | ||
| // Check for css() calls in functions. | ||
| CallExpression(node) { | ||
Anemy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!isCssCall(node)) { | ||
| return; | ||
| } | ||
|
|
||
| if (isInsideFunction(context)) { | ||
| context.report({ | ||
| node, | ||
| messageId: 'noInlineCSS', | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
43 changes: 43 additions & 0 deletions
43
configs/eslint-plugin-compass/rules/no-inline-emotion-css.test.js
This file contains hidden or 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,43 @@ | ||
| 'use strict'; | ||
| const { RuleTester } = require('eslint'); | ||
| const rule = require('./no-inline-emotion-css'); | ||
|
|
||
| const ruleTester = new RuleTester(); | ||
|
|
||
| ruleTester.run('no-leafygreen-outside-compass-components', rule, { | ||
Anemy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| valid: [ | ||
| { | ||
| code: "const staticSet = css({ background: 'orange' });", | ||
| parserOptions: { ecmaVersion: 2021 }, | ||
| }, | ||
| { | ||
| code: ` | ||
| const pineappleStyles = css({ background: 'purple' }); | ||
| function pineapple() { return pineappleStyles; };`, | ||
| parserOptions: { ecmaVersion: 2021 }, | ||
| }, | ||
| { | ||
| code: ` | ||
| const pineappleStyles = css({ background: 'purple' }); | ||
| function pineapple() { return (<div className={pineappleStyles}>pineapples</div>); }`, | ||
| parserOptions: { ecmaVersion: 2021, ecmaFeatures: { jsx: true } }, | ||
| }, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: "function pineapple() { const dynamicSet = css({ background: 'orange' }); }", | ||
| parserOptions: { ecmaVersion: 2021 }, | ||
| errors: [ | ||
| "Don't use a dynamic css() call in the render method, this creates a new class name every time component updates and is not performant. Static styles can be defined with css outside of render, dynamic should be passed through the style prop.", | ||
| ], | ||
| }, | ||
| { | ||
| code: "function pineapple() { return (<div className={css({ background: 'purple' })}>pineapples</div>); }", | ||
| parserOptions: { ecmaVersion: 2021, ecmaFeatures: { jsx: true } }, | ||
|
|
||
| errors: [ | ||
| "Don't use a dynamic css() call in the render method, this creates a new class name every time component updates and is not performant. Static styles can be defined with css outside of render, dynamic should be passed through the style prop.", | ||
| ], | ||
| }, | ||
| ], | ||
| }); | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ const testOptions = { | |
| ], | ||
| }; | ||
|
|
||
| ruleTester.run('no-leafygreen-outside-compass-components', rule, { | ||
| ruleTester.run('unique-mongodb-log-id', rule, { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ty! |
||
| valid: [ | ||
| { | ||
| code: 'mongoLogId(10);', | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ import { | |
| css, | ||
| ListEditor, | ||
| } from '@mongodb-js/compass-components'; | ||
| import React, { useMemo, useState } from 'react'; | ||
| import React, { useState } from 'react'; | ||
| import { | ||
| SORT_DIRECTION_OPTIONS, | ||
| getNextId, | ||
|
|
@@ -47,6 +47,10 @@ const sortDirectionStyles = css({ | |
| width: '150px', | ||
| }); | ||
|
|
||
| const comboboxStyles = css({ | ||
| minWidth: '200px', | ||
| }); | ||
|
|
||
| const mapSortFormDataToStageValue = ( | ||
| formData: SortFieldState[] | ||
| ): Record<string, number> => { | ||
|
|
@@ -159,14 +163,6 @@ export const SortForm = ({ fields, onChange }: WizardComponentProps) => { | |
| onSetFormData(newData); | ||
| }; | ||
|
|
||
| const comboboxClassName = useMemo(() => { | ||
| return css({ | ||
| width: `calc(${String( | ||
| Math.max(...fields.map(({ name }) => name.length), 10) | ||
| )}ch)`, | ||
| }); | ||
| }, [fields]); | ||
|
|
||
| return ( | ||
| <div className={containerStyles}> | ||
| <ListEditor | ||
|
|
@@ -178,7 +174,7 @@ export const SortForm = ({ fields, onChange }: WizardComponentProps) => { | |
| renderItem={(item, index) => { | ||
| return ( | ||
| <SortFormGroup | ||
| comboboxClassName={comboboxClassName} | ||
| comboboxClassName={comboboxStyles} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| index={index} | ||
| sortField={item.field} | ||
| sortDirection={item.direction} | ||
|
|
||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just checking for css calls found inside
JSXExpressionContaineris enough for initial iteraion, I think it's the main offender case, but I might be wrong. Otherwise it can be indeed tricky to target only functions that are React components, react hook rules for example try to identify something as a component just by function name starting with an uppercase letter, not the most precise logic 🙂There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, I added both!