Repository navigation
v0.0.41 - Remove all 3rd party dependencies, calc fixes
I removed the 3rd party @csstools/css-calc dependency so this library would have no deps, and be fully AI, in case there are any edge cases to be found in the resolving of calc that could use a test. After removing all references to this library, I replaced the function import with a different function name, without any declaration, with the hopes the AI would just implement the missing function. Instead it looked at the commit history to see my clever deception. I was doing this in the hopes that it wouldn't try to re-implement that library or reference it in any way. Which I don't think it actually did, because the resulting implementation actually fixes new bugs. This resulted in new tests, which was the goal of this change. -tjw
Release notes and code by Claud Opus 5 High
What was wrong
src/value/math.js called a simplifyCalc() that did not exist anywhere (it was the @csstools/css-calc calc() removed in commit 53839f8). All three call sites were wrapped in try/catch, so every call silently threw a ReferenceError and fell back to the unmodified value — which is why calc(10/2), calc(pi*100px), min()/max(), etc. never resolved.
What I did
New module src\value\math-expression.js — a real CSS math expression evaluator, replacing the removed dependency:
- Tokenizer for numbers (with fraction/exponent), units, percentages, identifiers, functions, operators, punctuation; records whether whitespace precedes each token.
- Recursive-descent parser with CSS precedence (
*//bind tighter than+/-), following the spec's grammar:+/-is an operator only when it stands apart from its number, socalc(1px-2px)andcalc(--0.05rem - 1.2em)are left as written rather than "fixed" into valid CSS. - Values as linear combinations of units (
Map<unit, total>), so multiplication/division require a plain number on one side, sums of a number and a dimension are rejected, and absolute lengths total in px. - All ten math functions:
calc,min,max,clamp(incl.nonebounds),abs,sign,hypot,mod,rem,round(incl. rounding strategies), nested to any depth;pi,e,infinity,NaNconstants. - Serializer that writes terms in the order the calculation states them, leads with an added term to save a sign, merges units of the same kind only when one kind is written in two units (
calc(1s - 200ms)→.8s), keeps a percentage that cancels out (percopiedTests/calc/0025), and refuses results no number states faithfully (infinite, beyond safe integers, or rounding away to nothing).
src/value/math.js: imports simplifyCalc/MATH_FUNCTION_NAMES/formatUnitTotals; tryFoldCalcExpression now shares the serializer; the min|max pass generalized to every math function; the math passes run until the value settles (a resolved inner function brings its parent within reach, which keeps output idempotent); the standalone-calc() path resolves the expression as written before the old flattening, and resolution is only taken when it doesn't make the value longer.
Verification
npm t: 590/590 (was 581/590), including the built-in idempotency re-runnpm run lint: clean,npm run idem: passesnpm run real(41 MB of real-world CSS): 24 files changed vs. the pre-removal snapshots, net −824 bytes, and several were latent correctness bugs in the old paren-flattening, e.g.calc(100% - (25px - 4px) - 2px)gave100% - 31px(now100% - 23px),calc((100% - 5em)/2)gave100% - 5em/2(now50% - 2.5em),calc(-1 * (1rem + 16px)/2)gave-1rem + 8px(now-.5rem - 8px).