[babel-plugin] Fix corrupted class names from null coercion in dynamic styles#1731
Open
henryqdineen wants to merge 2 commits into
Open
[babel-plugin] Fix corrupted class names from null coercion in dynamic styles#1731henryqdineen wants to merge 2 commits into
henryqdineen wants to merge 2 commits into
Conversation
…c styles
When a dynamic style has multiple conditional class names for a single
CSS property (e.g. multiple media queries or pseudo-classes), the
generated code joins them with `+`. If any condition evaluates to `null`
or `undefined` at runtime, JavaScript coerces it to the string "null" or
"undefined", producing corrupted class names like "nullxafpxz5".
The fix changes the else branch of the null-check ternary from `expr` to
`t.stringLiteral('')`. This is a regression from 7a1ed95 which replaced
the original `""` with `expr` during a refactor.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
@henryqdineen is attempting to deploy a commit to the Meta Open Source Team on Vercel. A member of the Team first needs to authorize it. |
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed / motivation ?
When a dynamic style has multiple conditional class names for a single CSS property (e.g. multiple media queries or pseudo-classes), the generated code joins them with
+string concatenation. If any condition evaluates tonullorundefinedat runtime, JavaScript coerces it to the string"null"or"undefined", producing corrupted class names like"nullxafpxz5".The fix changes the else branch of the null-check ternary from
exprtot.stringLiteral(''), so a non-matching condition contributes nothing to the concatenation instead of contributing"null".This is a regression from 7a1ed95 which refactored the single-class and multi-class code paths into one. The original code before that refactor already used
""for the multi-class case — the refactor replaced it withexpr, likely reasoning thatexprwould be null anyway in the else branch. That's true, butnull + "string"produces"nullstring"in JavaScript.As a side benefit, this also produces smaller output since
""replaces what was previously a duplicated copy of the original expression in the else branch (unless the expression is a single-character variable).Note that
""andnullare not identical in styleq's merge logic —nullis the intentional "unset" semantic while""is treated as an empty string value. In practice they behave the same (both claim the property slot, neither adds a real class name), and the all-null case was already broken before this fix (producing"nullnull"), so this is strictly an improvement.Linked PR/Issues
Fixes #1702
Additional Context
Existing snapshot tests updated to reflect the new
""fallback. All tests pass.Pre-flight checklist
Contribution Guidelines