From 439d495aaa53ebce35e46464a08b5fb10a5ecd61 Mon Sep 17 00:00:00 2001 From: Amine Date: Sun, 4 Oct 2026 09:38:12 +0200 Subject: [PATCH] fix(compiler): ignore `@let` declarations in the control flow content projection diagnostic The `controlFlowPreventingContentProjection` diagnostic (NG8011) counted `@let` declarations as root nodes of a control flow block, so a block like `@if (x) { @let a = 1;
{{a}}
}` was reported as preventing `
` from being projected into its slot. The compiler skips `@let` declarations when it picks the node to project (since #58607), so the node is projected correctly and the warning is a false positive. Skip `@let` declarations when counting root nodes, like the compiler does. Fixes #71172 --- .../test/ngtsc/template_typecheck_spec.ts | 69 +++++++++++++++++++ .../src/typecheck/ops/content_projection.ts | 6 ++ 2 files changed, 75 insertions(+) diff --git a/packages/compiler-cli/test/ngtsc/template_typecheck_spec.ts b/packages/compiler-cli/test/ngtsc/template_typecheck_spec.ts index 90985be69ce..f27c5d878d1 100644 --- a/packages/compiler-cli/test/ngtsc/template_typecheck_spec.ts +++ b/packages/compiler-cli/test/ngtsc/template_typecheck_spec.ts @@ -8233,6 +8233,75 @@ suppress expect(diags.length).toBe(0); }); + it('should not report when there are @let declarations at the root of the control flow node', () => { + env.write( + 'test.ts', + ` + import {Component} from '@angular/core'; + + @Component({ + selector: 'comp', + template: ' ', + }) + class Comp {} + + @Component({ + imports: [Comp], + template: \` + + @if (true) { + @let value = 1; +
{{value}}
+ } +
+ \`, + }) + class TestCmp {} + `, + ); + + const diags = env.driveDiagnostics(); + expect(diags.length).toBe(0); + }); + + it('should report when there are @let declarations and multiple nodes at the root of the control flow node', () => { + env.write( + 'test.ts', + ` + import {Component} from '@angular/core'; + + @Component({ + selector: 'comp', + template: ' ', + }) + class Comp {} + + @Component({ + imports: [Comp], + template: \` + + @if (true) { + @let value = 1; +
{{value}}
+ breaks projection + } +
+ \`, + }) + class TestCmp {} + `, + ); + + const diags = env + .driveDiagnostics() + .map((d) => ts.flattenDiagnosticMessageText(d.messageText, '')); + expect(diags.length).toBe(1); + expect(diags[0]).toContain( + `Node matches the "[foo]" slot of the "Comp" component, but will ` + + `not be projected into the specific slot because the surrounding @if has more than one node at its root.`, + ); + }); + it('should not report when the component only has a catch-all slot', () => { env.write( 'test.ts', diff --git a/packages/compiler/src/typecheck/ops/content_projection.ts b/packages/compiler/src/typecheck/ops/content_projection.ts index 9df54ae5be3..f6732af269d 100644 --- a/packages/compiler/src/typecheck/ops/content_projection.ts +++ b/packages/compiler/src/typecheck/ops/content_projection.ts @@ -14,6 +14,7 @@ import { ForLoopBlockEmpty, IfBlock, IfBlockBranch, + LetDeclaration, Node, SwitchBlock, SwitchBlockCaseGroup, @@ -134,6 +135,11 @@ export class TcbControlFlowContentProjectionOp extends TcbOp { // Check the number of root nodes while skipping empty text where relevant. for (const child of node.children) { + // `@let` declarations don't render anything so they don't affect content projection. + if (child instanceof LetDeclaration) { + continue; + } + // Normally `preserveWhitspaces` would have been accounted for during parsing, however // in `ngtsc/annotations/component/src/resources.ts#parseExtractedTemplate` we enable // `preserveWhitespaces` to preserve the accuracy of source maps diagnostics. This means