The error "Object is possibly null or undefined" is ambiguous. (#49797)

* added object name to TS2571, 2531, 2532 and 2533

* updated localized diagnostic messages

* updated baseline to fit diagnostic message change

* Revert "updated localized diagnostic messages"

This reverts commit 738cf094bd.

* specialized the error to EntityNameExpression

* updated baseline to fit new changes

* added multiline undefined access test

* added TS18049 - value cannot be used here

* adjusted baseline

* corrected a small linting issue

* Update error numbers after merge from main

Co-authored-by: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com>
This commit is contained in:
Alexis Cheron
2022-09-16 03:13:53 +04:00
committed by GitHub
parent a3f51b3b82
commit 8b35c1300e
71 changed files with 1793 additions and 1622 deletions

View File

@@ -29172,11 +29172,30 @@ namespace ts {
}
function reportObjectPossiblyNullOrUndefinedError(node: Node, facts: TypeFacts) {
error(node, facts & TypeFacts.IsUndefined ? facts & TypeFacts.IsNull ?
Diagnostics.Object_is_possibly_null_or_undefined :
Diagnostics.Object_is_possibly_undefined :
Diagnostics.Object_is_possibly_null
);
const nodeText = isEntityNameExpression(node) ? entityNameToString(node) : undefined;
if (node.kind === SyntaxKind.NullKeyword) {
error(node, Diagnostics.The_value_0_cannot_be_used_here, "null");
return;
}
if (nodeText !== undefined && nodeText.length < 100) {
if (isIdentifier(node) && nodeText === "undefined") {
error(node, Diagnostics.The_value_0_cannot_be_used_here, "undefined");
return;
}
error(node, facts & TypeFacts.IsUndefined ? facts & TypeFacts.IsNull ?
Diagnostics._0_is_possibly_null_or_undefined :
Diagnostics._0_is_possibly_undefined :
Diagnostics._0_is_possibly_null,
nodeText
);
}
else {
error(node, facts & TypeFacts.IsUndefined ? facts & TypeFacts.IsNull ?
Diagnostics.Object_is_possibly_null_or_undefined :
Diagnostics.Object_is_possibly_undefined :
Diagnostics.Object_is_possibly_null
);
}
}
function reportCannotInvokePossiblyNullOrUndefinedError(node: Node, facts: TypeFacts) {
@@ -29193,6 +29212,13 @@ namespace ts {
reportError: (node: Node, facts: TypeFacts) => void
): Type {
if (strictNullChecks && type.flags & TypeFlags.Unknown) {
if (isEntityNameExpression(node)) {
const nodeText = entityNameToString(node);
if (nodeText.length < 100) {
error(node, Diagnostics._0_is_of_type_unknown, nodeText);
return errorType;
}
}
error(node, Diagnostics.Object_is_of_type_unknown);
return errorType;
}
@@ -29212,6 +29238,17 @@ namespace ts {
function checkNonNullNonVoidType(type: Type, node: Node): Type {
const nonNullType = checkNonNullType(type, node);
if (nonNullType.flags & TypeFlags.Void) {
if (isEntityNameExpression(node)) {
const nodeText = entityNameToString(node);
if (isIdentifier(node) && nodeText === "undefined") {
error(node, Diagnostics.The_value_0_cannot_be_used_here, nodeText);
return nonNullType;
}
if (nodeText.length < 100) {
error(node, Diagnostics._0_is_possibly_undefined, nodeText);
return nonNullType;
}
}
error(node, Diagnostics.Object_is_possibly_undefined);
}
return nonNullType;

View File

@@ -7497,5 +7497,25 @@
"Properties with the 'accessor' modifier are only available when targeting ECMAScript 2015 and higher.": {
"category": "Error",
"code": 18045
},
"'{0}' is of type 'unknown'.": {
"category": "Error",
"code": 18046
},
"'{0}' is possibly 'null'.": {
"category": "Error",
"code": 18047
},
"'{0}' is possibly 'undefined'.": {
"category": "Error",
"code": 18048
},
"'{0}' is possibly 'null' or 'undefined'.": {
"category": "Error",
"code": 18049
},
"The value '{0}' cannot be used here.": {
"category": "Error",
"code": 18050
}
}