Skip to content

Commit 812463c

Browse files
committed
fix(compiler-cli): Ignore diagnostics on ngTemplateContextGuard lines in TCB (#63054)
Deprecated diagnostics can appear on the context guard becaues the directive itself may be deprecated. For example, context guards look like `if (i1.NgForOf.ngTemplateContextGuard(_t8, _t9) /*331,378*/) {...}` and typescript will report the deprecation on the whole element, from start to end tag, because the span for that node includes it. fixes angular/vscode-ng-language-service#2203 PR Close #63054
1 parent 45b030b commit 812463c

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

packages/compiler-cli/src/ngtsc/typecheck/src/type_check_block.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ class TcbTemplateBodyOp extends TcbOp {
598598
if (this.tcb.env.config.applyTemplateContextGuards) {
599599
const ctx = this.scope.resolve(hostNode);
600600
const guardInvoke = tsCallMethod(dirId, 'ngTemplateContextGuard', [dirInstId, ctx]);
601+
markIgnoreDiagnostics(guardInvoke);
601602
addParseSpanInfo(guardInvoke, hostNode.sourceSpan);
602603
guards.push(guardInvoke);
603604
} else if (

packages/language-service/test/diagnostic_spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,45 @@ describe('getSuggestedDiagnostics', () => {
767767
const diags = project.getSuggestionDiagnosticsForFile('app.ts');
768768
expect(diags.length).toBe(0);
769769
});
770+
771+
it('should not report deprecated for directive context guard', () => {
772+
const files = {
773+
'app.ts': `
774+
import {Component} from '@angular/core';
775+
776+
@Component({
777+
template: \`
778+
<div *my-directive>
779+
<span>Test</span>
780+
<span>Test</span>
781+
<span>Test</span>
782+
</div>
783+
\`,
784+
standalone: false,
785+
})
786+
export class AppComponent {}
787+
`,
788+
'bar.ts': `
789+
import {Directive, input} from '@angular/core';
790+
/**
791+
* @deprecated deprecated
792+
*/
793+
@Directive({
794+
selector: '[my-directive]',
795+
standalone: false,
796+
})
797+
export class MyDirective {
798+
static ngTemplateContextGuard(dir: MyDirective, ctx: any): true {
799+
return true;
800+
}
801+
}
802+
`,
803+
};
804+
const project = createModuleAndProjectWithDeclarations(env, 'test', files);
805+
806+
const diags = project.getSuggestionDiagnosticsForFile('app.ts');
807+
expect(diags.length).toBe(0);
808+
});
770809
});
771810

772811
function getTextOfDiagnostic(diag: ts.Diagnostic): string {

0 commit comments

Comments
 (0)