|
| 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, SafeCall, SafeKeyedRead, SafePropertyRead, TmplAstNode} from '@angular/compiler'; |
| 10 | +import ts from 'typescript'; |
| 11 | + |
| 12 | +import {NgCompilerOptions} from '../../../../core/api'; |
| 13 | +import {ErrorCode, ExtendedTemplateDiagnosticName} from '../../../../diagnostics'; |
| 14 | +import {NgTemplateDiagnostic, SymbolKind} from '../../../api'; |
| 15 | +import {TemplateCheckFactory, TemplateCheckWithVisitor, TemplateContext} from '../../api'; |
| 16 | + |
| 17 | +/** |
| 18 | + * Ensures the left side of an optional chain operation is nullable. |
| 19 | + * Returns diagnostics for the cases where the operator is useless. |
| 20 | + * This check should only be use if `strictNullChecks` is enabled, |
| 21 | + * otherwise it would produce inaccurate results. |
| 22 | + */ |
| 23 | +class OptionalChainNotNullableCheck extends |
| 24 | + TemplateCheckWithVisitor<ErrorCode.OPTIONAL_CHAIN_NOT_NULLABLE> { |
| 25 | + override code = ErrorCode.OPTIONAL_CHAIN_NOT_NULLABLE as const; |
| 26 | + |
| 27 | + override visitNode( |
| 28 | + ctx: TemplateContext<ErrorCode.OPTIONAL_CHAIN_NOT_NULLABLE>, component: ts.ClassDeclaration, |
| 29 | + node: TmplAstNode|AST): NgTemplateDiagnostic<ErrorCode.OPTIONAL_CHAIN_NOT_NULLABLE>[] { |
| 30 | + if (!(node instanceof SafeCall) && !(node instanceof SafePropertyRead) && |
| 31 | + !(node instanceof SafeKeyedRead)) |
| 32 | + return []; |
| 33 | + |
| 34 | + const symbolLeft = ctx.templateTypeChecker.getSymbolOfNode(node.receiver, component); |
| 35 | + if (symbolLeft === null || symbolLeft.kind !== SymbolKind.Expression) { |
| 36 | + return []; |
| 37 | + } |
| 38 | + const typeLeft = symbolLeft.tsType; |
| 39 | + if (typeLeft.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) { |
| 40 | + // We should not make assumptions about the any and unknown types; using a nullish coalescing |
| 41 | + // operator is acceptable for those. |
| 42 | + return []; |
| 43 | + } |
| 44 | + |
| 45 | + // If the left operand's type is different from its non-nullable self, then it must |
| 46 | + // contain a null or undefined so this nullish coalescing operator is useful. No diagnostic to |
| 47 | + // report. |
| 48 | + if (typeLeft.getNonNullableType() !== typeLeft) return []; |
| 49 | + |
| 50 | + const symbol = ctx.templateTypeChecker.getSymbolOfNode(node, component)!; |
| 51 | + if (symbol.kind !== SymbolKind.Expression) { |
| 52 | + return []; |
| 53 | + } |
| 54 | + const templateMapping = |
| 55 | + ctx.templateTypeChecker.getTemplateMappingAtTcbLocation(symbol.tcbLocation); |
| 56 | + if (templateMapping === null) { |
| 57 | + return []; |
| 58 | + } |
| 59 | + const advice = node instanceof SafePropertyRead ? |
| 60 | + `the '?.' operator can be replaced with the '.' operator` : |
| 61 | + `the '?.' operator can be safely removed`; |
| 62 | + const diagnostic = ctx.makeTemplateDiagnostic( |
| 63 | + templateMapping.span, |
| 64 | + `The left side of this optional chain operation does not include 'null' or 'undefined' in its type, therefore ${ |
| 65 | + advice}.`); |
| 66 | + return [diagnostic]; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +export const factory: TemplateCheckFactory< |
| 71 | + ErrorCode.OPTIONAL_CHAIN_NOT_NULLABLE, |
| 72 | + ExtendedTemplateDiagnosticName.OPTIONAL_CHAIN_NOT_NULLABLE> = { |
| 73 | + code: ErrorCode.OPTIONAL_CHAIN_NOT_NULLABLE, |
| 74 | + name: ExtendedTemplateDiagnosticName.OPTIONAL_CHAIN_NOT_NULLABLE, |
| 75 | + create: (options: NgCompilerOptions) => { |
| 76 | + // Require `strictNullChecks` to be enabled. |
| 77 | + const strictNullChecks = |
| 78 | + options.strictNullChecks === undefined ? !!options.strict : !!options.strictNullChecks; |
| 79 | + if (!strictNullChecks) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + return new OptionalChainNotNullableCheck(); |
| 84 | + }, |
| 85 | +}; |
0 commit comments