Repository navigation
Lint zeros in math functions by their type - #547
Arron Eicholz (arronei) wants to merge 2 commits into
Conversation
Inside a math function a number token is always a <number>, never a "unitless 0" <length> (css-values-4, Type Checking). So a zero length there needs its unit, and a unitless 0 where a length is needed makes the declaration invalid. zeroUnits: stop suggesting to drop the unit there. The rule only skipped a zero whose nearest enclosing function is `calc` (vscode#46997), so it still flagged: - a var() fallback inside calc: calc(var(--h, 0px) / 2) - zeros in min(), max(), clamp() and the other math functions - CALC() in another case Now any enclosing math function counts, compared case-insensitively. unitlessZeroInMath (new, warning): flag a unitless 0 that has to be a dimension: - next to a dimension in + or -, or in min(), max(), clamp(), hypot(), round(), mod(), rem() or abs(): calc(0 + 5px), max(0, 10vw) - in a math function that is the whole value of a property that takes a length or percentage but not a number: margin-block: calc(var(--h, 0) / -2) var() fallbacks count, because var() substitutes them. A zero that is a multiplier, a divisor or an argument of a number function stays a <number> and isn't flagged. Expressions with Sass or Less variables are skipped, because their math lets unitless numbers mix with lengths. The property's type comes from its restrictions. Newer properties such as margin-block, inset and gap have none, so it falls back to their MDN syntax.
Record the draft PR links in docs/upstream-prs.md and the README: microsoft/vscode-custom-data#147 and #148, mdn/data#1121 and microsoft/vscode-css-languageservice#547. Drop the Claude attribution and session lines from the PR folders' COMMIT_MSG.txt and PR.md, and update the doc-paths PR body: the Nov 12 web custom data update filled the cited descriptions by hand, but the lookup still returns nothing on main.
There was a problem hiding this comment.
Note
Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.
🟡 Changes recommended
Code review approval is blocked because there is a critical null-safety bug in src/services/lint.ts where decl?.getFullPropertyName().toLowerCase() can still throw and crash the linter in cases like calc()/var() outside declarations, alongside a minor maintainability nit about an unclear "ponytail" comment tag.
2 open issues
What changed in this PR
Aligns CSS linter behavior with CSS Values typed math rules so unitless 0 is treated correctly inside math functions and introduces a targeted warning when a unit is required.
Changes:
- Expand
zeroUnitssuppression so the linter no longer suggests dropping units for zeros inside any enclosing math function (case-insensitive). - Add a new
unitlessZeroInMathlint rule to warn on unitless0only when the expression/property requires a dimension. - Add CSS and SCSS regression tests covering both the updated
zeroUnitsbehavior and the new rule.
| File | Description |
|---|---|
| src/test/scss/lint.test.ts | Adds SCSS test coverage for unitlessZeroInMath and updated zero-in-math behavior, including preprocessor-specific cases. |
| src/test/css/lint.test.ts | Adds CSS tests validating the updated zeroUnits suppression and new unitlessZeroInMath warnings in typed math expressions. |
| src/services/lintRules.ts | Registers the new unitlessZeroInMath lint rule and defines its default diagnostic messaging/severity. |
| src/services/lint.ts | Implements math-function detection updates for zeroUnits and adds typed analysis to flag unitless zeros that must be dimensions in math contexts. |
Review effort: Balanced
Martin Aeschlimann (aeschli)
left a comment
There was a problem hiding this comment.
Two reproduced issues to address before merging
1. High priority: exponential validation work for nested calculations. productType() calls mathType() when filtering non-number factors, then calls it again on the same dimension factor. Nested parenthesized expressions multiply this repeated traversal.
Reproduction: generate valid CSS with const text = "a { width: calc(" + "(".repeat(20) + "1px" + ")".repeat(20) + "); }";, parse it with the CSS language service, then time doValidation() alone. On this machine the current main completed in about 1.6 ms, while this head exceeded a 3-second subprocess timeout. At depth 16 this head took about 344 ms. There are no zero values in this input. Please cache per-node type results or otherwise ensure bounded traversal, and add a focused nested-expression performance regression.
Location: src/services/lint.ts, productType() around lines 70-75 and recursive mathType().
2. The new warning flags valid Sass even without Sass variables. SCSS validation now reports unitlessZeroInMath for both:
a { width: max(0, 1px); }
a { width: calc(0); }Sass permits unit/unitless mixing in legacy-compatible min()/max() calculations, and simplifies calc(0) to the ordinary number 0. Skipping expressions only when they contain preprocessor variables therefore does not prevent these false positives. Please make the inference dialect-aware and add SCSS cases without variables. Sass documents the calculation simplification and legacy compatibility rules here: https://sass-lang.com/documentation/values/calculations/
These are separate from the earlier optional-chaining comment: the author's rebuttal to that comment is correct; I did not reproduce a null-declaration crash.
Reviewed commit: c0b8d67. Existing full tests pass, but do not cover these cases.


Inside a math function, a number token is always a
<number>. CSS Values 4: "unitless 0<length>s aren't supported in math functions", andwidth: calc(0 + 5px)is invalid. So a zero length in a math function needs its unit. Today the linter gets this backwards in both directions:var()substitutes its fallback, so with--glyph-heightunset the second line iscalc(0 / -2), a<number>, whichmargin-blockdoesn't accept. In Edge with--glyph-height: 12pxand no--glyph-descent,vertical-align: calc(var(--glyph-height, 0px) / 2 - var(--glyph-descent, 0px))computes to6px, and the same declaration with0computes tobaseline, because it is thrown out.zeroUnits (
src/services/lint.ts)microsoft/vscode#46997 stopped the hint for a zero whose nearest enclosing function is
calc. A zero in avar()fallback insidecalc, zeros inmin(),max(),clamp()and the other math functions, andCALC()still get it. Now any enclosing math function counts, compared case-insensitively. The list covers CSS Values 4 (calc,min,max,clamp,round,mod,rem, the trig, exponential and sign functions),-webkit-calcand-moz-calc, and the Values 5 functionscalc-size,progress,media-progress,container-progressandrandom.unitlessZeroInMath (new rule, warning;
src/services/lint.ts,lintRules.ts)It follows the spec's type checking and flags a unitless
0that has to be a dimension:min(),max(),clamp(),hypot(),round(),mod(),rem()andabs()need consistent arguments. So a0next to a dimension is flagged, wherever the math function is:calc(0 + 5px),max(0, 10vw),translate(calc(0 + 1px)).0that carries that type is flagged:width: calc(0),margin-block: calc(var(--h, 0) / -2).What isn't flagged:
0that stays a<number>: a multiplier (calc(0 * 10px),calc(var(--n, 0) * 1px)), a divisor, an argument ofpow(),sign()and other number functions, or a value of a property that takes numbers (line-height: max(0, 1.5))./may divide by a dimension (typed arithmetic), socalc(var(--a, 0) / var(--b))is skipped. Expressions with Sass or Less variables are skipped too, because their math lets unitless numbers mix with lengths.The parser chains
+ - * /left to right without precedence, so the check regroups them into products first.NumericValue.getValue()reads no sign, so-2comes back as unit-2. The check reads the number from the text instead.Property types come from
restrictions. 262 properties have none, includingmargin-block,padding-inline,inset,gapandscroll-margin-*, so the check falls back to the MDNsyntax(<'margin-top'>{1,2}) and resolves property references.stroke-widthandstroke-dashoffsetare listed as length-only but also take a<number>, so they're excluded.Checks
<number>at the top level.node_modules).Result
margin-block: calc(var(--glyph-height, 0px) / -2)margin-block: calc(var(--glyph-height, 0) / -2)vertical-align: calc(var(--h, 0) / 2 - var(--d, 0))CALC(0px - 10vw)width: max(0px, 10vw - 20px)width: max(0, 10vw - 20px)width: calc(0 + 5px),width: calc(0)width: calc(0 * 10px)line-height: max(0, 1.5)width: var(--w, 0px),translate(0px, 10px)Follow-up: a
css.lint.unitlessZeroInMathsetting (and thescss/lessones) in microsoft/vscodeextensions/css-language-features/package.json, so the rule shows up in Settings.