Skip to content

Commit afecb87

Browse files
committed
Use related spans to form a tree of errors.
Formatting is wrong, and I might want to format it as non-related spans, but the structure is exactly a tree.
1 parent 1f6ef05 commit afecb87

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

src/compiler/checker.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11672,7 +11672,7 @@ namespace ts {
1167211672
}
1167311673

1167411674
function checkTypeRelatedToAndOptionallyElaborate(source: Type, target: Type, relation: Map<RelationComparisonResult>, errorNode: Node | undefined, expr: Expression | undefined, headMessage?: DiagnosticMessage, containingMessageChain?: () => DiagnosticMessageChain | undefined): boolean;
11675-
function checkTypeRelatedToAndOptionallyElaborate(source: Type, target: Type, relation: Map<RelationComparisonResult>, errorNode: Node | undefined, expr: Expression | undefined, headMessage?: DiagnosticMessage, containingMessageChain?: () => DiagnosticMessageChain | undefined, breakdown?: boolean): [Node, DiagnosticMessageChain];
11675+
function checkTypeRelatedToAndOptionallyElaborate(source: Type, target: Type, relation: Map<RelationComparisonResult>, errorNode: Node | undefined, expr: Expression | undefined, headMessage?: DiagnosticMessage, containingMessageChain?: () => DiagnosticMessageChain | undefined, breakdown?: boolean): [Node, DiagnosticMessageChain] | false;
1167611676
function checkTypeRelatedToAndOptionallyElaborate(source: Type, target: Type, relation: Map<RelationComparisonResult>, errorNode: Node | undefined, expr: Expression | undefined, headMessage?: DiagnosticMessage, containingMessageChain?: () => DiagnosticMessageChain | undefined, breakdown?: boolean): boolean | [Node, DiagnosticMessageChain] {
1167711677
if (isTypeRelatedTo(source, target, relation)) return breakdown ? false : true;
1167811678
if (!errorNode || !elaborateError(expr, source, target, relation, headMessage)) {
@@ -12361,7 +12361,7 @@ namespace ts {
1236112361
containingMessageChain?: () => DiagnosticMessageChain | undefined,
1236212362
errorOutputContainer?: { error?: Diagnostic },
1236312363
breakdown?: boolean
12364-
): [Node, DiagnosticMessageChain];
12364+
): false | [Node, DiagnosticMessageChain];
1236512365
/**
1236612366
* Checks if 'source' is related to 'target' (e.g.: is a assignable to).
1236712367
* @param source The left-hand-side of the relation.
@@ -21512,21 +21512,20 @@ namespace ts {
2151221512
if (candidatesForArgumentError) {
2151321513
if (candidatesForArgumentError.length > 3) {
2151421514
const c = candidatesForArgumentError[candidatesForArgumentError.length - 1];
21515-
const chain = chainDiagnosticMessages(undefined, Diagnostics.Failed_to_find_a_suitable_overload_for_this_call);
21515+
const chain = chainDiagnosticMessages(undefined, Diagnostics.Failed_to_find_a_suitable_overload_for_this_call_from_the_0_closest_overloads, candidatesForArgumentError.length);
21516+
2151621517
getSignatureApplicabilityError(node, args, c, assignableRelation, CheckMode.Normal, /*reportErrors*/ true, () => chain);
2151721518
}
2151821519
else {
21519-
// const related: DiagnosticRelatedInformation[] = [];
21520-
for (const c of candidatesForArgumentError) {
21521-
const chain = chainDiagnosticMessages(chainDiagnosticMessages(undefined, Diagnostics.Overload_0_gave_the_following_error, signatureToString(c)), Diagnostics.Failed_to_find_a_suitable_overload_for_this_call);
21520+
const related: DiagnosticRelatedInformation[] = [];
21521+
const close = candidatesForArgumentError.filter(c => getMinArgumentCount(c) <= args.length && args.length <= getParameterCount(c));
21522+
for (const c of close) {
21523+
const chain = chainDiagnosticMessages(undefined, Diagnostics.Overload_0_gave_the_following_error, signatureToString(c));
2152221524
const r = getSignatureApplicabilityError(node, args, c, assignableRelation, CheckMode.Normal, /*reportErrors*/ true, () => chain);
2152321525
if (!r || !r[0]) continue; // TODO:assert!
21524-
diagnostics.add(createDiagnosticForNodeFromMessageChain(r[0], r[1], /*relatedInformation*/ undefined));
21525-
// related.push(argNode)
21526-
// This is not right; I want them to be siblings
21527-
// probably this is a new feature :(
21528-
// chain = chainDiagnosticMessages(chain, msg);
21526+
related.push(createDiagnosticForNodeFromMessageChain(r[0], r[1]));
2152921527
}
21528+
diagnostics.add(createDiagnosticForNodeFromMessageChain(node, chainDiagnosticMessages(undefined, Diagnostics.Failed_to_find_a_suitable_overload_for_this_call_from_the_0_closest_overloads, close.length), related));
2153021529
}
2153121530
}
2153221531
else if (candidateForArgumentArityError) {
@@ -21604,9 +21603,7 @@ namespace ts {
2160421603
}
2160521604
if (getSignatureApplicabilityError(node, args, checkCandidate, relation, argCheckMode, /*reportErrors*/ false, /*containingMessageChain*/ undefined)) {
2160621605
// Give preference to error candidates that have no rest parameters (as they are more specific)
21607-
if (getMinArgumentCount(checkCandidate) <= args.length && args.length <= getParameterCount(checkCandidate)) {
21608-
(candidatesForArgumentError || (candidatesForArgumentError = [])).push(checkCandidate);
21609-
}
21606+
(candidatesForArgumentError || (candidatesForArgumentError = [])).push(checkCandidate);
2161021607
continue;
2161121608
}
2161221609
if (argCheckMode) {
@@ -21626,9 +21623,7 @@ namespace ts {
2162621623
}
2162721624
if (getSignatureApplicabilityError(node, args, checkCandidate, relation, argCheckMode, /*reportErrors*/ false, /*containingMessageChain*/ undefined)) {
2162821625
// Give preference to error candidates that have no rest parameters (as they are more specific)
21629-
if (getMinArgumentCount(checkCandidate) <= args.length && args.length <= getParameterCount(checkCandidate)) {
21630-
(candidatesForArgumentError || (candidatesForArgumentError = [])).push(checkCandidate);
21631-
}
21626+
(candidatesForArgumentError || (candidatesForArgumentError = [])).push(checkCandidate);
2163221627
continue;
2163321628
}
2163421629
}

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2621,7 +2621,7 @@
26212621
"category": "Error",
26222622
"code": 2754
26232623
},
2624-
"Failed to find a suitable overload for this call.": {
2624+
"Failed to find a suitable overload for this call from the {0} closest overloads.": {
26252625
"category": "Error",
26262626
"code": 2755
26272627
},

0 commit comments

Comments
 (0)