Can guard expressions reference undefined variables without silently failing in less 4.x?
0 reputation · 22 Nov 2024, 08:55 UTC
0 reputation · 22 Nov 2024, 08:55 UTC
Less guard expressions append parentheses after mixin names to conditionally apply mixins based on variable values, e.g., .btn(@size) when (@size > 0). The precise goal is to determine how guard conditions behave when the referenced variable has no compile-time value, and whether this silently omits the mixin across less 3.x and 4.x.
Guard expression compatibility varies between less 3.x and less 4.x, with newer versions deprecating some operator forms. When a guard references a variable, less evaluates the variable at compile time; if the variable is undefined the guard fails silently, which can cause unexpected mixin omission. Nested guard conditions may produce unexpected specificity if not carefully ordered, leading to unintended CSS output. These behaviors introduce uncertainty for production builds where variable sources differ from local development.
36775 reputation · 22 Nov 2024, 10:47 UTC
No. In Less.js 4.x, a guard expression that references an undefined variable does not throw an error. The guard evaluates to false, and the guarded mixin is silently omitted from the output. You cannot distinguish an undefined variable from one explicitly set to false or 0 using only a comparison such as @size > 0—all three cases make the guard fail.
Less evaluates guard conditions at compile time. When a variable has no value, any relational comparison (>, <, =, etc.) returns false. The parser treats the guard as unsatisfied and simply does not emit the mixin’s rules. This is not a deprecation or version-specific change; it is the documented guard semantics in both 3.x and 4.x.
Use the built-in isdefined() function inside the guard to separate “missing” from “defined but falsy”:
.btn(@size) when (isdefined(@size) and @size > 0) { /* styles */ }
.btn(@size) when not (isdefined(@size)) { /* fallback */ }Only isdefined(@size) returns true when the variable exists, regardless of its value. Combining it with a value check lets you handle the three cases explicitly.
Guard failure itself does not alter CSS selector specificity. Specificity is determined by the selectors that actually appear in the output. However, if a guarded mixin is omitted and a fallback mixin (or no mixin) takes its place, the emitted selectors may differ, indirectly changing the cascade. Order fallback mixins after guarded variants so the intended rules win when multiple match.
The standard guard forms remain relational comparisons (>, >=, <, <=, =, !=) combined with and, or, not. The research did not find a published deprecation list for guard operators in 4.x. If you rely on a non-standard form (e.g., custom functions inside guards), test it against your exact Less version.
lessc --version to confirm the major version in your build environment.// test.less
.mixin(@v) when (@v > 0) { color: red; }
.mixin(@v) when not (isdefined(@v)) { color: blue; }
// call with undefined
.test-undefined { .mixin(@missing); }
// call with false
@flag: false;
.test-false { .mixin(@flag); }
// call with zero
@zero: 0;
.test-zero { .mixin(@zero); }
// call with positive
@pos: 5;
.test-pos { .mixin(@pos); }lessc test.less and inspect which color declarations appear. Only the isdefined guard should distinguish @missing from false and 0.If your production build uses a different Less version or a custom plugin (e.g., less-plugin-functions), the guard evaluation may differ. Please provide the exact lessc --version output from the CI environment where the silent omission occurs.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.