|
| 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 * as ts from 'typescript'; |
| 10 | + |
| 11 | +import {ErrorCode, 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 {NullishCoalescingNotNullableCheck} from '../../../checks/nullish_coalescing_not_nullable'; |
| 17 | +import {ExtendedTemplateCheckerImpl} from '../../../src/extended_template_checker'; |
| 18 | + |
| 19 | +runInEachFileSystem(() => { |
| 20 | + describe('NullishCoalescingNotNullableCheck', () => { |
| 21 | + it('should produce nullish coalescing warning', () => { |
| 22 | + const fileName = absoluteFrom('/main.ts'); |
| 23 | + const {program, templateTypeChecker} = setup([{ |
| 24 | + fileName, |
| 25 | + templates: { |
| 26 | + 'TestCmp': `{{ var1 ?? 'foo' }}`, |
| 27 | + }, |
| 28 | + source: 'export class TestCmp { var1: string = "text"; }' |
| 29 | + }]); |
| 30 | + const sf = getSourceFileOrError(program, fileName); |
| 31 | + const component = getClass(sf, 'TestCmp'); |
| 32 | + const extendedTemplateChecker = new ExtendedTemplateCheckerImpl( |
| 33 | + templateTypeChecker, program.getTypeChecker(), [new NullishCoalescingNotNullableCheck()]); |
| 34 | + const diags = extendedTemplateChecker.getDiagnosticsForComponent(component); |
| 35 | + expect(diags.length).toBe(1); |
| 36 | + expect(diags[0].category).toBe(ts.DiagnosticCategory.Warning); |
| 37 | + expect(diags[0].code).toBe(ngErrorCode(ErrorCode.NULLISH_COALESCING_NOT_NULLABLE)); |
| 38 | + expect(getSourceCodeForDiagnostic(diags[0])).toBe(`var1 ?? 'foo'`); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should not produce nullish coalescing warning for a nullable type', () => { |
| 42 | + const fileName = absoluteFrom('/main.ts'); |
| 43 | + const {program, templateTypeChecker} = setup([{ |
| 44 | + fileName, |
| 45 | + templates: { |
| 46 | + 'TestCmp': `{{ var1 ?? 'foo' }}`, |
| 47 | + }, |
| 48 | + source: 'export class TestCmp { var1: string | null = "text"; }' |
| 49 | + }]); |
| 50 | + const sf = getSourceFileOrError(program, fileName); |
| 51 | + const component = getClass(sf, 'TestCmp'); |
| 52 | + const extendedTemplateChecker = new ExtendedTemplateCheckerImpl( |
| 53 | + templateTypeChecker, program.getTypeChecker(), [new NullishCoalescingNotNullableCheck()]); |
| 54 | + const diags = extendedTemplateChecker.getDiagnosticsForComponent(component); |
| 55 | + expect(diags.length).toBe(0); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should not produce nullish coalescing warning for a type that includes undefined', () => { |
| 59 | + const fileName = absoluteFrom('/main.ts'); |
| 60 | + const {program, templateTypeChecker} = setup([{ |
| 61 | + fileName, |
| 62 | + templates: { |
| 63 | + 'TestCmp': `{{ var1 ?? 'foo' }}`, |
| 64 | + }, |
| 65 | + source: 'export class TestCmp { var1: string | undefined = "text"; }' |
| 66 | + }]); |
| 67 | + const sf = getSourceFileOrError(program, fileName); |
| 68 | + const component = getClass(sf, 'TestCmp'); |
| 69 | + const extendedTemplateChecker = new ExtendedTemplateCheckerImpl( |
| 70 | + templateTypeChecker, program.getTypeChecker(), [new NullishCoalescingNotNullableCheck()]); |
| 71 | + const diags = extendedTemplateChecker.getDiagnosticsForComponent(component); |
| 72 | + expect(diags.length).toBe(0); |
| 73 | + }); |
| 74 | + }); |
| 75 | +}); |
0 commit comments