|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {AST, ASTWithSource, TmplAstLetDeclaration, TmplAstNode} from '@angular/compiler'; |
| 10 | +import ts from 'typescript'; |
| 11 | + |
| 12 | +import {ErrorCode, ExtendedTemplateDiagnosticName} from '../../../../diagnostics'; |
| 13 | +import {NgTemplateDiagnostic} from '../../../api'; |
| 14 | +import {TemplateCheckFactory, TemplateCheckWithVisitor, TemplateContext} from '../../api'; |
| 15 | + |
| 16 | +interface ClassAnalysis { |
| 17 | + allLetDeclarations: Set<TmplAstLetDeclaration>; |
| 18 | + usedLetDeclarations: Set<TmplAstLetDeclaration>; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Ensures that all `@let` declarations in a template are used. |
| 23 | + */ |
| 24 | +class UnusedLetDeclarationCheck extends TemplateCheckWithVisitor<ErrorCode.UNUSED_LET_DECLARATION> { |
| 25 | + override code = ErrorCode.UNUSED_LET_DECLARATION as const; |
| 26 | + private analysis = new Map<ts.ClassDeclaration, ClassAnalysis>(); |
| 27 | + |
| 28 | + override run( |
| 29 | + ctx: TemplateContext<ErrorCode.UNUSED_LET_DECLARATION>, |
| 30 | + component: ts.ClassDeclaration, |
| 31 | + template: TmplAstNode[], |
| 32 | + ): NgTemplateDiagnostic<ErrorCode.UNUSED_LET_DECLARATION>[] { |
| 33 | + super.run(ctx, component, template); |
| 34 | + |
| 35 | + const diagnostics: NgTemplateDiagnostic<ErrorCode.UNUSED_LET_DECLARATION>[] = []; |
| 36 | + const {allLetDeclarations, usedLetDeclarations} = this.getAnalysis(component); |
| 37 | + |
| 38 | + for (const decl of allLetDeclarations) { |
| 39 | + if (!usedLetDeclarations.has(decl)) { |
| 40 | + diagnostics.push( |
| 41 | + ctx.makeTemplateDiagnostic( |
| 42 | + decl.sourceSpan, |
| 43 | + `@let ${decl.name} is declared but its value is never read.`, |
| 44 | + ), |
| 45 | + ); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + this.analysis.clear(); |
| 50 | + return diagnostics; |
| 51 | + } |
| 52 | + |
| 53 | + override visitNode( |
| 54 | + ctx: TemplateContext<ErrorCode.UNUSED_LET_DECLARATION>, |
| 55 | + component: ts.ClassDeclaration, |
| 56 | + node: TmplAstNode | AST, |
| 57 | + ): NgTemplateDiagnostic<ErrorCode.UNUSED_LET_DECLARATION>[] { |
| 58 | + if (node instanceof TmplAstLetDeclaration) { |
| 59 | + this.getAnalysis(component).allLetDeclarations.add(node); |
| 60 | + } else if (node instanceof AST) { |
| 61 | + const unwrappedNode = node instanceof ASTWithSource ? node.ast : node; |
| 62 | + const target = ctx.templateTypeChecker.getExpressionTarget(unwrappedNode, component); |
| 63 | + |
| 64 | + if (target !== null && target instanceof TmplAstLetDeclaration) { |
| 65 | + this.getAnalysis(component).usedLetDeclarations.add(target); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return []; |
| 70 | + } |
| 71 | + |
| 72 | + private getAnalysis(node: ts.ClassDeclaration): ClassAnalysis { |
| 73 | + if (!this.analysis.has(node)) { |
| 74 | + this.analysis.set(node, {allLetDeclarations: new Set(), usedLetDeclarations: new Set()}); |
| 75 | + } |
| 76 | + return this.analysis.get(node)!; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +export const factory: TemplateCheckFactory< |
| 81 | + ErrorCode.UNUSED_LET_DECLARATION, |
| 82 | + ExtendedTemplateDiagnosticName.UNUSED_LET_DECLARATION |
| 83 | +> = { |
| 84 | + code: ErrorCode.UNUSED_LET_DECLARATION, |
| 85 | + name: ExtendedTemplateDiagnosticName.UNUSED_LET_DECLARATION, |
| 86 | + create: () => new UnusedLetDeclarationCheck(), |
| 87 | +}; |
0 commit comments