-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compiler-cli): add extended diagnostic for non-nullable optional…
… chains (#46686) This commit adds an extended diagnostics check that is similar to the nullish coalescing check, but targeting optional chains. If the receiver expression of the optional chain is non-nullable, then the extended diagnostic can report an error or warning that can be fixed by changing the optional chain into a regular access. Closes #44870 PR Close #46686
- Loading branch information
1 parent
a6d5fe2
commit 93c65e7
Showing
11 changed files
with
417 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
.../compiler-cli/src/ngtsc/typecheck/extended/checks/optional_chain_not_nullable/BUILD.bazel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
load("//tools:defaults.bzl", "ts_library") | ||
|
||
ts_library( | ||
name = "optional_chain_not_nullable", | ||
srcs = ["index.ts"], | ||
visibility = ["//packages/compiler-cli/src/ngtsc:__subpackages__"], | ||
deps = [ | ||
"//packages/compiler", | ||
"//packages/compiler-cli/src/ngtsc/core:api", | ||
"//packages/compiler-cli/src/ngtsc/diagnostics", | ||
"//packages/compiler-cli/src/ngtsc/typecheck/api", | ||
"//packages/compiler-cli/src/ngtsc/typecheck/extended/api", | ||
"@npm//typescript", | ||
], | ||
) |
85 changes: 85 additions & 0 deletions
85
...ges/compiler-cli/src/ngtsc/typecheck/extended/checks/optional_chain_not_nullable/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {AST, SafeCall, SafeKeyedRead, SafePropertyRead, TmplAstNode} from '@angular/compiler'; | ||
import ts from 'typescript'; | ||
|
||
import {NgCompilerOptions} from '../../../../core/api'; | ||
import {ErrorCode, ExtendedTemplateDiagnosticName} from '../../../../diagnostics'; | ||
import {NgTemplateDiagnostic, SymbolKind} from '../../../api'; | ||
import {TemplateCheckFactory, TemplateCheckWithVisitor, TemplateContext} from '../../api'; | ||
|
||
/** | ||
* Ensures the left side of an optional chain operation is nullable. | ||
* Returns diagnostics for the cases where the operator is useless. | ||
* This check should only be use if `strictNullChecks` is enabled, | ||
* otherwise it would produce inaccurate results. | ||
*/ | ||
class OptionalChainNotNullableCheck extends | ||
TemplateCheckWithVisitor<ErrorCode.OPTIONAL_CHAIN_NOT_NULLABLE> { | ||
override code = ErrorCode.OPTIONAL_CHAIN_NOT_NULLABLE as const; | ||
|
||
override visitNode( | ||
ctx: TemplateContext<ErrorCode.OPTIONAL_CHAIN_NOT_NULLABLE>, component: ts.ClassDeclaration, | ||
node: TmplAstNode|AST): NgTemplateDiagnostic<ErrorCode.OPTIONAL_CHAIN_NOT_NULLABLE>[] { | ||
if (!(node instanceof SafeCall) && !(node instanceof SafePropertyRead) && | ||
!(node instanceof SafeKeyedRead)) | ||
return []; | ||
|
||
const symbolLeft = ctx.templateTypeChecker.getSymbolOfNode(node.receiver, component); | ||
if (symbolLeft === null || symbolLeft.kind !== SymbolKind.Expression) { | ||
return []; | ||
} | ||
const typeLeft = symbolLeft.tsType; | ||
if (typeLeft.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) { | ||
// We should not make assumptions about the any and unknown types; using a nullish coalescing | ||
// operator is acceptable for those. | ||
return []; | ||
} | ||
|
||
// If the left operand's type is different from its non-nullable self, then it must | ||
// contain a null or undefined so this nullish coalescing operator is useful. No diagnostic to | ||
// report. | ||
if (typeLeft.getNonNullableType() !== typeLeft) return []; | ||
|
||
const symbol = ctx.templateTypeChecker.getSymbolOfNode(node, component)!; | ||
if (symbol.kind !== SymbolKind.Expression) { | ||
return []; | ||
} | ||
const templateMapping = | ||
ctx.templateTypeChecker.getTemplateMappingAtTcbLocation(symbol.tcbLocation); | ||
if (templateMapping === null) { | ||
return []; | ||
} | ||
const advice = node instanceof SafePropertyRead ? | ||
`the '?.' operator can be replaced with the '.' operator` : | ||
`the '?.' operator can be safely removed`; | ||
const diagnostic = ctx.makeTemplateDiagnostic( | ||
templateMapping.span, | ||
`The left side of this optional chain operation does not include 'null' or 'undefined' in its type, therefore ${ | ||
advice}.`); | ||
return [diagnostic]; | ||
} | ||
} | ||
|
||
export const factory: TemplateCheckFactory< | ||
ErrorCode.OPTIONAL_CHAIN_NOT_NULLABLE, | ||
ExtendedTemplateDiagnosticName.OPTIONAL_CHAIN_NOT_NULLABLE> = { | ||
code: ErrorCode.OPTIONAL_CHAIN_NOT_NULLABLE, | ||
name: ExtendedTemplateDiagnosticName.OPTIONAL_CHAIN_NOT_NULLABLE, | ||
create: (options: NgCompilerOptions) => { | ||
// Require `strictNullChecks` to be enabled. | ||
const strictNullChecks = | ||
options.strictNullChecks === undefined ? !!options.strict : !!options.strictNullChecks; | ||
if (!strictNullChecks) { | ||
return null; | ||
} | ||
|
||
return new OptionalChainNotNullableCheck(); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...iler-cli/src/ngtsc/typecheck/extended/test/checks/optional_chain_not_nullable/BUILD.bazel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
load("//tools:defaults.bzl", "jasmine_node_test", "ts_library") | ||
|
||
ts_library( | ||
name = "test_lib", | ||
testonly = True, | ||
srcs = ["optional_chain_not_nullable_spec.ts"], | ||
deps = [ | ||
"//packages/compiler", | ||
"//packages/compiler-cli/src/ngtsc/core:api", | ||
"//packages/compiler-cli/src/ngtsc/diagnostics", | ||
"//packages/compiler-cli/src/ngtsc/file_system", | ||
"//packages/compiler-cli/src/ngtsc/file_system/testing", | ||
"//packages/compiler-cli/src/ngtsc/testing", | ||
"//packages/compiler-cli/src/ngtsc/typecheck/extended", | ||
"//packages/compiler-cli/src/ngtsc/typecheck/extended/checks/optional_chain_not_nullable", | ||
"//packages/compiler-cli/src/ngtsc/typecheck/testing", | ||
"@npm//typescript", | ||
], | ||
) | ||
|
||
jasmine_node_test( | ||
name = "test", | ||
bootstrap = ["//tools/testing:node_no_angular_es2015"], | ||
deps = [ | ||
":test_lib", | ||
], | ||
) |
Oops, something went wrong.