Skip to content

fix: avoid TypeError on nested @supports with dumpLineNumbers#4446

Merged
matthew-dean merged 1 commit into
less:masterfrom
sarathfrancis90:fix/debug-info-nested-supports
Jun 14, 2026
Merged

fix: avoid TypeError on nested @supports with dumpLineNumbers#4446
matthew-dean merged 1 commit into
less:masterfrom
sarathfrancis90:fix/debug-info-nested-supports

Conversation

@sarathfrancis90

@sarathfrancis90 sarathfrancis90 commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

What:

Rendering valid Less with dumpLineNumbers enabled throws a TypeError when a @supports (or @document) block is nested inside a selector, e.g.

.foo {
  @supports (display: grid) {
    margin: 0;
  }
}
> less.render(src, { dumpLineNumbers: "comments" })
TypeError: Cannot read properties of undefined (reading 'lineNumber')

It happens in all three modes (comments, mediaquery, all). Top-level @supports, and @media/@container, are unaffected. This is the long-standing #3073.

Why:

A nested @supports/@document ends up as an implicit, non-root ruleset that never gets a debugInfo attached during parsing (unlike @media, which threads one through). When that ruleset renders, debug-info.js reads lineNumber/fileName off the missing debugInfo and throws instead of producing CSS.

The fix just skips emitting debug info for a node that has none — the same outcome you already get for any node without a recorded line, so nodes that do have line info are unchanged.

I added a nested @supports block to the shared linenumbers.less fixture; it reproduces the throw across the comments/mediaquery/all expectations before the change and passes after. grunt test:node is green.

Checklist:

  • Documentation N/A
  • Added/updated unit tests
  • Code complete

Summary by CodeRabbit

  • Bug Fixes

    • Improved debug information handling to prevent unnecessary output when debug context is unavailable.
  • Tests

    • Expanded test coverage for CSS @supports conditional feature with enhanced line number mapping across multiple debug output formats.

A nested @supports (or @document) builds an implicit, non-root ruleset
that never gets a debugInfo attached during parsing. With dumpLineNumbers
enabled, genCSS tried to read lineNumber/fileName off that missing
debugInfo and threw a TypeError instead of producing output.

Skip emitting debug info when a node has none, the same way nodes without
a recorded line are already handled elsewhere.
@dosubot dosubot Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Jun 12, 2026
@coderabbitai

coderabbitai Bot commented Jun 12, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The PR modifies the Less compiler's debug-info module to safely skip output generation when debug metadata is unavailable, and adds comprehensive test fixtures that verify @supports CSS rule blocks compile correctly with proper line-number annotations across all debug output modes (all, comments, and mediaquery).

Changes

@supports rule debug info

Layer / File(s) Summary
Debug info guard
packages/less/lib/less/tree/debug-info.js
debugInfo adds a guard to check ctx.debugInfo before emitting output, with comments explaining that some parsed nodes lack debug metadata and have no line number to emit.
@supports test fixtures and expected outputs
packages/test-data/tests-config/debug/linenumbers.less, packages/test-data/tests-config/debug/all/linenumbers-all.css, packages/test-data/tests-config/debug/comments/linenumbers-comments.css, packages/test-data/tests-config/debug/mediaquery/linenumbers-mediaquery.css
Source Less file introduces an .supports-rule block with an @supports (display: grid) conditional and nested color rules; expected outputs verify the compiled results include correct line-number comments and debug-info media queries across all debug modes.

🎯 2 (Simple) | ⏱️ ~12 minutes

size:L

🐰 A CSS grid support emerges, line numbers dance free,
Debug guards now handle what some nodes cannot see,
Test fixtures bloom bright in modes all and three,
Nested colors green and blue, just as they should be! 🌱

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely describes the main fix: preventing a TypeError when @supports blocks are nested with dumpLineNumbers enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/less/lib/less/tree/debug-info.js (1)

10-12: 🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win

Update the typedef to reflect that debugInfo is optional.

The DebugInfoContext typedef declares debugInfo as a required property, but the new guard on Line 64 allows ctx.debugInfo to be undefined. This creates a type mismatch between the signature and the implementation.

📝 Proposed fix to make debugInfo optional in the typedef
 /**
  * `@typedef` {object} DebugInfoContext
- * `@property` {DebugInfoData} debugInfo
+ * `@property` {DebugInfoData} [debugInfo]
  */
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/less/lib/less/tree/debug-info.js` around lines 10 - 12, The typedef
for DebugInfoContext currently declares debugInfo as required but the code (see
guard using ctx.debugInfo in debug-info.js) permits it to be undefined; update
the JSDoc typedef to mark debugInfo optional (e.g., change the `@property` to use
the optional syntax like [debugInfo] or DebugInfoData=) so the type matches the
implementation and references to DebugInfoContext/documentation reflect that
ctx.debugInfo may be absent.
🧹 Nitpick comments (1)
packages/test-data/tests-config/debug/linenumbers.less (1)

35-42: ⚡ Quick win

Consider adding a test case for nested @document as well.

The PR description mentions that both @supports and @document are affected by this issue. While the fix should handle both, adding an explicit test case for @document would provide more comprehensive coverage.

📋 Example test case for `@document`
.document-rule {
  `@document` url(https://example.com/) {
    color: purple;
    .nested-document {
      color: orange;
    }
  }
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/test-data/tests-config/debug/linenumbers.less` around lines 35 - 42,
Add a parallel test case covering nested `@document` rules: create a new block
similar to .supports-rule but using `@document` (use the selector .document-rule
and nested .nested-document) containing a top-level declaration and a nested
declaration to mirror the structure in the diff; ensure the new test sits
alongside the existing .supports-rule test so the parser/transformer behavior
for `@document` is exercised the same way as for `@supports`.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@packages/less/lib/less/tree/debug-info.js`:
- Around line 10-12: The typedef for DebugInfoContext currently declares
debugInfo as required but the code (see guard using ctx.debugInfo in
debug-info.js) permits it to be undefined; update the JSDoc typedef to mark
debugInfo optional (e.g., change the `@property` to use the optional syntax like
[debugInfo] or DebugInfoData=) so the type matches the implementation and
references to DebugInfoContext/documentation reflect that ctx.debugInfo may be
absent.

---

Nitpick comments:
In `@packages/test-data/tests-config/debug/linenumbers.less`:
- Around line 35-42: Add a parallel test case covering nested `@document` rules:
create a new block similar to .supports-rule but using `@document` (use the
selector .document-rule and nested .nested-document) containing a top-level
declaration and a nested declaration to mirror the structure in the diff; ensure
the new test sits alongside the existing .supports-rule test so the
parser/transformer behavior for `@document` is exercised the same way as for
`@supports`.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: a506d09b-39ee-4881-ad55-a75fdeb7a6d2

📥 Commits

Reviewing files that changed from the base of the PR and between f4a63f2 and 575136b.

📒 Files selected for processing (5)
  • packages/less/lib/less/tree/debug-info.js
  • packages/test-data/tests-config/debug/all/linenumbers-all.css
  • packages/test-data/tests-config/debug/comments/linenumbers-comments.css
  • packages/test-data/tests-config/debug/linenumbers.less
  • packages/test-data/tests-config/debug/mediaquery/linenumbers-mediaquery.css

@matthew-dean matthew-dean merged commit d03e7a4 into less:master Jun 14, 2026
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants