|
| 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 ts from 'typescript'; |
| 10 | + |
| 11 | +import {ErrorCode, ExtendedTemplateDiagnosticName, ngErrorCode} from '../../../../../diagnostics'; |
| 12 | +import {absoluteFrom, getSourceFileOrError} from '../../../../../file_system'; |
| 13 | +import {runInEachFileSystem} from '../../../../../file_system/testing'; |
| 14 | +import {getSourceCodeForDiagnostic} from '../../../../../testing'; |
| 15 | +import {getClass, setup} from '../../../../testing'; |
| 16 | +import {factory as unusedLetDeclarationFactory} from '../../../checks/unused_let_declaration/index'; |
| 17 | +import {ExtendedTemplateCheckerImpl} from '../../../src/extended_template_checker'; |
| 18 | + |
| 19 | +runInEachFileSystem(() => { |
| 20 | + describe('UnusedLetDeclarationCheck', () => { |
| 21 | + function diagnose(template: string) { |
| 22 | + const fileName = absoluteFrom('/main.ts'); |
| 23 | + const {program, templateTypeChecker} = setup([ |
| 24 | + { |
| 25 | + fileName, |
| 26 | + templates: { |
| 27 | + 'TestCmp': template, |
| 28 | + }, |
| 29 | + source: ` |
| 30 | + export class TestCmp { |
| 31 | + eventCallback(value: any) {} |
| 32 | + } |
| 33 | + `, |
| 34 | + }, |
| 35 | + ]); |
| 36 | + const sf = getSourceFileOrError(program, fileName); |
| 37 | + const component = getClass(sf, 'TestCmp'); |
| 38 | + const extendedTemplateChecker = new ExtendedTemplateCheckerImpl( |
| 39 | + templateTypeChecker, |
| 40 | + program.getTypeChecker(), |
| 41 | + [unusedLetDeclarationFactory], |
| 42 | + {}, |
| 43 | + ); |
| 44 | + return extendedTemplateChecker.getDiagnosticsForComponent(component); |
| 45 | + } |
| 46 | + |
| 47 | + it('binds the error code to its extended template diagnostic name', () => { |
| 48 | + expect(unusedLetDeclarationFactory.code).toBe(ErrorCode.UNUSED_LET_DECLARATION); |
| 49 | + expect(unusedLetDeclarationFactory.name).toBe( |
| 50 | + ExtendedTemplateDiagnosticName.UNUSED_LET_DECLARATION, |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should report a @let declaration that is not used', () => { |
| 55 | + const diags = diagnose(` |
| 56 | + @let used = 1; |
| 57 | + @let unused = 2; |
| 58 | + {{used}} |
| 59 | + `); |
| 60 | + |
| 61 | + expect(diags.length).toBe(1); |
| 62 | + expect(diags[0].category).toBe(ts.DiagnosticCategory.Warning); |
| 63 | + expect(diags[0].code).toBe(ngErrorCode(ErrorCode.UNUSED_LET_DECLARATION)); |
| 64 | + expect(getSourceCodeForDiagnostic(diags[0])).toBe('@let unused = 2'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should report a @let declaration that is not used', () => { |
| 68 | + const diags = diagnose(` |
| 69 | + @let foo = 1; |
| 70 | +
|
| 71 | + @if (true) { |
| 72 | + @let foo = 2; |
| 73 | + {{foo}} |
| 74 | + } |
| 75 | + `); |
| 76 | + |
| 77 | + expect(diags.length).toBe(1); |
| 78 | + expect(diags[0].category).toBe(ts.DiagnosticCategory.Warning); |
| 79 | + expect(diags[0].code).toBe(ngErrorCode(ErrorCode.UNUSED_LET_DECLARATION)); |
| 80 | + expect(getSourceCodeForDiagnostic(diags[0])).toBe('@let foo = 1'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should not report a @let declaration that is only used in other @let declarations', () => { |
| 84 | + const diags = diagnose(` |
| 85 | + @let one = 1; |
| 86 | + @let two = 2; |
| 87 | + @let three = one + two; |
| 88 | + {{three}} |
| 89 | + `); |
| 90 | + |
| 91 | + expect(diags.length).toBe(0); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should not report a @let declaration that is only used in an event listener', () => { |
| 95 | + const diags = diagnose(` |
| 96 | + @let foo = 1; |
| 97 | + <button (click)="eventCallback(foo + 1)">Click me</button> |
| 98 | + `); |
| 99 | + |
| 100 | + expect(diags.length).toBe(0); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should not report a @let declaration that is only used in a structural directive', () => { |
| 104 | + const diags = diagnose(` |
| 105 | + @let foo = null; |
| 106 | + <div *ngIf="foo"></div> |
| 107 | + `); |
| 108 | + |
| 109 | + expect(diags.length).toBe(0); |
| 110 | + }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments