-
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?
Conversation
|
*/ | ||
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 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
✅ Deploy Preview for compiled-css-in-js canceled.
|
`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 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.
596c55f
to
8c5e709
Compare
|
||
/** 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 comment
The reason will be displayed to describe this comment to others. Learn more.
* - A usage | |
* - A usage of the `cssMap` API from Compiled |
@@ -647,6 +648,17 @@ const extractObjectExpression = (node: t.ObjectExpression, meta: Metadata): CSSO | |||
return; | |||
} | |||
|
|||
if (isCompiledStyleCall(propValue, updatedMeta.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?
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.
Same as Liam — a test case or two and we're golden! If this throws errors, we should almost certainly fix them and let them block us—I think it's "safe" to land.
What is this change?
The following code fails silently:
We add an error so that it no longer fails silently.
Why are we making this change?
We support this: (see #1321 )
But we don't support this:
We should either:
someObject['red']
telling developers either to switch tosomeObject.red
or switch tocssMap
, orsomeObject['red']
The first option is easier so I've done that in this PR 😅 but curious to hear your thoughts on whether we should in fact do the latter instead.
PR checklist
Don't delete me!
I have...
website/