Skip to content

Commit eb6b87a

Browse files
committed
Refactor to use related info everywhere
1 parent 094a001 commit eb6b87a

File tree

4 files changed

+96
-85
lines changed

4 files changed

+96
-85
lines changed

src/compiler/checker.ts

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -933,15 +933,16 @@ namespace ts {
933933
}
934934

935935
function errorAndMaybeSuggestAwait(
936-
location: Node | undefined,
936+
location: Node,
937937
maybeMissingAwait: boolean,
938-
defaultMessage: DiagnosticMessage,
939-
missingAwaitMessage: DiagnosticMessage,
938+
message: DiagnosticMessage,
940939
arg0?: string | number | undefined, arg1?: string | number | undefined, arg2?: string | number | undefined, arg3?: string | number | undefined): Diagnostic {
940+
const diagnostic = error(location, message, arg0, arg1, arg2, arg3);
941941
if (maybeMissingAwait) {
942-
return error(location, missingAwaitMessage, arg0, arg1, arg2, arg3);
942+
const related = createDiagnosticForNode(location, Diagnostics.Did_you_forget_to_use_await);
943+
addRelatedInfo(diagnostic, related);
943944
}
944-
return error(location, defaultMessage, arg0, arg1, arg2, arg3);
945+
return diagnostic;
945946
}
946947

947948
function createSymbol(flags: SymbolFlags, name: __String, checkFlags?: CheckFlags) {
@@ -22350,11 +22351,11 @@ namespace ts {
2235022351
return true;
2235122352
}
2235222353

22353-
function invocationErrorDetails(apparentType: Type, kind: SignatureKind): DiagnosticMessageChain {
22354+
function invocationErrorDetails(apparentType: Type, kind: SignatureKind): { messageChain: DiagnosticMessageChain, relatedMessage: DiagnosticMessage | undefined } {
2235422355
let errorInfo: DiagnosticMessageChain | undefined;
2235522356
const isCall = kind === SignatureKind.Call;
2235622357
const awaitedType = getAwaitedType(apparentType);
22357-
const mightWorkWithAwait = awaitedType && getSignaturesOfType(awaitedType, kind).length > 0;
22358+
const maybeMissingAwait = awaitedType && getSignaturesOfType(awaitedType, kind).length > 0;
2235822359
if (apparentType.flags & TypeFlags.Union) {
2235922360
const types = (apparentType as UnionType).types;
2236022361
let hasSignatures = false;
@@ -22419,15 +22420,20 @@ namespace ts {
2241922420
typeToString(apparentType)
2242022421
);
2242122422
}
22422-
return chainDiagnosticMessages(
22423-
errorInfo,
22424-
mightWorkWithAwait
22425-
? isCall ? Diagnostics.This_expression_is_not_callable_Did_you_forget_to_use_await : Diagnostics.This_expression_is_not_constructable_Did_you_forget_to_use_await
22426-
: isCall ? Diagnostics.This_expression_is_not_callable : Diagnostics.This_expression_is_not_constructable
22427-
);
22423+
return {
22424+
messageChain: chainDiagnosticMessages(
22425+
errorInfo,
22426+
isCall ? Diagnostics.This_expression_is_not_callable : Diagnostics.This_expression_is_not_constructable
22427+
),
22428+
relatedMessage: maybeMissingAwait ? Diagnostics.Did_you_forget_to_use_await : undefined,
22429+
};
2242822430
}
2242922431
function invocationError(errorTarget: Node, apparentType: Type, kind: SignatureKind, relatedInformation?: DiagnosticRelatedInformation) {
22430-
const diagnostic = createDiagnosticForNodeFromMessageChain(errorTarget, invocationErrorDetails(apparentType, kind));
22432+
const { messageChain, relatedMessage: relatedInfo } = invocationErrorDetails(apparentType, kind);
22433+
const diagnostic = createDiagnosticForNodeFromMessageChain(errorTarget, messageChain);
22434+
if (relatedInfo) {
22435+
addRelatedInfo(diagnostic, createDiagnosticForNode(errorTarget, relatedInfo));
22436+
}
2243122437
if (isCallExpression(errorTarget.parent)) {
2243222438
const { start, length } = getDiagnosticSpanForCallNode(errorTarget.parent, /* doNotIncludeArguments */ true);
2243322439
diagnostic.start = start;
@@ -22527,9 +22533,12 @@ namespace ts {
2252722533

2252822534
const headMessage = getDiagnosticHeadMessageForDecoratorResolution(node);
2252922535
if (!callSignatures.length) {
22530-
let errorInfo = invocationErrorDetails(apparentType, SignatureKind.Call);
22531-
errorInfo = chainDiagnosticMessages(errorInfo, headMessage);
22532-
const diag = createDiagnosticForNodeFromMessageChain(node.expression, errorInfo);
22536+
const errorDetails = invocationErrorDetails(apparentType, SignatureKind.Call);
22537+
const messageChain = chainDiagnosticMessages(errorDetails.messageChain, headMessage);
22538+
const diag = createDiagnosticForNodeFromMessageChain(node.expression, messageChain);
22539+
if (errorDetails.relatedMessage) {
22540+
addRelatedInfo(diag, createDiagnosticForNode(node.expression, errorDetails.relatedMessage));
22541+
}
2253322542
diagnostics.add(diag);
2253422543
invocationErrorRecovery(apparentType, SignatureKind.Call, diag);
2253522544
return resolveErrorCall(node);
@@ -23687,14 +23696,13 @@ namespace ts {
2368723696
}
2368823697
}
2368923698

23690-
function checkArithmeticOperandType(operand: Node, type: Type, diagnostic: DiagnosticMessage, missingAwaitDiagnostic: DiagnosticMessage): boolean {
23699+
function checkArithmeticOperandType(operand: Node, type: Type, diagnostic: DiagnosticMessage): boolean {
2369123700
if (!isTypeAssignableTo(type, numberOrBigIntType)) {
2369223701
const awaitedType = getAwaitedType(type);
2369323702
errorAndMaybeSuggestAwait(
2369423703
operand,
2369523704
!!awaitedType && isTypeAssignableTo(awaitedType, numberOrBigIntType),
23696-
diagnostic,
23697-
missingAwaitDiagnostic);
23705+
diagnostic);
2369823706
return false;
2369923707
}
2370023708
return true;
@@ -23892,8 +23900,7 @@ namespace ts {
2389223900
case SyntaxKind.PlusPlusToken:
2389323901
case SyntaxKind.MinusMinusToken:
2389423902
const ok = checkArithmeticOperandType(node.operand, checkNonNullType(operandType, node.operand),
23895-
Diagnostics.An_arithmetic_operand_must_be_of_type_any_number_bigint_or_an_enum_type,
23896-
Diagnostics.An_arithmetic_operand_must_be_of_type_any_number_bigint_or_an_enum_type_Did_you_forget_to_use_await);
23903+
Diagnostics.An_arithmetic_operand_must_be_of_type_any_number_bigint_or_an_enum_type);
2389723904
if (ok) {
2389823905
// run check only if former checks succeeded to avoid reporting cascading errors
2389923906
checkReferenceExpression(node.operand, Diagnostics.The_operand_of_an_increment_or_decrement_operator_must_be_a_variable_or_a_property_access);
@@ -23911,8 +23918,7 @@ namespace ts {
2391123918
const ok = checkArithmeticOperandType(
2391223919
node.operand,
2391323920
checkNonNullType(operandType, node.operand),
23914-
Diagnostics.An_arithmetic_operand_must_be_of_type_any_number_bigint_or_an_enum_type,
23915-
Diagnostics.An_arithmetic_operand_must_be_of_type_any_number_bigint_or_an_enum_type_Did_you_forget_to_use_await);
23921+
Diagnostics.An_arithmetic_operand_must_be_of_type_any_number_bigint_or_an_enum_type);
2391623922
if (ok) {
2391723923
// run check only if former checks succeeded to avoid reporting cascading errors
2391823924
checkReferenceExpression(node.operand, Diagnostics.The_operand_of_an_increment_or_decrement_operator_must_be_a_variable_or_a_property_access);
@@ -24304,8 +24310,8 @@ namespace ts {
2430424310
}
2430524311
else {
2430624312
// otherwise just check each operand separately and report errors as normal
24307-
const leftOk = checkArithmeticOperandType(left, leftType, Diagnostics.The_left_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_bigint_or_an_enum_type, Diagnostics.The_left_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_bigint_or_an_enum_type_Did_you_forget_to_use_await);
24308-
const rightOk = checkArithmeticOperandType(right, rightType, Diagnostics.The_right_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_bigint_or_an_enum_type, Diagnostics.The_right_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_bigint_or_an_enum_type_Did_you_forget_to_use_await);
24313+
const leftOk = checkArithmeticOperandType(left, leftType, Diagnostics.The_left_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_bigint_or_an_enum_type);
24314+
const rightOk = checkArithmeticOperandType(right, rightType, Diagnostics.The_right_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_bigint_or_an_enum_type);
2430924315
let resultType: Type;
2431024316
// If both are any or unknown, allow operation; assume it will resolve to number
2431124317
if ((isTypeAssignableToKind(leftType, TypeFlags.AnyOrUnknown) && isTypeAssignableToKind(rightType, TypeFlags.AnyOrUnknown)) ||
@@ -24558,7 +24564,6 @@ namespace ts {
2455824564
errNode,
2455924565
wouldWorkWithAwait,
2456024566
Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2,
24561-
Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2_Did_you_forget_to_use_await,
2456224567
tokenToString(operatorToken.kind),
2456324568
leftStr,
2456424569
rightStr,
@@ -24583,7 +24588,6 @@ namespace ts {
2458324588
errNode,
2458424589
maybeMissingAwait,
2458524590
Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap,
24586-
Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap_Did_you_forget_to_use_await,
2458724591
typeName, leftStr, rightStr);
2458824592
}
2458924593

@@ -27937,28 +27941,22 @@ namespace ts {
2793727941
// number and string input is allowed, we want to say that number is not an
2793827942
// array type or a string type.
2793927943
const yieldType = getIterationTypeOfIterable(use, IterationTypeKind.Yield, inputType, /*errorNode*/ undefined);
27940-
const [defaultDiagnostic, missingAwaitDiagnostic]: [DiagnosticMessage, DiagnosticMessage | undefined] = !(use & IterationUse.AllowsStringInputFlag) || hasStringConstituent
27944+
const [defaultDiagnostic, maybeMissingAwait]: [DiagnosticMessage, boolean] = !(use & IterationUse.AllowsStringInputFlag) || hasStringConstituent
2794127945
? downlevelIteration
27942-
? [Diagnostics.Type_0_is_not_an_array_type_or_does_not_have_a_Symbol_iterator_method_that_returns_an_iterator, Diagnostics.Type_0_is_not_an_array_type_or_does_not_have_a_Symbol_iterator_method_that_returns_an_iterator_Did_you_forget_to_use_await]
27946+
? [Diagnostics.Type_0_is_not_an_array_type_or_does_not_have_a_Symbol_iterator_method_that_returns_an_iterator, true]
2794327947
: yieldType
27944-
? [Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_Use_compiler_option_downlevelIteration_to_allow_iterating_of_iterators, undefined]
27945-
: [Diagnostics.Type_0_is_not_an_array_type, Diagnostics.Type_0_is_not_an_array_type_Did_you_forget_to_use_await]
27948+
? [Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_Use_compiler_option_downlevelIteration_to_allow_iterating_of_iterators, false]
27949+
: [Diagnostics.Type_0_is_not_an_array_type, true]
2794627950
: downlevelIteration
27947-
? [Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_or_does_not_have_a_Symbol_iterator_method_that_returns_an_iterator, Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_or_does_not_have_a_Symbol_iterator_method_that_returns_an_iterator_Did_you_forget_to_use_await]
27951+
? [Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_or_does_not_have_a_Symbol_iterator_method_that_returns_an_iterator, true]
2794827952
: yieldType
27949-
? [Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_Use_compiler_option_downlevelIteration_to_allow_iterating_of_iterators, undefined]
27950-
: [Diagnostics.Type_0_is_not_an_array_type_or_a_string_type, Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_Did_you_forget_to_use_await];
27951-
if (missingAwaitDiagnostic) {
27952-
errorAndMaybeSuggestAwait(
27953-
errorNode,
27954-
!!getAwaitedTypeOfPromise(arrayType),
27955-
defaultDiagnostic,
27956-
missingAwaitDiagnostic,
27957-
typeToString(arrayType));
27958-
}
27959-
else {
27960-
error(errorNode, defaultDiagnostic, typeToString(arrayType));
27961-
}
27953+
? [Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_Use_compiler_option_downlevelIteration_to_allow_iterating_of_iterators, false]
27954+
: [Diagnostics.Type_0_is_not_an_array_type_or_a_string_type, true];
27955+
errorAndMaybeSuggestAwait(
27956+
errorNode,
27957+
maybeMissingAwait && !!getAwaitedTypeOfPromise(arrayType),
27958+
defaultDiagnostic,
27959+
typeToString(arrayType));
2796227960
}
2796327961
return hasStringConstituent ? stringType : undefined;
2796427962
}
@@ -28257,10 +28255,10 @@ namespace ts {
2825728255
}
2825828256

2825928257
function reportTypeNotIterableError(errorNode: Node, type: Type, allowAsyncIterables: boolean): void {
28260-
const [defaultDiagnostic, missingAwaitDiagnostic] = allowAsyncIterables
28261-
? [Diagnostics.Type_0_must_have_a_Symbol_asyncIterator_method_that_returns_an_async_iterator, Diagnostics.Type_0_must_have_a_Symbol_asyncIterator_method_that_returns_an_async_iterator_Did_you_forget_to_use_await]
28262-
: [Diagnostics.Type_0_must_have_a_Symbol_iterator_method_that_returns_an_iterator, Diagnostics.Type_0_must_have_a_Symbol_iterator_method_that_returns_an_iterator_Did_you_forget_to_use_await];
28263-
errorAndMaybeSuggestAwait(errorNode, !!getAwaitedTypeOfPromise(type), defaultDiagnostic, missingAwaitDiagnostic, typeToString(type));
28258+
const message = allowAsyncIterables
28259+
? Diagnostics.Type_0_must_have_a_Symbol_asyncIterator_method_that_returns_an_async_iterator
28260+
: Diagnostics.Type_0_must_have_a_Symbol_iterator_method_that_returns_an_iterator;
28261+
errorAndMaybeSuggestAwait(errorNode, !!getAwaitedTypeOfPromise(type), message, typeToString(type));
2826428262
}
2826528263

2826628264
/**

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,8 +2655,7 @@
26552655
},
26562656
"Cannot iterate value because the 'next' method of its iterator expects type '{1}', but for-of will always send '{0}'.": {
26572657
"category": "Error",
2658-
"code": 2763
2659-
},
2658+
"code": 77 },
26602659
"Cannot iterate value because the 'next' method of its iterator expects type '{1}', but array spread will always send '{0}'.": {
26612660
"category": "Error",
26622661
"code": 2764

0 commit comments

Comments
 (0)