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

Throw error if css function call used inside object in unsupported way #1815

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
14 changes: 13 additions & 1 deletion packages/babel-plugin/src/utils/css-builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
isCompiledCSSTaggedTemplateExpression,
isCompiledKeyframesCallExpression,
isCompiledKeyframesTaggedTemplateExpression,
isCompiledStyleCall,
} from './is-compiled';
import { isEmptyValue } from './is-empty';
import {
Expand Down Expand Up @@ -591,7 +592,7 @@ const extractObjectExpression = (node: t.ObjectExpression, meta: Metadata): CSSO

if (t.isArrowFunctionExpression(propValue)) {
/*
Given statments like:
Given statements like:
fontWeight: (props) => props.isBold ? 'bold': 'normal',
marginTop: (props) => `${props.isLast ? 5 : 10}px`,

Expand Down Expand Up @@ -647,6 +648,17 @@ const extractObjectExpression = (node: t.ObjectExpression, meta: Metadata): CSSO
return;
}

if (isCompiledStyleCall(propValue, updatedMeta.state)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you please write a test case for it?

throw new Error(
"You can't use Compiled APIs like this within an object.\n\n" +
`This is the invalid code: ${generate(node).code}\n\n` +
"Usually this happens because you're referencing some styles using syntax like `styles['someString']`. We don't support this for `css` function calls. Instead, you should either:\n" +
'- change it to not use square brackets, like `styles.hello`\n' +
'- if you need to use square brackets (e.g. you are styling something like `styles[someKey]`), use the cssMap API instead -- https://compiledcssinjs.com/docs/api-cssmap\n\n' +
Copy link
Collaborator Author

@dddlr dddlr Mar 10, 2025

Choose a reason for hiding this comment

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

the implication of course is that theoretically, you should only need cssMap if which set of styles you are using is dynamic, or otherwise not known until runtime.

For example, if you had some info styles and some warning styles, both of which are statically defined... but you didn't know whether to apply the info styles or warning styles until someone clicks on a button.

'If you triggered this error through another way, we want to know about it! Please report this error message and your code to us, either (for Atlassian employees) on #help-compiled or (non-employees) on GitHub.'
);
}

const { expression, variableName } = getVariableDeclaratorValueForOwnPath(
propValue,
updatedMeta
Expand Down
19 changes: 19 additions & 0 deletions packages/babel-plugin/src/utils/is-compiled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,22 @@ export const isCompiledStyledTaggedTemplateExpression = (
t.isTaggedTemplateExpression(node) &&
(isCompiledStyledMemberExpression(node.tag, state) ||
isCompiledStyledCompositionCallExpression(node.tag, state));

/** Returns true if and only if the current node is one of the following:
* - A usage of the `css` API from Compiled
* - A usage
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
* - A usage
* - A usage of the `cssMap` API from Compiled

* - A usage of the `styled` API from Compiled
* - IS NOT a usage of the `keyframes` API from Compiled
*/
export const isCompiledStyleCall = (node: t.Expression, state: State): boolean => {
const checkers = [
isCompiledCSSCallExpression,
Copy link
Collaborator Author

@dddlr dddlr Mar 10, 2025

Choose a reason for hiding this comment

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

we probably don't need all of these... we can delete the ones that can't actually be triggered when we write the tests

isCompiledCSSTaggedTemplateExpression,
isCompiledCSSMapCallExpression,
isCompiledStyledCompositionCallExpression,
isCompiledStyledCallExpression,
isCompiledStyledTaggedTemplateExpression,
];

return checkers.some((checker) => checker(node, state));
};
Loading