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

Conversation

dddlr
Copy link
Collaborator

@dddlr dddlr commented Mar 10, 2025

What is this change?

The following code fails silently:

const someObject = {
    blue: css({ color: 'blue' }),
    red: css({ color: 'red' }),
}

const App = <div css={someObject['red']}>hello</div>

We add an error so that it no longer fails silently.

Why are we making this change?

We support this: (see #1321 )

const someObject = {
    blue: css({ color: 'blue' }),
    red: css({ color: 'red' }),
}

const App = <div css={someObject.red}>hello</div>

But we don't support this:

const someObject = {
    blue: css({ color: 'blue' }),
    red: css({ color: 'red' }),
}

const App = <div css={someObject['red']}>hello</div>

We should either:

  • throw an error for someObject['red'] telling developers either to switch to someObject.red or switch to cssMap, or
  • add support for someObject['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...

  • Updated or added applicable tests
  • Updated the documentation in website/
  • Added a changeset (if making any changes that affect Compiled's behaviour)

Copy link

changeset-bot bot commented Mar 10, 2025

⚠️ No Changeset found

Latest commit: 8c5e709

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

*/
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

Copy link

netlify bot commented Mar 10, 2025

Deploy Preview for compiled-css-in-js canceled.

Name Link
🔨 Latest commit 8c5e709
🔍 Latest deploy log https://app.netlify.com/sites/compiled-css-in-js/deploys/67ce80fe8ef3390008f02e85

`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.

@dddlr dddlr force-pushed the bug/css-function-call-in-object branch from 596c55f to 8c5e709 Compare March 10, 2025 06:04
@alexreardon alexreardon self-requested a review March 10, 2025 06:33

/** 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

@@ -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?

Copy link
Collaborator

@kylorhall-atlassian kylorhall-atlassian left a 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants