|
| 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, Binary, TmplAstNode} from '@angular/compiler'; |
| 10 | +import * as ts from 'typescript'; |
| 11 | + |
| 12 | +import {ErrorCode} from '../../../../diagnostics'; |
| 13 | +import {NgTemplateDiagnostic, SymbolKind} from '../../../api'; |
| 14 | +import {TemplateCheckWithVisitor, TemplateContext} from '../../api'; |
| 15 | + |
| 16 | +/** |
| 17 | + * Ensures the left side of a nullish coalescing operation is nullable. |
| 18 | + * Returns diagnostics for the cases where the operator is useless. |
| 19 | + * This check should only be use if `strictNullChecks` is enabled, |
| 20 | + * otherwise it would produce inaccurate results. |
| 21 | + */ |
| 22 | +export class NullishCoalescingNotNullableCheck extends |
| 23 | + TemplateCheckWithVisitor<ErrorCode.NULLISH_COALESCING_NOT_NULLABLE> { |
| 24 | + override code = ErrorCode.NULLISH_COALESCING_NOT_NULLABLE as const; |
| 25 | + |
| 26 | + override visitNode(ctx: TemplateContext, component: ts.ClassDeclaration, node: TmplAstNode|AST): |
| 27 | + NgTemplateDiagnostic<ErrorCode.NULLISH_COALESCING_NOT_NULLABLE>[] { |
| 28 | + if (!(node instanceof Binary) || node.operation !== '??') return []; |
| 29 | + |
| 30 | + const symbolLeft = ctx.templateTypeChecker.getSymbolOfNode(node.left, component)!; |
| 31 | + if (symbolLeft.kind !== SymbolKind.Expression) { |
| 32 | + return []; |
| 33 | + } |
| 34 | + const typeLeft = symbolLeft.tsType; |
| 35 | + // If the left operand's type is different from its non-nullable self, then it must |
| 36 | + // contain a null or undefined so this nullish coalescing operator is useful. No diagnostic to |
| 37 | + // report. |
| 38 | + if (typeLeft.getNonNullableType() !== typeLeft) return []; |
| 39 | + |
| 40 | + const symbol = ctx.templateTypeChecker.getSymbolOfNode(node, component)!; |
| 41 | + if (symbol.kind !== SymbolKind.Expression) { |
| 42 | + return []; |
| 43 | + } |
| 44 | + const span = |
| 45 | + ctx.templateTypeChecker.getTemplateMappingAtShimLocation(symbol.shimLocation)!.span; |
| 46 | + const diagnostic = ctx.templateTypeChecker.makeTemplateDiagnostic( |
| 47 | + component, span, ts.DiagnosticCategory.Warning, ErrorCode.NULLISH_COALESCING_NOT_NULLABLE, |
| 48 | + `The left side of this nullish coalescing operation does not include 'null' or 'undefined' in its type, therefore the '??' operator can be safely removed.`); |
| 49 | + return [diagnostic]; |
| 50 | + } |
| 51 | +} |
0 commit comments