Skip to content

Fix leading-zero normalizer skipping negative fractional values#1749

Open
durvesh1992 wants to merge 1 commit into
facebook:mainfrom
durvesh1992:fix/leading-zero-negative-values
Open

Fix leading-zero normalizer skipping negative fractional values#1749
durvesh1992 wants to merge 1 commit into
facebook:mainfrom
durvesh1992:fix/leading-zero-negative-values

Conversation

@durvesh1992

Copy link
Copy Markdown

Summary

The leading-zero normalizer shortens fractional CSS values for smaller output (0.5px.5px). But its guard excludes negative numbers:

if (value < 1 && value >= 0) {   // value >= 0 excludes negatives
  node.value = value.toString().replace('0.', '.') + (dimension ? dimension.unit : '');
}

So -0.5px is emitted as-is while 0.5px becomes .5px — inconsistent output for equivalent magnitudes. The transform already produces the correct shortened string for negatives ((-0.5).toString().replace('0.', '.')-.5); only the value gate blocked it.

Fix

Use Math.abs(value) < 1 so negative fractions are normalized too (-0.5px-.5px). 0/-0 and |value| >= 1 are unchanged.

Test plan

yarn --cwd packages/@stylexjs/babel-plugin jest

Added a regression test (marginTop: '-0.5px'margin-top:-.5px) that fails before and passes after. Full babel-plugin suite: 960 passed, no snapshot regressions.

normalizeLeadingZero stripped the leading zero from positive fractions
(0.5px -> .5px) for smaller output, but the guard `value < 1 && value
>= 0` excluded negatives, so -0.5px was left untouched — inconsistent
output for an equivalent value. Use Math.abs(value) < 1 so negative
fractions are shortened too (-0.5px -> -.5px); 0 and |value| >= 1 are
unchanged.

Adds a regression test (fails before / passes after).
@vercel

vercel Bot commented Jun 29, 2026

Copy link
Copy Markdown

Someone 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 29, 2026
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.

1 participant