diff --git a/packages/compiler-cli/src/ngtsc/typecheck/extended/api/api.ts b/packages/compiler-cli/src/ngtsc/typecheck/extended/api/api.ts index 0daee3242f5..23a7777ba24 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/extended/api/api.ts +++ b/packages/compiler-cli/src/ngtsc/typecheck/extended/api/api.ts @@ -21,7 +21,8 @@ export interface TemplateCheck { code: T; /** Runs check and returns information about the diagnostics to be generated. */ - run(ctx: TemplateContext, template: TmplAstNode[]): NgTemplateDiagnostic[]; + run(ctx: TemplateContext, component: ts.ClassDeclaration, + template: TmplAstNode[]): NgTemplateDiagnostic[]; } /** @@ -36,7 +37,4 @@ export interface TemplateContext { * in the template (it is not to query types outside the Angular component). */ typeChecker: ts.TypeChecker; - - /** The `@Component()` class from which the template was obtained. */ - component: ts.ClassDeclaration; } diff --git a/packages/compiler-cli/src/ngtsc/typecheck/extended/checks/invalid_banana_in_box/index.ts b/packages/compiler-cli/src/ngtsc/typecheck/extended/checks/invalid_banana_in_box/index.ts index 14b2ae1b186..800c6081e67 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/extended/checks/invalid_banana_in_box/index.ts +++ b/packages/compiler-cli/src/ngtsc/typecheck/extended/checks/invalid_banana_in_box/index.ts @@ -21,9 +21,9 @@ import {TemplateCheck, TemplateContext} from '../../api'; export class InvalidBananaInBoxCheck implements TemplateCheck { code: ErrorCode.INVALID_BANANA_IN_BOX = 8101; - run(ctx: TemplateContext, + run(ctx: TemplateContext, component: ts.ClassDeclaration, template: TmplAstNode[]): NgTemplateDiagnostic[] { - const visitor = new BananaVisitor(ctx); + const visitor = new BananaVisitor(ctx, component); return visitor.getDiagnostics(template); } @@ -32,7 +32,8 @@ export class InvalidBananaInBoxCheck implements TemplateCheck[] = []; - constructor(public readonly ctx: TemplateContext) { + constructor( + public readonly ctx: TemplateContext, private readonly component: ts.ClassDeclaration) { super(); } @@ -48,7 +49,7 @@ class BananaVisitor extends TmplAstRecursiveVisitor { const boundSyntax = boundEvent.sourceSpan.toString(); const expectedBoundSyntax = boundSyntax.replace(`(${name})`, `[(${name.slice(1, -1)})]`); this.diagnostics.push(this.ctx.templateTypeChecker.makeTemplateDiagnostic( - this.ctx.component, boundEvent.sourceSpan, ts.DiagnosticCategory.Warning, + this.component, boundEvent.sourceSpan, ts.DiagnosticCategory.Warning, ErrorCode.INVALID_BANANA_IN_BOX, `In the two-way binding syntax the parentheses should be inside the brackets, ex. '${ expectedBoundSyntax}'. diff --git a/packages/compiler-cli/src/ngtsc/typecheck/extended/src/extended_template_checker.ts b/packages/compiler-cli/src/ngtsc/typecheck/extended/src/extended_template_checker.ts index c6e139000f0..9305f1f2064 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/extended/src/extended_template_checker.ts +++ b/packages/compiler-cli/src/ngtsc/typecheck/extended/src/extended_template_checker.ts @@ -13,13 +13,17 @@ import {TemplateDiagnostic, TemplateTypeChecker} from '../../api'; import {ExtendedTemplateChecker, TemplateCheck, TemplateContext} from '../api'; export class ExtendedTemplateCheckerImpl implements ExtendedTemplateChecker { + private ctx: TemplateContext; + constructor( - private readonly templateTypeChecker: TemplateTypeChecker, - private readonly typeChecker: ts.TypeChecker, - private readonly templateChecks: TemplateCheck[]) {} + templateTypeChecker: TemplateTypeChecker, typeChecker: ts.TypeChecker, + private readonly templateChecks: TemplateCheck[]) { + this.ctx = {templateTypeChecker: templateTypeChecker, typeChecker: typeChecker} as + TemplateContext; + } getDiagnosticsForComponent(component: ts.ClassDeclaration): TemplateDiagnostic[] { - const template = this.templateTypeChecker.getTemplate(component); + const template = this.ctx.templateTypeChecker.getTemplate(component); // Skip checks if component has no template. This can happen if the user writes a // `@Component()` but doesn't add the template, could happen in the language service // when users are in the middle of typing code. @@ -28,14 +32,8 @@ export class ExtendedTemplateCheckerImpl implements ExtendedTemplateChecker { } const diagnostics: TemplateDiagnostic[] = []; - const ctx = { - templateTypeChecker: this.templateTypeChecker, - typeChecker: this.typeChecker, - component - } as TemplateContext; - for (const check of this.templateChecks) { - diagnostics.push(...deduplicateDiagnostics(check.run(ctx, template))); + diagnostics.push(...deduplicateDiagnostics(check.run(this.ctx, component, template))); } return diagnostics;