fix(unplugin): arithmetic support for defineConsts#1707
Draft
skovhus wants to merge 2 commits into
Draft
Conversation
|
@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>
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. |
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 ?
JS arithmetic on
defineVars/defineConststoken references insidestylex.create()silently produced broken CSS (C.a + C.b→"var(--a)var(--b)",C.a - 1→NaN).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-derivedvar()strings as today, anddefineConstsvalues are substituted by the existing CSS aggregation step.zIndex: C.a + C.b→calc(var(--a) + var(--b))(→calc(26 + 14)for consts)zIndex: C.elevated - 1→calc(var(--elevated) - 1)marginLeft: -C.gutter→calc(-1 * var(--gutter))defineConsts,defineVars(incl. sibling refs), and mixed expressionsEverything that can't map to
calc()is now a compile-time error.Breaking change
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
Contribution Guidelines