Skip to content

Add related span pointing at missing arguments #27013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19208,12 +19208,16 @@ namespace ts {
let aboveArgCount = Number.POSITIVE_INFINITY;

let argCount = args.length;
let closestSignature: Signature | undefined;
for (const sig of signatures) {
const minCount = getMinArgumentCount(sig);
const maxCount = getParameterCount(sig);
if (minCount < argCount && minCount > belowArgCount) belowArgCount = minCount;
if (argCount < maxCount && maxCount < aboveArgCount) aboveArgCount = maxCount;
min = Math.min(min, minCount);
if (minCount < min) {
min = minCount;
closestSignature = sig;
}
max = Math.max(max, maxCount);
}

Expand All @@ -19226,16 +19230,29 @@ namespace ts {
argCount--;
}

let related: DiagnosticWithLocation | undefined;
if (closestSignature && getMinArgumentCount(closestSignature) > argCount && closestSignature.declaration) {
const paramDecl = closestSignature.declaration.parameters[closestSignature.thisParameter ? argCount + 1 : argCount];
if (paramDecl) {
related = createDiagnosticForNode(
paramDecl,
isBindingPattern(paramDecl.name) ? Diagnostics.An_argument_matching_this_binding_pattern_was_not_provided : Diagnostics.An_argument_for_0_was_not_provided,
!paramDecl.name ? argCount : !isBindingPattern(paramDecl.name) ? idText(getFirstIdentifier(paramDecl.name)) : undefined
);
}
}
if (hasRestParameter || hasSpreadArgument) {
const error = hasRestParameter && hasSpreadArgument ? Diagnostics.Expected_at_least_0_arguments_but_got_1_or_more :
hasRestParameter ? Diagnostics.Expected_at_least_0_arguments_but_got_1 :
Diagnostics.Expected_0_arguments_but_got_1_or_more;
return createDiagnosticForNode(node, error, paramRange, argCount);
const diagnostic = createDiagnosticForNode(node, error, paramRange, argCount);
return related ? addRelatedInfo(diagnostic, related) : diagnostic;
}
if (min < argCount && argCount < max) {
return createDiagnosticForNode(node, Diagnostics.No_overload_expects_0_arguments_but_overloads_do_exist_that_expect_either_1_or_2_arguments, argCount, belowArgCount, aboveArgCount);
}
return createDiagnosticForNode(node, Diagnostics.Expected_0_arguments_but_got_1, paramRange, argCount);
const diagnostic = createDiagnosticForNode(node, Diagnostics.Expected_0_arguments_but_got_1, paramRange, argCount);
return related ? addRelatedInfo(diagnostic, related) : diagnostic;
}

function getTypeArgumentArityError(node: Node, signatures: ReadonlyArray<Signature>, typeArguments: NodeArray<TypeNode>) {
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3708,6 +3708,14 @@
"category": "Message",
"code": 6209
},
"An argument for '{0}' was not provided.": {
"category": "Message",
"code": 6210
},
"An argument matching this binding pattern was not provided.": {
"category": "Message",
"code": 6211
},

"Projects to reference": {
"category": "Message",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
tests/cases/compiler/arityErrorRelatedSpanBindingPattern.ts(5,1): error TS2554: Expected 3 arguments, but got 2.
tests/cases/compiler/arityErrorRelatedSpanBindingPattern.ts(7,1): error TS2554: Expected 3 arguments, but got 2.


==== tests/cases/compiler/arityErrorRelatedSpanBindingPattern.ts (2 errors) ====
function foo(a, b, {c}): void {}

function bar(a, b, [c]): void {}

foo("", 0);
~~~~~~~~~~
!!! error TS2554: Expected 3 arguments, but got 2.
!!! related TS6211 tests/cases/compiler/arityErrorRelatedSpanBindingPattern.ts:1:20: An argument matching this binding pattern was not provided.

bar("", 0);
~~~~~~~~~~
!!! error TS2554: Expected 3 arguments, but got 2.
!!! related TS6211 tests/cases/compiler/arityErrorRelatedSpanBindingPattern.ts:3:20: An argument matching this binding pattern was not provided.

19 changes: 19 additions & 0 deletions tests/baselines/reference/arityErrorRelatedSpanBindingPattern.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//// [arityErrorRelatedSpanBindingPattern.ts]
function foo(a, b, {c}): void {}

function bar(a, b, [c]): void {}

foo("", 0);

bar("", 0);


//// [arityErrorRelatedSpanBindingPattern.js]
function foo(a, b, _a) {
var c = _a.c;
}
function bar(a, b, _a) {
var c = _a[0];
}
foo("", 0);
bar("", 0);
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
=== tests/cases/compiler/arityErrorRelatedSpanBindingPattern.ts ===
function foo(a, b, {c}): void {}
>foo : Symbol(foo, Decl(arityErrorRelatedSpanBindingPattern.ts, 0, 0))
>a : Symbol(a, Decl(arityErrorRelatedSpanBindingPattern.ts, 0, 13))
>b : Symbol(b, Decl(arityErrorRelatedSpanBindingPattern.ts, 0, 15))
>c : Symbol(c, Decl(arityErrorRelatedSpanBindingPattern.ts, 0, 20))

function bar(a, b, [c]): void {}
>bar : Symbol(bar, Decl(arityErrorRelatedSpanBindingPattern.ts, 0, 32))
>a : Symbol(a, Decl(arityErrorRelatedSpanBindingPattern.ts, 2, 13))
>b : Symbol(b, Decl(arityErrorRelatedSpanBindingPattern.ts, 2, 15))
>c : Symbol(c, Decl(arityErrorRelatedSpanBindingPattern.ts, 2, 20))

foo("", 0);
>foo : Symbol(foo, Decl(arityErrorRelatedSpanBindingPattern.ts, 0, 0))

bar("", 0);
>bar : Symbol(bar, Decl(arityErrorRelatedSpanBindingPattern.ts, 0, 32))

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
=== tests/cases/compiler/arityErrorRelatedSpanBindingPattern.ts ===
function foo(a, b, {c}): void {}
>foo : (a: any, b: any, { c }: { c: any; }) => void
>a : any
>b : any
>c : any

function bar(a, b, [c]): void {}
>bar : (a: any, b: any, [c]: [any]) => void
>a : any
>b : any
>c : any

foo("", 0);
>foo("", 0) : void
>foo : (a: any, b: any, { c }: { c: any; }) => void
>"" : ""
>0 : 0

bar("", 0);
>bar("", 0) : void
>bar : (a: any, b: any, [c]: [any]) => void
>"" : ""
>0 : 0

1 change: 1 addition & 0 deletions tests/baselines/reference/baseCheck.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ tests/cases/compiler/baseCheck.ts(26,9): error TS2304: Cannot find name 'x'.
class D extends C { constructor(public z: number) { super(this.z) } } // too few params
~~~~~~~~~~~~~
!!! error TS2554: Expected 2 arguments, but got 1.
!!! related TS6210 tests/cases/compiler/baseCheck.ts:1:34: An argument for 'y' was not provided.
~~~~
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
class E extends C { constructor(public z: number) { super(0, this.z) } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ tests/cases/compiler/blockScopedSameNameFunctionDeclarationES5.ts(16,1): error T
foo(10);
foo(); // not ok - needs number
~~~~~
!!! error TS2554: Expected 1 arguments, but got 0.
!!! error TS2554: Expected 1 arguments, but got 0.
!!! related TS6210 tests/cases/compiler/blockScopedSameNameFunctionDeclarationES5.ts:1:14: An argument for 'a' was not provided.
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ tests/cases/compiler/blockScopedSameNameFunctionDeclarationES6.ts(16,1): error T
foo(10);
foo(); // not ok - needs number
~~~~~
!!! error TS2554: Expected 1 arguments, but got 0.
!!! error TS2554: Expected 1 arguments, but got 0.
!!! related TS6210 tests/cases/compiler/blockScopedSameNameFunctionDeclarationES6.ts:1:14: An argument for 'a' was not provided.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ tests/cases/compiler/blockScopedSameNameFunctionDeclarationStrictES5.ts(17,1): e
foo(); // not ok - needs number
~~~~~
!!! error TS2554: Expected 1 arguments, but got 0.
!!! related TS6210 tests/cases/compiler/blockScopedSameNameFunctionDeclarationStrictES5.ts:2:14: An argument for 'a' was not provided.
}
foo(10);
foo(); // not ok - needs number
~~~~~
!!! error TS2554: Expected 1 arguments, but got 0.
!!! error TS2554: Expected 1 arguments, but got 0.
!!! related TS6210 tests/cases/compiler/blockScopedSameNameFunctionDeclarationStrictES5.ts:2:14: An argument for 'a' was not provided.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ tests/cases/compiler/blockScopedSameNameFunctionDeclarationStrictES6.ts(17,1): e
foo(); // not ok
~~~~~
!!! error TS2554: Expected 1 arguments, but got 0.
!!! related TS6210 tests/cases/compiler/blockScopedSameNameFunctionDeclarationStrictES6.ts:2:14: An argument for 'a' was not provided.
}
foo(10);
foo(); // not ok - needs number
~~~~~
!!! error TS2554: Expected 1 arguments, but got 0.
!!! error TS2554: Expected 1 arguments, but got 0.
!!! related TS6210 tests/cases/compiler/blockScopedSameNameFunctionDeclarationStrictES6.ts:2:14: An argument for 'a' was not provided.
2 changes: 2 additions & 0 deletions tests/baselines/reference/callWithSpread2.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts(34,8): erro
prefix(...ns) // required parameters are required
~~~~~~~~~~~~~
!!! error TS2556: Expected 1-3 arguments, but got 0 or more.
!!! related TS6210 tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts:3:25: An argument for 's' was not provided.
prefix(...mixed)
~~~~~~~~~~~~~~~~
!!! error TS2556: Expected 1-3 arguments, but got 0 or more.
!!! related TS6210 tests/cases/conformance/expressions/functionCalls/callWithSpread2.ts:3:25: An argument for 's' was not provided.
prefix(...tuple)
~~~~~~~~
!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ tests/cases/conformance/salsa/second.ts(17,15): error TS2345: Argument of type '
super(); // error: not enough arguments
~~~~~~~
!!! error TS2554: Expected 1 arguments, but got 0.
!!! related TS6210 tests/cases/conformance/salsa/first.js:5:16: An argument for 'numberOxen' was not provided.
this.foonly = 12
}
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ tests/cases/conformance/classes/members/constructorFunctionTypes/classWithBaseCl
var c = new C(); // error
~~~~~~~
!!! error TS2554: Expected 1 arguments, but got 0.
!!! related TS6210 tests/cases/conformance/classes/members/constructorFunctionTypes/classWithBaseClassButNoConstructor.ts:2:17: An argument for 'x' was not provided.
var c2 = new C(1); // ok

class Base2<T,U> {
Expand All @@ -31,6 +32,7 @@ tests/cases/conformance/classes/members/constructorFunctionTypes/classWithBaseCl
var d = new D(); // error
~~~~~~~
!!! error TS2554: Expected 1 arguments, but got 0.
!!! related TS6210 tests/cases/conformance/classes/members/constructorFunctionTypes/classWithBaseClassButNoConstructor.ts:14:17: An argument for 'x' was not provided.
var d2 = new D(1); // ok

// specialized base class
Expand All @@ -42,6 +44,7 @@ tests/cases/conformance/classes/members/constructorFunctionTypes/classWithBaseCl
var d3 = new D(); // error
~~~~~~~
!!! error TS2554: Expected 1 arguments, but got 0.
!!! related TS6210 tests/cases/conformance/classes/members/constructorFunctionTypes/classWithBaseClassButNoConstructor.ts:14:17: An argument for 'x' was not provided.
var d4 = new D(1); // ok

class D3 extends Base2<string, number> {
Expand All @@ -52,4 +55,5 @@ tests/cases/conformance/classes/members/constructorFunctionTypes/classWithBaseCl
var d5 = new D(); // error
~~~~~~~
!!! error TS2554: Expected 1 arguments, but got 0.
!!! related TS6210 tests/cases/conformance/classes/members/constructorFunctionTypes/classWithBaseClassButNoConstructor.ts:14:17: An argument for 'x' was not provided.
var d6 = new D(1); // ok
6 changes: 6 additions & 0 deletions tests/baselines/reference/classWithConstructors.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ tests/cases/conformance/classes/members/constructorFunctionTypes/classWithConstr
var c = new C(); // error
~~~~~~~
!!! error TS2554: Expected 1 arguments, but got 0.
!!! related TS6210 tests/cases/conformance/classes/members/constructorFunctionTypes/classWithConstructors.ts:3:21: An argument for 'x' was not provided.
var c2 = new C(''); // ok

class C2 {
Expand All @@ -26,6 +27,7 @@ tests/cases/conformance/classes/members/constructorFunctionTypes/classWithConstr
var c3 = new C2(); // error
~~~~~~~~
!!! error TS2554: Expected 1 arguments, but got 0.
!!! related TS6210 tests/cases/conformance/classes/members/constructorFunctionTypes/classWithConstructors.ts:10:21: An argument for 'x' was not provided.
var c4 = new C2(''); // ok
var c5 = new C2(1); // ok

Expand All @@ -34,6 +36,7 @@ tests/cases/conformance/classes/members/constructorFunctionTypes/classWithConstr
var d = new D(); // error
~~~~~~~
!!! error TS2554: Expected 1 arguments, but got 0.
!!! related TS6210 tests/cases/conformance/classes/members/constructorFunctionTypes/classWithConstructors.ts:10:21: An argument for 'x' was not provided.
var d2 = new D(1); // ok
var d3 = new D(''); // ok
}
Expand All @@ -46,6 +49,7 @@ tests/cases/conformance/classes/members/constructorFunctionTypes/classWithConstr
var c = new C(); // error
~~~~~~~
!!! error TS2554: Expected 1 arguments, but got 0.
!!! related TS6210 tests/cases/conformance/classes/members/constructorFunctionTypes/classWithConstructors.ts:28:21: An argument for 'x' was not provided.
var c2 = new C(''); // ok

class C2<T,U> {
Expand All @@ -57,6 +61,7 @@ tests/cases/conformance/classes/members/constructorFunctionTypes/classWithConstr
var c3 = new C2(); // error
~~~~~~~~
!!! error TS2554: Expected 1-2 arguments, but got 0.
!!! related TS6210 tests/cases/conformance/classes/members/constructorFunctionTypes/classWithConstructors.ts:35:21: An argument for 'x' was not provided.
var c4 = new C2(''); // ok
var c5 = new C2(1, 2); // ok

Expand All @@ -65,6 +70,7 @@ tests/cases/conformance/classes/members/constructorFunctionTypes/classWithConstr
var d = new D(); // error
~~~~~~~
!!! error TS2554: Expected 1-2 arguments, but got 0.
!!! related TS6210 tests/cases/conformance/classes/members/constructorFunctionTypes/classWithConstructors.ts:35:21: An argument for 'x' was not provided.
var d2 = new D(1); // ok
var d3 = new D(''); // ok
}
6 changes: 5 additions & 1 deletion tests/baselines/reference/cloduleTest2.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ tests/cases/compiler/cloduleTest2.ts(36,10): error TS2554: Expected 1 arguments,
var r = new m3d(); // error
~~~~~~~~~
!!! error TS2554: Expected 1 arguments, but got 0.
!!! related TS6210 tests/cases/compiler/cloduleTest2.ts:3:37: An argument for 'foo' was not provided.
}

module T2 {
Expand All @@ -23,6 +24,7 @@ tests/cases/compiler/cloduleTest2.ts(36,10): error TS2554: Expected 1 arguments,
var r = new m3d(); // error
~~~~~~~~~
!!! error TS2554: Expected 1 arguments, but got 0.
!!! related TS6210 tests/cases/compiler/cloduleTest2.ts:8:37: An argument for 'foo' was not provided.
}

module T3 {
Expand Down Expand Up @@ -56,8 +58,10 @@ tests/cases/compiler/cloduleTest2.ts(36,10): error TS2554: Expected 1 arguments,
var r = new m3d(); // error
~~~~~~~~~
!!! error TS2554: Expected 1 arguments, but got 0.
!!! related TS6210 tests/cases/compiler/cloduleTest2.ts:32:33: An argument for 'foo' was not provided.

declare class m4d extends m3d { }
var r2 = new m4d(); // error
~~~~~~~~~
!!! error TS2554: Expected 1 arguments, but got 0.
!!! error TS2554: Expected 1 arguments, but got 0.
!!! related TS6210 tests/cases/compiler/cloduleTest2.ts:32:33: An argument for 'foo' was not provided.
1 change: 1 addition & 0 deletions tests/baselines/reference/constructorFunctions.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@ tests/cases/conformance/salsa/index.js(55,13): error TS2554: Expected 1 argument
var c7_v1 = new C7();
~~~~~~~~
!!! error TS2554: Expected 1 arguments, but got 0.
!!! related TS6210 tests/cases/conformance/salsa/index.js:53:13: An argument for 'num' was not provided.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ tests/cases/conformance/classes/constructorDeclarations/automaticConstructors/de
var r = new Derived(); // error
~~~~~~~~~~~~~
!!! error TS2554: Expected 1 arguments, but got 0.
!!! related TS6210 tests/cases/conformance/classes/constructorDeclarations/automaticConstructors/derivedClassWithoutExplicitConstructor.ts:3:17: An argument for 'x' was not provided.
var r2 = new Derived(1);

class Base2<T> {
Expand All @@ -31,4 +32,5 @@ tests/cases/conformance/classes/constructorDeclarations/automaticConstructors/de
var d = new D(); // error
~~~~~~~
!!! error TS2554: Expected 1 arguments, but got 0.
!!! related TS6210 tests/cases/conformance/classes/constructorDeclarations/automaticConstructors/derivedClassWithoutExplicitConstructor.ts:16:17: An argument for 'x' was not provided.
var d2 = new D(new Date()); // ok
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ tests/cases/conformance/classes/constructorDeclarations/automaticConstructors/de
var r = new Derived(); // error
~~~~~~~~~~~~~
!!! error TS2554: Expected 1-3 arguments, but got 0.
!!! related TS6210 tests/cases/conformance/classes/constructorDeclarations/automaticConstructors/derivedClassWithoutExplicitConstructor2.ts:3:17: An argument for 'x' was not provided.
var r2 = new Derived(1);
var r3 = new Derived(1, 2);
var r4 = new Derived(1, 2, 3);
Expand All @@ -37,6 +38,7 @@ tests/cases/conformance/classes/constructorDeclarations/automaticConstructors/de
var d = new D(); // error
~~~~~~~
!!! error TS2554: Expected 1-3 arguments, but got 0.
!!! related TS6210 tests/cases/conformance/classes/constructorDeclarations/automaticConstructors/derivedClassWithoutExplicitConstructor2.ts:20:17: An argument for 'x' was not provided.
var d2 = new D(new Date()); // ok
var d3 = new D(new Date(), new Date());
var d4 = new D(new Date(), new Date(), new Date());
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ tests/cases/conformance/classes/constructorDeclarations/automaticConstructors/de
var r = new Derived(); // error
~~~~~~~~~~~~~
!!! error TS2554: Expected 2 arguments, but got 0.
!!! related TS6210 tests/cases/conformance/classes/constructorDeclarations/automaticConstructors/derivedClassWithoutExplicitConstructor3.ts:10:17: An argument for 'y' was not provided.
var r2 = new Derived2(1); // error
~~~~~~~~~~~~~~~
!!! error TS2554: Expected 2 arguments, but got 1.
!!! related TS6210 tests/cases/conformance/classes/constructorDeclarations/automaticConstructors/derivedClassWithoutExplicitConstructor3.ts:10:28: An argument for 'z' was not provided.
var r3 = new Derived('', '');

class Base2<T> {
Expand All @@ -55,7 +57,9 @@ tests/cases/conformance/classes/constructorDeclarations/automaticConstructors/de
var d = new D2(); // error
~~~~~~~~
!!! error TS2554: Expected 2 arguments, but got 0.
!!! related TS6210 tests/cases/conformance/classes/constructorDeclarations/automaticConstructors/derivedClassWithoutExplicitConstructor3.ts:32:17: An argument for 'y' was not provided.
var d2 = new D2(new Date()); // error
~~~~~~~~~~~~~~~~~~
!!! error TS2554: Expected 2 arguments, but got 1.
!!! related TS6210 tests/cases/conformance/classes/constructorDeclarations/automaticConstructors/derivedClassWithoutExplicitConstructor3.ts:32:23: An argument for 'z' was not provided.
var d3 = new D2(new Date(), new Date()); // ok
Loading