From d2ee0660a6df38d23daaa91ad567ceb41ecf3d56 Mon Sep 17 00:00:00 2001 From: Durvesh Pilankar Date: Mon, 29 Jun 2026 16:53:01 -0700 Subject: [PATCH] Fix leading-zero normalizer skipping negative fractional values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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). --- .../__tests__/transform-value-normalization-test.js | 9 +++++++++ .../src/shared/utils/normalizers/leading-zero.js | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/@stylexjs/babel-plugin/__tests__/transform-value-normalization-test.js b/packages/@stylexjs/babel-plugin/__tests__/transform-value-normalization-test.js index 60a577e15..b2f08ca65 100644 --- a/packages/@stylexjs/babel-plugin/__tests__/transform-value-normalization-test.js +++ b/packages/@stylexjs/babel-plugin/__tests__/transform-value-normalization-test.js @@ -174,6 +174,15 @@ describe('@stylexjs/babel-plugin', () => { `); }); + test('strip leading zeros from negative values', () => { + const code = transform(` + import stylex from 'stylex'; + const styles = stylex.create({ x: { marginTop: '-0.5px' } }); + `); + expect(code).toContain('margin-top:-.5px'); + expect(code).not.toContain('-0.5px'); + }); + test('use double quotes in empty strings', () => { expect( transform(` diff --git a/packages/@stylexjs/babel-plugin/src/shared/utils/normalizers/leading-zero.js b/packages/@stylexjs/babel-plugin/src/shared/utils/normalizers/leading-zero.js index 2c8866a19..00b1aad3b 100644 --- a/packages/@stylexjs/babel-plugin/src/shared/utils/normalizers/leading-zero.js +++ b/packages/@stylexjs/babel-plugin/src/shared/utils/normalizers/leading-zero.js @@ -27,7 +27,7 @@ export default function normalizeLeadingZero( return; } const dimension = parser.unit(node.value); - if (value < 1 && value >= 0) { + if (Math.abs(value) < 1) { node.value = value.toString().replace('0.', '.') + (dimension ? dimension.unit : ''); }