Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
84 changes: 84 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,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',
});
}
},
};
},
};
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.",
],
},
],
});
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
Loading