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; <div foo>{{a}}</div> }` was reported as preventing
`<div foo>` 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
This commit is contained in:
Amine
2026-10-06 12:59:20 -04:00
committed by Kirill Cherkashin
parent fc3dc890aa
commit 439d495aaa
2 changed files with 75 additions and 0 deletions
@@ -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: '<ng-content/> <ng-content select="[foo]"/>',
})
class Comp {}
@Component({
imports: [Comp],
template: \`
<comp>
@if (true) {
@let value = 1;
<div foo>{{value}}</div>
}
</comp>
\`,
})
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: '<ng-content/> <ng-content select="[foo]"/>',
})
class Comp {}
@Component({
imports: [Comp],
template: \`
<comp>
@if (true) {
@let value = 1;
<div foo>{{value}}</div>
breaks projection
}
</comp>
\`,
})
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',
@@ -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