Skip to content

Commit e9c6d96

Browse files
authored
Add related span pointing at missing arguments (#27013)
1 parent 78c8003 commit e9c6d96

File tree

59 files changed

+235
-15
lines changed

Some content is hidden

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

59 files changed

+235
-15
lines changed

src/compiler/checker.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19208,12 +19208,16 @@ namespace ts {
1920819208
let aboveArgCount = Number.POSITIVE_INFINITY;
1920919209

1921019210
let argCount = args.length;
19211+
let closestSignature: Signature | undefined;
1921119212
for (const sig of signatures) {
1921219213
const minCount = getMinArgumentCount(sig);
1921319214
const maxCount = getParameterCount(sig);
1921419215
if (minCount < argCount && minCount > belowArgCount) belowArgCount = minCount;
1921519216
if (argCount < maxCount && maxCount < aboveArgCount) aboveArgCount = maxCount;
19216-
min = Math.min(min, minCount);
19217+
if (minCount < min) {
19218+
min = minCount;
19219+
closestSignature = sig;
19220+
}
1921719221
max = Math.max(max, maxCount);
1921819222
}
1921919223

@@ -19226,16 +19230,29 @@ namespace ts {
1922619230
argCount--;
1922719231
}
1922819232

19233+
let related: DiagnosticWithLocation | undefined;
19234+
if (closestSignature && getMinArgumentCount(closestSignature) > argCount && closestSignature.declaration) {
19235+
const paramDecl = closestSignature.declaration.parameters[closestSignature.thisParameter ? argCount + 1 : argCount];
19236+
if (paramDecl) {
19237+
related = createDiagnosticForNode(
19238+
paramDecl,
19239+
isBindingPattern(paramDecl.name) ? Diagnostics.An_argument_matching_this_binding_pattern_was_not_provided : Diagnostics.An_argument_for_0_was_not_provided,
19240+
!paramDecl.name ? argCount : !isBindingPattern(paramDecl.name) ? idText(getFirstIdentifier(paramDecl.name)) : undefined
19241+
);
19242+
}
19243+
}
1922919244
if (hasRestParameter || hasSpreadArgument) {
1923019245
const error = hasRestParameter && hasSpreadArgument ? Diagnostics.Expected_at_least_0_arguments_but_got_1_or_more :
1923119246
hasRestParameter ? Diagnostics.Expected_at_least_0_arguments_but_got_1 :
1923219247
Diagnostics.Expected_0_arguments_but_got_1_or_more;
19233-
return createDiagnosticForNode(node, error, paramRange, argCount);
19248+
const diagnostic = createDiagnosticForNode(node, error, paramRange, argCount);
19249+
return related ? addRelatedInfo(diagnostic, related) : diagnostic;
1923419250
}
1923519251
if (min < argCount && argCount < max) {
1923619252
return createDiagnosticForNode(node, Diagnostics.No_overload_expects_0_arguments_but_overloads_do_exist_that_expect_either_1_or_2_arguments, argCount, belowArgCount, aboveArgCount);
1923719253
}
19238-
return createDiagnosticForNode(node, Diagnostics.Expected_0_arguments_but_got_1, paramRange, argCount);
19254+
const diagnostic = createDiagnosticForNode(node, Diagnostics.Expected_0_arguments_but_got_1, paramRange, argCount);
19255+
return related ? addRelatedInfo(diagnostic, related) : diagnostic;
1923919256
}
1924019257

1924119258
function getTypeArgumentArityError(node: Node, signatures: ReadonlyArray<Signature>, typeArguments: NodeArray<TypeNode>) {

src/compiler/diagnosticMessages.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3708,6 +3708,14 @@
37083708
"category": "Message",
37093709
"code": 6209
37103710
},
3711+
"An argument for '{0}' was not provided.": {
3712+
"category": "Message",
3713+
"code": 6210
3714+
},
3715+
"An argument matching this binding pattern was not provided.": {
3716+
"category": "Message",
3717+
"code": 6211
3718+
},
37113719

37123720
"Projects to reference": {
37133721
"category": "Message",
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
tests/cases/compiler/arityErrorRelatedSpanBindingPattern.ts(5,1): error TS2554: Expected 3 arguments, but got 2.
2+
tests/cases/compiler/arityErrorRelatedSpanBindingPattern.ts(7,1): error TS2554: Expected 3 arguments, but got 2.
3+
4+
5+
==== tests/cases/compiler/arityErrorRelatedSpanBindingPattern.ts (2 errors) ====
6+
function foo(a, b, {c}): void {}
7+
8+
function bar(a, b, [c]): void {}
9+
10+
foo("", 0);
11+
~~~~~~~~~~
12+
!!! error TS2554: Expected 3 arguments, but got 2.
13+
!!! related TS6211 tests/cases/compiler/arityErrorRelatedSpanBindingPattern.ts:1:20: An argument matching this binding pattern was not provided.
14+
15+
bar("", 0);
16+
~~~~~~~~~~
17+
!!! error TS2554: Expected 3 arguments, but got 2.
18+
!!! related TS6211 tests/cases/compiler/arityErrorRelatedSpanBindingPattern.ts:3:20: An argument matching this binding pattern was not provided.
19+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [arityErrorRelatedSpanBindingPattern.ts]
2+
function foo(a, b, {c}): void {}
3+
4+
function bar(a, b, [c]): void {}
5+
6+
foo("", 0);
7+
8+
bar("", 0);
9+
10+
11+
//// [arityErrorRelatedSpanBindingPattern.js]
12+
function foo(a, b, _a) {
13+
var c = _a.c;
14+
}
15+
function bar(a, b, _a) {
16+
var c = _a[0];
17+
}
18+
foo("", 0);
19+
bar("", 0);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/compiler/arityErrorRelatedSpanBindingPattern.ts ===
2+
function foo(a, b, {c}): void {}
3+
>foo : Symbol(foo, Decl(arityErrorRelatedSpanBindingPattern.ts, 0, 0))
4+
>a : Symbol(a, Decl(arityErrorRelatedSpanBindingPattern.ts, 0, 13))
5+
>b : Symbol(b, Decl(arityErrorRelatedSpanBindingPattern.ts, 0, 15))
6+
>c : Symbol(c, Decl(arityErrorRelatedSpanBindingPattern.ts, 0, 20))
7+
8+
function bar(a, b, [c]): void {}
9+
>bar : Symbol(bar, Decl(arityErrorRelatedSpanBindingPattern.ts, 0, 32))
10+
>a : Symbol(a, Decl(arityErrorRelatedSpanBindingPattern.ts, 2, 13))
11+
>b : Symbol(b, Decl(arityErrorRelatedSpanBindingPattern.ts, 2, 15))
12+
>c : Symbol(c, Decl(arityErrorRelatedSpanBindingPattern.ts, 2, 20))
13+
14+
foo("", 0);
15+
>foo : Symbol(foo, Decl(arityErrorRelatedSpanBindingPattern.ts, 0, 0))
16+
17+
bar("", 0);
18+
>bar : Symbol(bar, Decl(arityErrorRelatedSpanBindingPattern.ts, 0, 32))
19+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/compiler/arityErrorRelatedSpanBindingPattern.ts ===
2+
function foo(a, b, {c}): void {}
3+
>foo : (a: any, b: any, { c }: { c: any; }) => void
4+
>a : any
5+
>b : any
6+
>c : any
7+
8+
function bar(a, b, [c]): void {}
9+
>bar : (a: any, b: any, [c]: [any]) => void
10+
>a : any
11+
>b : any
12+
>c : any
13+
14+
foo("", 0);
15+
>foo("", 0) : void
16+
>foo : (a: any, b: any, { c }: { c: any; }) => void
17+
>"" : ""
18+
>0 : 0
19+
20+
bar("", 0);
21+
>bar("", 0) : void
22+
>bar : (a: any, b: any, [c]: [any]) => void
23+
>"" : ""
24+
>0 : 0
25+

tests/baselines/reference/baseCheck.errors.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ tests/cases/compiler/baseCheck.ts(26,9): error TS2304: Cannot find name 'x'.
3232
class D extends C { constructor(public z: number) { super(this.z) } } // too few params
3333
~~~~~~~~~~~~~
3434
!!! error TS2554: Expected 2 arguments, but got 1.
35+
!!! related TS6210 tests/cases/compiler/baseCheck.ts:1:34: An argument for 'y' was not provided.
3536
~~~~
3637
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
3738
class E extends C { constructor(public z: number) { super(0, this.z) } }

tests/baselines/reference/blockScopedSameNameFunctionDeclarationES5.errors.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ tests/cases/compiler/blockScopedSameNameFunctionDeclarationES5.ts(16,1): error T
3434
foo(10);
3535
foo(); // not ok - needs number
3636
~~~~~
37-
!!! error TS2554: Expected 1 arguments, but got 0.
37+
!!! error TS2554: Expected 1 arguments, but got 0.
38+
!!! related TS6210 tests/cases/compiler/blockScopedSameNameFunctionDeclarationES5.ts:1:14: An argument for 'a' was not provided.

tests/baselines/reference/blockScopedSameNameFunctionDeclarationES6.errors.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ tests/cases/compiler/blockScopedSameNameFunctionDeclarationES6.ts(16,1): error T
3434
foo(10);
3535
foo(); // not ok - needs number
3636
~~~~~
37-
!!! error TS2554: Expected 1 arguments, but got 0.
37+
!!! error TS2554: Expected 1 arguments, but got 0.
38+
!!! related TS6210 tests/cases/compiler/blockScopedSameNameFunctionDeclarationES6.ts:1:14: An argument for 'a' was not provided.

tests/baselines/reference/blockScopedSameNameFunctionDeclarationStrictES5.errors.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ tests/cases/compiler/blockScopedSameNameFunctionDeclarationStrictES5.ts(17,1): e
3131
foo(); // not ok - needs number
3232
~~~~~
3333
!!! error TS2554: Expected 1 arguments, but got 0.
34+
!!! related TS6210 tests/cases/compiler/blockScopedSameNameFunctionDeclarationStrictES5.ts:2:14: An argument for 'a' was not provided.
3435
}
3536
foo(10);
3637
foo(); // not ok - needs number
3738
~~~~~
38-
!!! error TS2554: Expected 1 arguments, but got 0.
39+
!!! error TS2554: Expected 1 arguments, but got 0.
40+
!!! related TS6210 tests/cases/compiler/blockScopedSameNameFunctionDeclarationStrictES5.ts:2:14: An argument for 'a' was not provided.

0 commit comments

Comments
 (0)