|
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import {TmplAstBoundEvent, TmplAstNode, TmplAstRecursiveVisitor} from '@angular/compiler'; |
| 9 | +import {AST, TmplAstBoundEvent, TmplAstNode} from '@angular/compiler'; |
10 | 10 | import * as ts from 'typescript'; |
11 | 11 |
|
12 | 12 | import {ErrorCode} from '../../../../diagnostics'; |
13 | 13 | import {NgTemplateDiagnostic} from '../../../api'; |
14 | | -import {TemplateCheck, TemplateContext} from '../../api'; |
| 14 | +import {TemplateCheckWithVisitor, TemplateContext} from '../../api'; |
15 | 15 |
|
16 | 16 | /** |
17 | 17 | * Ensures the two-way binding syntax is correct. |
18 | 18 | * Parentheses should be inside the brackets "[()]". |
19 | 19 | * Will return diagnostic information when "([])" is found. |
20 | 20 | */ |
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]; |
66 | 40 | } |
67 | 41 | } |
0 commit comments