Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions configs/eslint-config-compass/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ module.exports = {
plugins: [...shared.plugins, '@mongodb-js/compass', 'chai-friendly'],
rules: {
...shared.rules,
'@mongodb-js/compass/no-inline-emotion-css': 'warn',
'@mongodb-js/compass/no-leafygreen-outside-compass-components': 'error',
'@mongodb-js/compass/unique-mongodb-log-id': [
'error',
Expand Down
1 change: 1 addition & 0 deletions configs/eslint-plugin-compass/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
module.exports = {
rules: {
'no-inline-emotion-css': require('./rules/no-inline-emotion-css'),
'no-leafygreen-outside-compass-components': require('./rules/no-leafygreen-outside-compass-components'),
'unique-mongodb-log-id': require('./rules/unique-mongodb-log-id'),
},
Expand Down
64 changes: 64 additions & 0 deletions configs/eslint-plugin-compass/rules/no-inline-emotion-css.js
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'
);
Copy link
Collaborator

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 JSXExpressionContainer is 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 🙂

Copy link
Member Author

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!

}

/** @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) {
if (!isCssCall(node)) {
return;
}

if (isInsideFunction(context)) {
context.report({
node,
messageId: 'noInlineCSS',
});
}
},
};
},
};
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, {
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.",
],
},
],
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const testOptions = {
],
};

ruleTester.run('no-leafygreen-outside-compass-components', rule, {
ruleTester.run('unique-mongodb-log-id', rule, {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ty!

valid: [
{
code: 'mongoLogId(10);',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -47,6 +47,10 @@ const sortDirectionStyles = css({
width: '150px',
});

const comboboxStyles = css({
minWidth: '200px',
});

const mapSortFormDataToStageValue = (
formData: SortFieldState[]
): Record<string, number> => {
Expand Down Expand Up @@ -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
Expand All @@ -178,7 +174,7 @@ export const SortForm = ({ fields, onChange }: WizardComponentProps) => {
renderItem={(item, index) => {
return (
<SortFormGroup
comboboxClassName={comboboxClassName}
comboboxClassName={comboboxStyles}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before after
Screenshot 2025-09-11 at 1 35 03 PM Screenshot 2025-09-11 at 1 37 07 PM

index={index}
sortField={item.field}
sortDirection={item.direction}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ export default function SearchNoResults() {
return (
<div className={centeredContent}>
<Subtitle
className={css(
className={
darkMode ? missingAtlasIndexDarkStyles : missingAtlasIndexLightStyles
)}
}
>
No preview documents
</Subtitle>
Expand Down