Skip to content

Commit 8c5e709

Browse files
committed
Throw error if css function call used inside object in unsupported way
1 parent f04a4d0 commit 8c5e709

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

packages/babel-plugin/src/utils/css-builders.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
isCompiledCSSTaggedTemplateExpression,
1818
isCompiledKeyframesCallExpression,
1919
isCompiledKeyframesTaggedTemplateExpression,
20+
isCompiledStyleCall,
2021
} from './is-compiled';
2122
import { isEmptyValue } from './is-empty';
2223
import {
@@ -591,7 +592,7 @@ const extractObjectExpression = (node: t.ObjectExpression, meta: Metadata): CSSO
591592

592593
if (t.isArrowFunctionExpression(propValue)) {
593594
/*
594-
Given statments like:
595+
Given statements like:
595596
fontWeight: (props) => props.isBold ? 'bold': 'normal',
596597
marginTop: (props) => `${props.isLast ? 5 : 10}px`,
597598
@@ -647,6 +648,17 @@ const extractObjectExpression = (node: t.ObjectExpression, meta: Metadata): CSSO
647648
return;
648649
}
649650

651+
if (isCompiledStyleCall(propValue, updatedMeta.state)) {
652+
throw new Error(
653+
"You can't use Compiled APIs like this within an object.\n\n" +
654+
`This is the invalid code: ${generate(node).code}\n\n` +
655+
"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" +
656+
'- change it to not use square brackets, like `styles.hello`\n' +
657+
'- 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' +
658+
'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.'
659+
);
660+
}
661+
650662
const { expression, variableName } = getVariableDeclaratorValueForOwnPath(
651663
propValue,
652664
updatedMeta

packages/babel-plugin/src/utils/is-compiled.ts

+19
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,22 @@ export const isCompiledStyledTaggedTemplateExpression = (
135135
t.isTaggedTemplateExpression(node) &&
136136
(isCompiledStyledMemberExpression(node.tag, state) ||
137137
isCompiledStyledCompositionCallExpression(node.tag, state));
138+
139+
/** Returns true if and only if the current node is one of the following:
140+
* - A usage of the `css` API from Compiled
141+
* - A usage
142+
* - A usage of the `styled` API from Compiled
143+
* - IS NOT a usage of the `keyframes` API from Compiled
144+
*/
145+
export const isCompiledStyleCall = (node: t.Expression, state: State): boolean => {
146+
const checkers = [
147+
isCompiledCSSCallExpression,
148+
isCompiledCSSTaggedTemplateExpression,
149+
isCompiledCSSMapCallExpression,
150+
isCompiledStyledCompositionCallExpression,
151+
isCompiledStyledCallExpression,
152+
isCompiledStyledTaggedTemplateExpression,
153+
];
154+
155+
return checkers.some((checker) => checker(node, state));
156+
};

0 commit comments

Comments
 (0)