Skip to content

[babel-plugin] Fix corrupted class names from null coercion in dynamic styles#1731

Open
henryqdineen wants to merge 2 commits into
facebook:mainfrom
henryqdineen:hqd-fix-null-coercion-dynamic-styles
Open

[babel-plugin] Fix corrupted class names from null coercion in dynamic styles#1731
henryqdineen wants to merge 2 commits into
facebook:mainfrom
henryqdineen:hqd-fix-null-coercion-dynamic-styles

Conversation

@henryqdineen

Copy link
Copy Markdown
Collaborator

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 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(''), 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 with expr, likely reasoning that expr would be null anyway in the else branch. That's true, but null + "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 "" and null are not identical in styleq's merge logic — null is 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

…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>
@vercel

vercel Bot commented Jun 22, 2026

Copy link
Copy Markdown

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

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 22, 2026
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Corrupted class names including string "null" generated from dynamic style with media query conditions

1 participant