Skip to content

Commit 8b35c13

Browse files
norechsandersn
andauthoredSep 15, 2022
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 738cf09. * 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>
1 parent a3f51b3 commit 8b35c13

File tree

71 files changed

+1793
-1622
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+1793
-1622
lines changed
 

‎src/compiler/checker.ts

+42-5
Original file line numberDiff line numberDiff line change
@@ -29172,11 +29172,30 @@ namespace ts {
2917229172
}
2917329173

2917429174
function reportObjectPossiblyNullOrUndefinedError(node: Node, facts: TypeFacts) {
29175-
error(node, facts & TypeFacts.IsUndefined ? facts & TypeFacts.IsNull ?
29176-
Diagnostics.Object_is_possibly_null_or_undefined :
29177-
Diagnostics.Object_is_possibly_undefined :
29178-
Diagnostics.Object_is_possibly_null
29179-
);
29175+
const nodeText = isEntityNameExpression(node) ? entityNameToString(node) : undefined;
29176+
if (node.kind === SyntaxKind.NullKeyword) {
29177+
error(node, Diagnostics.The_value_0_cannot_be_used_here, "null");
29178+
return;
29179+
}
29180+
if (nodeText !== undefined && nodeText.length < 100) {
29181+
if (isIdentifier(node) && nodeText === "undefined") {
29182+
error(node, Diagnostics.The_value_0_cannot_be_used_here, "undefined");
29183+
return;
29184+
}
29185+
error(node, facts & TypeFacts.IsUndefined ? facts & TypeFacts.IsNull ?
29186+
Diagnostics._0_is_possibly_null_or_undefined :
29187+
Diagnostics._0_is_possibly_undefined :
29188+
Diagnostics._0_is_possibly_null,
29189+
nodeText
29190+
);
29191+
}
29192+
else {
29193+
error(node, facts & TypeFacts.IsUndefined ? facts & TypeFacts.IsNull ?
29194+
Diagnostics.Object_is_possibly_null_or_undefined :
29195+
Diagnostics.Object_is_possibly_undefined :
29196+
Diagnostics.Object_is_possibly_null
29197+
);
29198+
}
2918029199
}
2918129200

2918229201
function reportCannotInvokePossiblyNullOrUndefinedError(node: Node, facts: TypeFacts) {
@@ -29193,6 +29212,13 @@ namespace ts {
2919329212
reportError: (node: Node, facts: TypeFacts) => void
2919429213
): Type {
2919529214
if (strictNullChecks && type.flags & TypeFlags.Unknown) {
29215+
if (isEntityNameExpression(node)) {
29216+
const nodeText = entityNameToString(node);
29217+
if (nodeText.length < 100) {
29218+
error(node, Diagnostics._0_is_of_type_unknown, nodeText);
29219+
return errorType;
29220+
}
29221+
}
2919629222
error(node, Diagnostics.Object_is_of_type_unknown);
2919729223
return errorType;
2919829224
}
@@ -29212,6 +29238,17 @@ namespace ts {
2921229238
function checkNonNullNonVoidType(type: Type, node: Node): Type {
2921329239
const nonNullType = checkNonNullType(type, node);
2921429240
if (nonNullType.flags & TypeFlags.Void) {
29241+
if (isEntityNameExpression(node)) {
29242+
const nodeText = entityNameToString(node);
29243+
if (isIdentifier(node) && nodeText === "undefined") {
29244+
error(node, Diagnostics.The_value_0_cannot_be_used_here, nodeText);
29245+
return nonNullType;
29246+
}
29247+
if (nodeText.length < 100) {
29248+
error(node, Diagnostics._0_is_possibly_undefined, nodeText);
29249+
return nonNullType;
29250+
}
29251+
}
2921529252
error(node, Diagnostics.Object_is_possibly_undefined);
2921629253
}
2921729254
return nonNullType;

‎src/compiler/diagnosticMessages.json

+20
Original file line numberDiff line numberDiff line change
@@ -7497,5 +7497,25 @@
74977497
"Properties with the 'accessor' modifier are only available when targeting ECMAScript 2015 and higher.": {
74987498
"category": "Error",
74997499
"code": 18045
7500+
},
7501+
"'{0}' is of type 'unknown'.": {
7502+
"category": "Error",
7503+
"code": 18046
7504+
},
7505+
"'{0}' is possibly 'null'.": {
7506+
"category": "Error",
7507+
"code": 18047
7508+
},
7509+
"'{0}' is possibly 'undefined'.": {
7510+
"category": "Error",
7511+
"code": 18048
7512+
},
7513+
"'{0}' is possibly 'null' or 'undefined'.": {
7514+
"category": "Error",
7515+
"code": 18049
7516+
},
7517+
"The value '{0}' cannot be used here.": {
7518+
"category": "Error",
7519+
"code": 18050
75007520
}
75017521
}

0 commit comments

Comments
 (0)