mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
refactor(compiler-cli): move component out of TemplateContext (#43232)
Move `component` out of the `TemplateContext` so the context can be reused for multiple components. Refs #42966 PR Close #43232
This commit is contained in:
parent
50249827f1
commit
dee04bd96e
3 changed files with 16 additions and 19 deletions
|
|
@ -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>[];
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
@ -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}'.
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Reference in a new issue