Skip to content

fix(unplugin): arithmetic support for defineConsts#1707

Draft
skovhus wants to merge 2 commits into
facebook:mainfrom
skovhus:kenneth/improve-define-const-errors
Draft

fix(unplugin): arithmetic support for defineConsts#1707
skovhus wants to merge 2 commits into
facebook:mainfrom
skovhus:kenneth/improve-define-const-errors

Conversation

@skovhus

@skovhus skovhus commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

What changed / motivation ?

JS arithmetic on defineVars/defineConsts token references inside stylex.create() silently produced broken CSS (C.a + C.b"var(--a)var(--b)", C.a - 1NaN).

This PR compiles arithmetic to CSS calc() instead — without violating the "transforming a file must not read other files" constraint: the emitted expression is built from the same hash-derived var() strings as today, and defineConsts values are substituted by the existing CSS aggregation step.

  • zIndex: C.a + C.bcalc(var(--a) + var(--b)) (→ calc(26 + 14) for consts)
  • zIndex: C.elevated - 1calc(var(--elevated) - 1)
  • marginLeft: -C.guttercalc(-1 * var(--gutter))
  • Works for defineConsts, defineVars (incl. sibling refs), and mixed expressions

Everything that can't map to calc() is now a compile-time error.

Breaking change

⚠️ Comparisons on token refs (e.g. C.a === 'red' ? x : y) previously evaluated silently (string comparison, usually picking the wrong branch) and now fail the build with an actionable error. Null/undefined guards are exempt and keep working.

Linked PR/Issues

Fixes #1597

Additional Context

Screenshots, Tests, Anything Else

Pre-flight checklist

@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 11, 2026
@vercel

vercel Bot commented Jun 11, 2026

Copy link
Copy Markdown

@skovhus is attempting to deploy a commit to the Meta Open Source Team on Vercel.

A member of the Team first needs to authorize it.

- Allow ==/!=/===/!== null and undefined guards on token refs (previously-correct
  defensive patterns keep compiling)
- Add comparison-specific compile error message for other comparisons
- Fail on jammed '+' concatenation (token + '10px') instead of emitting
  invalid CSS; separated list concatenation ('solid ' + token) still works
- Reject calc() expressions in computed style keys
- Throw token-misuse errors inside dynamic styles instead of silently
  degrading to runtime inline styles
- Preserve evaluator deopt reasons through defineVars function values
- Widen var() detection to cover unicode custom-property names
- Validate calc dimension operands via postcss-value-parser + CSS unit
  whitelist (rejects bogus units like '10foo')
- Share evaluationError helper across all evaluating visitors (keyframes,
  positionTry, viewTransitionClass, nested variants included)
- Extract shared roundForCss from transform-value

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@skovhus skovhus changed the title Improve arithmetic support for defineConsts fix(unplugin): arithmetic support for defineConsts Jun 11, 2026
@skovhus

skovhus commented Jun 11, 2026

Copy link
Copy Markdown
Contributor Author

@nmn if we don't want to support arithmetic for some reason (would be nice if we could), then at least I think we should fail hard instead of silently emitting broken CSS.

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.

defineConsts arithmetic silently breaks inside stylex.create()

1 participant