Skip to content

Commit fc85fa8

Browse files
danieltre23thePunderWoman
authored andcommitted
refactor(compiler-cli): use TemplateCheckWithVisitor in InvalidBananaInBoxCheck (#43232)
Modify the existing template check to use the new abstract class. This makes template checks easier to read and understand. Refs #42966 PR Close #43232
1 parent 46e68df commit fc85fa8

2 files changed

Lines changed: 22 additions & 100 deletions

File tree

‎packages/compiler-cli/src/ngtsc/typecheck/extended/checks/invalid_banana_in_box/index.ts‎

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,62 +6,36 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {TmplAstBoundEvent, TmplAstNode, TmplAstRecursiveVisitor} from '@angular/compiler';
9+
import {AST, TmplAstBoundEvent, TmplAstNode} from '@angular/compiler';
1010
import * as ts from 'typescript';
1111

1212
import {ErrorCode} from '../../../../diagnostics';
1313
import {NgTemplateDiagnostic} from '../../../api';
14-
import {TemplateCheck, TemplateContext} from '../../api';
14+
import {TemplateCheckWithVisitor, TemplateContext} from '../../api';
1515

1616
/**
1717
* Ensures the two-way binding syntax is correct.
1818
* Parentheses should be inside the brackets "[()]".
1919
* Will return diagnostic information when "([])" is found.
2020
*/
21-
export class InvalidBananaInBoxCheck implements TemplateCheck<ErrorCode.INVALID_BANANA_IN_BOX> {
22-
code: ErrorCode.INVALID_BANANA_IN_BOX = 8101;
23-
24-
run(ctx: TemplateContext, component: ts.ClassDeclaration,
25-
template: TmplAstNode[]): NgTemplateDiagnostic<ErrorCode.INVALID_BANANA_IN_BOX>[] {
26-
const visitor = new BananaVisitor(ctx, component);
27-
28-
return visitor.getDiagnostics(template);
29-
}
30-
}
31-
32-
class BananaVisitor extends TmplAstRecursiveVisitor {
33-
private diagnostics: NgTemplateDiagnostic<ErrorCode.INVALID_BANANA_IN_BOX>[] = [];
34-
35-
constructor(
36-
public readonly ctx: TemplateContext, private readonly component: ts.ClassDeclaration) {
37-
super();
38-
}
39-
40-
/**
41-
* Check for outputs with names surrounded in brackets "[]".
42-
* The syntax '([foo])="bar"' would be interpreted as an @Output()
43-
* with name '[foo]'. Just like '(foo)="bar"' would have the name 'foo'.
44-
* Generate diagnostic information for the cases found.
45-
*/
46-
override visitBoundEvent(boundEvent: TmplAstBoundEvent) {
47-
const name = boundEvent.name;
48-
if (name.startsWith('[') && name.endsWith(']')) {
49-
const boundSyntax = boundEvent.sourceSpan.toString();
50-
const expectedBoundSyntax = boundSyntax.replace(`(${name})`, `[(${name.slice(1, -1)})]`);
51-
this.diagnostics.push(this.ctx.templateTypeChecker.makeTemplateDiagnostic(
52-
this.component, boundEvent.sourceSpan, ts.DiagnosticCategory.Warning,
53-
ErrorCode.INVALID_BANANA_IN_BOX,
54-
`In the two-way binding syntax the parentheses should be inside the brackets, ex. '${
55-
expectedBoundSyntax}'.
56-
Find more at https://angular.io/guide/two-way-binding`));
57-
}
58-
}
59-
60-
getDiagnostics(template: TmplAstNode[]): NgTemplateDiagnostic<ErrorCode.INVALID_BANANA_IN_BOX>[] {
61-
this.diagnostics = [];
62-
for (const node of template) {
63-
node.visit(this);
64-
}
65-
return this.diagnostics;
21+
export class InvalidBananaInBoxCheck extends
22+
TemplateCheckWithVisitor<ErrorCode.INVALID_BANANA_IN_BOX> {
23+
override code = ErrorCode.INVALID_BANANA_IN_BOX as const;
24+
25+
override visitNode(ctx: TemplateContext, component: ts.ClassDeclaration, node: TmplAstNode|AST):
26+
NgTemplateDiagnostic<ErrorCode.INVALID_BANANA_IN_BOX>[] {
27+
if (!(node instanceof TmplAstBoundEvent)) return [];
28+
29+
const name = node.name;
30+
if (!name.startsWith('[') || !name.endsWith(']')) return [];
31+
32+
const boundSyntax = node.sourceSpan.toString();
33+
const expectedBoundSyntax = boundSyntax.replace(`(${name})`, `[(${name.slice(1, -1)})]`);
34+
const diagnostic = ctx.templateTypeChecker.makeTemplateDiagnostic(
35+
component, node.sourceSpan, ts.DiagnosticCategory.Warning, ErrorCode.INVALID_BANANA_IN_BOX,
36+
`In the two-way binding syntax the parentheses should be inside the brackets, ex. '${
37+
expectedBoundSyntax}'.
38+
Find more at https://angular.io/guide/two-way-binding`);
39+
return [diagnostic];
6640
}
6741
}

‎packages/compiler-cli/src/ngtsc/typecheck/extended/src/extended_template_checker.ts‎

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -33,61 +33,9 @@ export class ExtendedTemplateCheckerImpl implements ExtendedTemplateChecker {
3333
const diagnostics: TemplateDiagnostic[] = [];
3434

3535
for (const check of this.templateChecks) {
36-
diagnostics.push(...deduplicateDiagnostics(check.run(this.ctx, component, template)));
36+
diagnostics.push(...check.run(this.ctx, component, template));
3737
}
3838

3939
return diagnostics;
4040
}
4141
}
42-
43-
44-
// Filter out duplicated diagnostics, this is possible due to the way the compiler
45-
// handles desugaring and produces `AST`s. Ex.
46-
//
47-
// ```
48-
// <div *ngIf="true" (foo)="bar">test</div>
49-
// ```
50-
//
51-
// Would result in the following AST:
52-
//
53-
// ```
54-
// Template {
55-
// outputs: [
56-
// BoundEvent {
57-
// name: 'foo',
58-
// /.../
59-
// }
60-
// ],
61-
// children: [
62-
// Element {
63-
// outputs: [
64-
// BoundEvent {
65-
// name: 'foo',
66-
// /.../
67-
// }
68-
// ]
69-
// }
70-
// ],
71-
// /.../
72-
// }
73-
// ```
74-
//
75-
// In this case a duplicated diagnostic could be generated for the output `foo`.
76-
// TODO(danieltrevino): handle duplicated diagnostics when they are being generated
77-
// to avoid extra work (could be directly in the visitor).
78-
// https://github.com/angular/angular/pull/42984#discussion_r684823926
79-
function deduplicateDiagnostics<T extends ts.Diagnostic>(diagnostics: T[]): T[] {
80-
const result: T[] = [];
81-
for (const newDiag of diagnostics) {
82-
const isDuplicateDiag = result.some(existingDiag => areDiagnosticsEqual(newDiag, existingDiag));
83-
if (!isDuplicateDiag) {
84-
result.push(newDiag);
85-
}
86-
}
87-
return result;
88-
}
89-
90-
function areDiagnosticsEqual(first: ts.Diagnostic, second: ts.Diagnostic): boolean {
91-
return first.file?.fileName === second.file?.fileName && first.start === second.start &&
92-
first.length === second.length && first.code === second.code;
93-
}

0 commit comments

Comments
 (0)