-
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 all 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
84 changes: 84 additions & 0 deletions
84
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,84 @@ | ||
| '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 react function. | ||
| * This only checks for JSXExpressionContainers or an uppercase function name, | ||
| * so it may miss some cases. | ||
| * @param {Object} context - ESLint context. | ||
| * @returns {boolean} - Whether we're inside a function. | ||
| */ | ||
| function isInsideReactFunction(context) { | ||
| const ancestors = context.getAncestors(); | ||
|
|
||
| const hasJSXAncestor = ancestors.some( | ||
| (ancestor) => ancestor.type === 'JSXExpressionContainer' | ||
| ); | ||
|
|
||
| if (hasJSXAncestor) { | ||
| return true; | ||
| } | ||
|
|
||
| const currentFunction = ancestors.find( | ||
| (ancestor) => | ||
| ancestor.type === 'FunctionDeclaration' || | ||
| ancestor.type === 'FunctionExpression' || | ||
| ancestor.type === 'ArrowFunctionExpression' | ||
| ); | ||
| if (currentFunction) { | ||
| // If the function name starts with an uppercase letter maybe it's a React component. | ||
| if ( | ||
| currentFunction.type === 'FunctionDeclaration' && | ||
| currentFunction.id && | ||
| /^[A-Z]/.test(currentFunction.id.name) | ||
| ) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** @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 dynamic css() calls in react rendering. | ||
| CallExpression(node) { | ||
| if (!isCssCall(node)) { | ||
| return; | ||
| } | ||
|
|
||
| if (isInsideReactFunction(context)) { | ||
| context.report({ | ||
| node, | ||
| messageId: 'noInlineCSS', | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
51 changes: 51 additions & 0 deletions
51
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,51 @@ | ||
| 'use strict'; | ||
| const { RuleTester } = require('eslint'); | ||
| const rule = require('./no-inline-emotion-css'); | ||
|
|
||
| const ruleTester = new RuleTester(); | ||
|
|
||
| ruleTester.run('no-inline-emotion-css', rule, { | ||
| 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 } }, | ||
| }, | ||
| { | ||
| code: "function pineapple() { const dynamicSet = css({ background: 'orange' }); }", | ||
| parserOptions: { ecmaVersion: 2021 }, | ||
| }, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: ` | ||
| function Pineapple() { | ||
| const pineappleStyles = css({ background: 'purple' }); | ||
| return (<div className={pineappleStyles}>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.", | ||
| ], | ||
| }, | ||
| { | ||
| 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
Oops, something went wrong.
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.


Uh oh!
There was an error while loading. Please reload this page.