Evaluation and CSS generation
Less turns the parsed tree into CSS by first evaluating Less meaning in scoped contexts and then running ordered tree visitors that build selectors, apply extensions, remove hidden nodes, and prepare output. The final tree is emitted as CSS, with optional source-map data that links generated text back to Less files. This page describes the middle and final stages of the compiler pipeline; see parser and syntax tree for the input tree and built-in functions and values for value evaluation.
Evaluation model
Evaluation changes a syntax tree into a tree whose values, conditions, mixin calls, imports, and nested at-rules have meaning for the current scope. The evaluator keeps this work separate from CSS emission so that Less constructs can resolve before the compiler decides which nodes and selectors belong in the output.
Evaluation contexts
An evaluation context is the shared state for one tree evaluation. It carries the active frame stack for scope lookup, function registries, math and unit rules, important-state propagation, import paths, URL rules, and state used to combine nested media blocks. packages/…/contexts.js defines this state and its path and expression helpers.
Rulesets use the frame stack as the current Less scope. A ruleset evaluates its selectors and body, adds itself to the stack, expands imports and mixin calls, evaluates declarations and other nodes, then removes its frame. This lets nested rules and mixins see the right variables and functions without making the output visitor responsible for Less meaning. The scope model connects to variables, mixins, and namespaces.
References: packages/…/contexts.js• L44-155 packages/…/ruleset.js• L117-287
Tree evaluation
packages/…/transform-tree.js creates one evaluation context and evaluates the root before it runs the built-in transformation passes. External variables can enter as an initial ruleset frame, so callers can provide values without changing the source tree. The result is an evaluated tree that still has structure such as rulesets and at-rules, but no longer depends on unresolved Less expressions for normal output.
The render path constructs a ParseTree from the parsed root and calls toCSS. That method catches evaluation and output errors, so the public result reports compiler errors with the import set that produced the tree. It also applies registered post-processors after CSS generation. The parent compiler pipeline describes the stages before this handoff.
References: packages/…/transform-tree.js• L10-101 packages/…/render.js• L24-37 packages/…/parse-tree.js• L12-35
Visitor passes
Visitors provide the ordered tree transformations between evaluation and CSS generation. A visitor can inspect a node before its children, skip child traversal, replace a node, or return arrays that the generic visitor flattens into the surrounding rule list.
Visitor traversal
packages/…/visitor.js supplies the common traversal contract. It maps each tree node type to visit<Type> and visit<Type>Out hooks, then walks child nodes through each node’s accept method. Replacing visitors can remove nodes or expand one node into many, which is how mixin results and compiled nested rules become ordinary output rules.
The built-in passes run in this order: selector joining, visibility marking, extend processing, and CSS preparation. Plugin visitors can run before evaluation or join the built-in sequence, so a pass that changes selectors or visibility must preserve the contracts expected by later passes.
References: packages/…/visitor.js• L33-129 packages/…/transform-tree.js• L44-89
Selector and at-rule transformation
Evaluation leaves nested selectors and at-rules in a form that still reflects Less nesting. The transformation passes turn that structure into selector paths and at-rule bodies that CSS can express while preserving the relationships between parent scopes and nested output.
Selector paths
A selector path is the ordered set of selectors needed to reach one nested ruleset. The selector visitor resolves & and ordinary nesting against the active parent paths, expands selector combinations, and stores the resulting paths on each ruleset. The later CSS visitor removes paths that cannot produce output and writes each remaining path as one CSS selector chain.
This path model is the main boundary between Less nesting and CSS nesting output. It also gives extend processing a stable sequence of selector elements to match. More language-level detail belongs in selectors, extends, and at-rules.
References: packages/…/join-selector-visitor.js• L25-62 packages/…/ruleset.js• L673-1065 packages/…/selector.js• L156-199
Extend relationships
An extend relationship adds the extending selector to rulesets that match a target selector. ExtendVisitor first collects extends against the joined selector paths, expands chains such as one selector extending another, and then adds derived paths to matching rulesets. It keeps the work within the relevant at-rule scope so an extend does not cross an incompatible media or at-rule boundary.
The process supports chained extensions and warns when a declared extend matches nothing. It also stops circular chains after a bounded number of iterations and raises an error when the chain is detected. The resulting selectors remain part of the same CSS ruleset, so declarations do not need to be copied into separate blocks.
References: packages/…/extend-visitor.js• L106-143 packages/…/extend-visitor.js• L146-245 packages/…/extend-visitor.js• L260-296
Visibility state
Visibility state controls whether a tree node or selector can reach CSS output. The visibility pass marks eligible tree nodes, while visibility blocks preserve nodes that must stay internal during evaluation, such as definitions or content used by another Less construct. Derived selectors and mixin replacements carry this state forward.
The CSS visitor uses that state to discard hidden declarations, comments, imports, selectors, empty rulesets, and empty at-rules. It can keep a container when a hidden child has become relevant through a reference such as an extend. This two-step model lets evaluation retain source structure long enough for references to resolve without leaking that structure into CSS.
References: packages/…/set-tree-visibility-visitor.js• L3-54 packages/…/node.js• L251-301 packages/…/to-css-visitor.js• L15-83 packages/…/mixin-call.js• L252-264
At-rule composition
Nested at-rules carry their own conditions and output bodies, so they need composition rules in addition to selector joining. Nested media blocks combine their condition paths and can bubble selectors into the surrounding context; other nestable at-rules preserve their boundary while their bodies are evaluated and filtered.
During CSS preparation, at-rule bodies are visited, merged where the CSS form allows it, and removed when no visible content remains. This keeps media and other at-rule output valid while allowing nested Less rules to produce selectors in the correct scope.
References: packages/…/media.js• L55-84 packages/…/nested-at-rule.js• L122-236 packages/…/to-css-visitor.js• L121-200
CSS output
CSS generation is the final tree pass and serialization step. It converts the transformed tree into valid CSS text while applying output policy such as visibility, rule merging, duplicate removal, formatting, and compression.
CSS preparation
ToCSSVisitor is a replacing visitor that prepares the tree for serialization. It compiles selector paths, moves nested rulesets into output positions, merges compatible declarations, removes duplicate declarations, and rejects nodes that are not valid at the root or inside a CSS rule. The pass returns a smaller tree whose remaining nodes are intended for CSS output.
The ruleset generator then writes selectors and declarations in tree order. It handles root rulesets without braces, writes nested rules with indentation unless compression is enabled, and tracks the last rule so separators and semicolons remain correct. The output context carries formatting state while each node emits its own CSS text.
References: packages/…/to-css-visitor.js• L86-129 packages/…/to-css-visitor.js• L203-329 packages/…/ruleset.js• L547-666
Source map output
When source maps are enabled, CSS is emitted through a source-map output sink instead of a plain string sink. Each emitted chunk can carry the originating file and source index, so the sink records generated positions against Less input positions and imported source contents.
ParseTree.toCSS normalizes source-map options and returns the external map with the CSS. The builder can append an external source-map URL or an inline data URL, and it can include normalized source contents. If an emitted node has no matching source content, its CSS still appears but that chunk cannot receive a normal source mapping.
References: packages/…/parse-tree.js• L36-119 packages/…/source-map-builder.js• L7-78 packages/…/source-map-output.js• L48-147
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the visitor order aligned with the data each pass creates: joined selector paths must exist before extends run, and visibility must be settled before CSS filtering.
- If an evaluation-context field changes, check the node evaluators that read it and the render options that populate it. Math, scope frames, media state, and source-map flags cross those boundaries.
- If selector path shape changes, review both extend matching and ruleset CSS generation. The two stages consume the same path structure but apply different rules.
- If visibility rules change, check mixin replacement visibility and the CSS visitor’s handling of silent or referenced children. A node can be needed for evaluation without being direct CSS output.
- If source locations or output sinks change, keep node file information, imported source contents, ignored-character offsets, and generated map filenames in agreement.
- If at-rule bubbling changes, check selector context and nested media composition together. A valid nested tree can still produce the wrong selector scope if only one side changes.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A selector-changing pass runs without the selector paths expected by extend processing | Extends do not match the intended nested selectors or add the wrong paths | Preserve the selector-join pass before ExtendVisitor and keep path shape stable |
| An extend chain is circular or grows without end | Compilation stops with a circular-extend error | The extend processor tracks parent IDs and has a bounded chain check |
| A hidden node is made visible without respecting its visibility block | Definitions or reference-only content can leak into CSS | Apply visibility through the node visibility APIs and let ToCSSVisitor resolve containers |
| A root declaration or an invalid function result reaches CSS preparation | CSS generation raises a compiler error instead of emitting invalid CSS | ToCSSVisitor validates root and returned node types before serialization |
| Source-map output lacks the source content for an emitted file | CSS is emitted, but the affected chunk has no normal source mapping | Keep import contents and each node’s file information synchronized with the map sink |
| A nested media or at-rule crosses an incompatible boundary during bubbling | Conditions or selectors are emitted in the wrong at-rule scope | Preserve the at-rule-specific evaluation and bubbling rules |
Key files
| File | Why to read it |
|---|---|
| packages/…/render.js | Shows how parsing hands a root tree to evaluation and CSS generation. |
| packages/…/parse-tree.js | Defines the public CSS result, error boundary, source-map setup, and import list. |
| packages/…/transform-tree.js | Shows the evaluation handoff and the order of built-in and plugin visitor passes. |
| packages/…/contexts.js | Defines the state shared by node evaluation, scope lookup, math, URLs, and nested media handling. |
| packages/…/ruleset.js | Explains scoped evaluation and the final ruleset CSS walk. |
| packages/…/visitor.js | Defines the traversal and node-replacement contract used by all visitors. |
| packages/…/join-selector-visitor.js | Shows how nested Less selectors become selector paths. |
| packages/…/extend-visitor.js | Shows selector matching, extend chaining, and derived selector creation. |
| packages/…/set-tree-visibility-visitor.js | Defines the pass that marks nodes as eligible or ineligible for output. |
| packages/…/to-css-visitor.js | Shows tree cleanup, CSS validity checks, rule merging, and output selection. |
| packages/…/nested-at-rule.js | Explains composition and bubbling for nestable at-rules. |
| packages/…/source-map-builder.js and packages/…/source-map-output.js | Show how emitted CSS chunks become external or inline source-map output. |