-
Notifications
You must be signed in to change notification settings - Fork 70
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ import { | |
isCompiledCSSTaggedTemplateExpression, | ||
isCompiledKeyframesCallExpression, | ||
isCompiledKeyframesTaggedTemplateExpression, | ||
isCompiledStyleCall, | ||
} from './is-compiled'; | ||
import { isEmptyValue } from './is-empty'; | ||
import { | ||
|
@@ -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`, | ||
|
||
|
@@ -647,6 +648,17 @@ const extractObjectExpression = (node: t.ObjectExpression, meta: Metadata): CSSO | |
return; | ||
} | ||
|
||
if (isCompiledStyleCall(propValue, updatedMeta.state)) { | ||
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' + | ||
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. the implication of course is that theoretically, you should only need 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 | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
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.
Suggested change
|
||||||
* - 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, | ||||||
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. 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)); | ||||||
}; |
There was a problem hiding this comment.
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?