Skip to content

Commit

Permalink
refactor(compiler-cli): move component out of TemplateContext (#4…
Browse files Browse the repository at this point in the history
…3232)

Move `component` out of the `TemplateContext` so the context can be
reused for multiple components.

Refs #42966

PR Close #43232
  • Loading branch information
danieltre23 authored and thePunderWoman committed Aug 26, 2021
1 parent e29f1d9 commit debe517
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
6 changes: 2 additions & 4 deletions packages/compiler-cli/src/ngtsc/typecheck/extended/api/api.ts
Expand Up @@ -21,7 +21,8 @@ export interface TemplateCheck<T extends ErrorCode> {
code: T;

/** Runs check and returns information about the diagnostics to be generated. */
run(ctx: TemplateContext, template: TmplAstNode[]): NgTemplateDiagnostic<T>[];
run(ctx: TemplateContext, component: ts.ClassDeclaration,
template: TmplAstNode[]): NgTemplateDiagnostic<T>[];
}

/**
Expand All @@ -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;
}
Expand Up @@ -21,9 +21,9 @@ import {TemplateCheck, TemplateContext} from '../../api';
export class InvalidBananaInBoxCheck implements TemplateCheck<ErrorCode.INVALID_BANANA_IN_BOX> {
code: ErrorCode.INVALID_BANANA_IN_BOX = 8101;

run(ctx: TemplateContext,
run(ctx: TemplateContext, component: ts.ClassDeclaration,
template: TmplAstNode[]): NgTemplateDiagnostic<ErrorCode.INVALID_BANANA_IN_BOX>[] {
const visitor = new BananaVisitor(ctx);
const visitor = new BananaVisitor(ctx, component);

return visitor.getDiagnostics(template);
}
Expand All @@ -32,7 +32,8 @@ export class InvalidBananaInBoxCheck implements TemplateCheck<ErrorCode.INVALID_
class BananaVisitor extends TmplAstRecursiveVisitor {
private diagnostics: NgTemplateDiagnostic<ErrorCode.INVALID_BANANA_IN_BOX>[] = [];

constructor(public readonly ctx: TemplateContext) {
constructor(
public readonly ctx: TemplateContext, private readonly component: ts.ClassDeclaration) {
super();
}

Expand All @@ -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}'.
Expand Down
Expand Up @@ -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<ErrorCode>[]) {}
templateTypeChecker: TemplateTypeChecker, typeChecker: ts.TypeChecker,
private readonly templateChecks: TemplateCheck<ErrorCode>[]) {
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.
Expand All @@ -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;
Expand Down

0 comments on commit debe517

Please sign in to comment.