Skip to content

Commit 2cf2bbd

Browse files
committed
improve test case and add related diagnostic
1 parent 6b2ea46 commit 2cf2bbd

6 files changed

+45
-16
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19655,14 +19655,14 @@ namespace ts {
1965519655
error(node, Diagnostics.Value_of_type_0_is_not_callable_Did_you_mean_to_include_new, typeToString(funcType));
1965619656
}
1965719657
else {
19658+
let relatedInformation: DiagnosticRelatedInformation | undefined;
1965819659
if (node.arguments.length === 1 && isTypeAssertion(first(node.arguments))) {
1965919660
const text = getSourceFileOfNode(node).text;
19660-
const pos = skipTrivia(text, node.expression.end, /* stepAfterLineBreak */ true) - 1;
19661-
if (isLineBreak(text.charCodeAt(pos))) {
19662-
error(node.expression, Diagnostics.It_is_highly_likely_that_you_are_missing_a_semicolon);
19661+
if (isLineBreak(text.charCodeAt(skipTrivia(text, node.expression.end, /* stopAfterLineBreak */ true) - 1))) {
19662+
relatedInformation = createDiagnosticForNode(node.expression, Diagnostics.It_is_highly_likely_that_you_are_missing_a_semicolon);
1966319663
}
1966419664
}
19665-
invocationError(node, apparentType, SignatureKind.Call);
19665+
invocationError(node, apparentType, SignatureKind.Call, relatedInformation);
1966619666
}
1966719667
return resolveErrorCall(node);
1966819668
}
@@ -19832,11 +19832,12 @@ namespace ts {
1983219832
return true;
1983319833
}
1983419834

19835-
function invocationError(node: Node, apparentType: Type, kind: SignatureKind) {
19836-
invocationErrorRecovery(apparentType, kind, error(node, kind === SignatureKind.Call
19837-
? Diagnostics.Cannot_invoke_an_expression_whose_type_lacks_a_call_signature_Type_0_has_no_compatible_call_signatures
19838-
: Diagnostics.Cannot_use_new_with_an_expression_whose_type_lacks_a_call_or_construct_signature
19839-
, typeToString(apparentType)));
19835+
function invocationError(node: Node, apparentType: Type, kind: SignatureKind, relatedInformation?: DiagnosticRelatedInformation) {
19836+
const diagnostic = error(node, (kind === SignatureKind.Call ?
19837+
Diagnostics.Cannot_invoke_an_expression_whose_type_lacks_a_call_signature_Type_0_has_no_compatible_call_signatures :
19838+
Diagnostics.Cannot_use_new_with_an_expression_whose_type_lacks_a_call_or_construct_signature
19839+
), typeToString(apparentType));
19840+
invocationErrorRecovery(apparentType, kind, relatedInformation ? addRelatedInfo(diagnostic, relatedInformation) : diagnostic);
1984019841
}
1984119842

1984219843
function invocationErrorRecovery(apparentType: Type, kind: SignatureKind, diagnostic: Diagnostic) {
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
tests/cases/compiler/betterErrorForAccidentallyCallingTypeAssertionExpressions.ts(3,1): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
22
tests/cases/compiler/betterErrorForAccidentallyCallingTypeAssertionExpressions.ts(5,1): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
3-
tests/cases/compiler/betterErrorForAccidentallyCallingTypeAssertionExpressions.ts(7,1): error TS2734: It is highly likely that you are missing a semicolon.
43
tests/cases/compiler/betterErrorForAccidentallyCallingTypeAssertionExpressions.ts(7,1): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
5-
tests/cases/compiler/betterErrorForAccidentallyCallingTypeAssertionExpressions.ts(10,1): error TS2734: It is highly likely that you are missing a semicolon.
64
tests/cases/compiler/betterErrorForAccidentallyCallingTypeAssertionExpressions.ts(10,1): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
5+
tests/cases/compiler/betterErrorForAccidentallyCallingTypeAssertionExpressions.ts(13,1): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
76

87

9-
==== tests/cases/compiler/betterErrorForAccidentallyCallingTypeAssertionExpressions.ts (6 errors) ====
8+
==== tests/cases/compiler/betterErrorForAccidentallyCallingTypeAssertionExpressions.ts (5 errors) ====
109
declare function foo(): string;
1110

1211
foo()(1 as number).toString();
@@ -19,17 +18,22 @@ tests/cases/compiler/betterErrorForAccidentallyCallingTypeAssertionExpressions.t
1918

2019
foo()
2120
~~~~~
22-
!!! error TS2734: It is highly likely that you are missing a semicolon.
23-
~~~~~
2421
(1 as number).toString();
2522
~~~~~~~~~~~~~
2623
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
24+
!!! related TS2734 tests/cases/compiler/betterErrorForAccidentallyCallingTypeAssertionExpressions.ts:7:1: It is highly likely that you are missing a semicolon.
2725

2826
foo()
29-
~~~~~
30-
!!! error TS2734: It is highly likely that you are missing a semicolon.
3127
~~~~~~~~
3228
(1 as number).toString();
3329
~~~~~~~~~~~~~~~~~
3430
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
31+
!!! related TS2734 tests/cases/compiler/betterErrorForAccidentallyCallingTypeAssertionExpressions.ts:10:1: It is highly likely that you are missing a semicolon.
32+
33+
foo()
34+
~~~~~~~~
35+
(<number>1).toString();
36+
~~~~~~~~~~~~~~~
37+
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
38+
!!! related TS2734 tests/cases/compiler/betterErrorForAccidentallyCallingTypeAssertionExpressions.ts:13:1: It is highly likely that you are missing a semicolon.
3539

tests/baselines/reference/betterErrorForAccidentallyCallingTypeAssertionExpressions.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ foo()
1010

1111
foo()
1212
(1 as number).toString();
13+
14+
foo()
15+
(<number>1).toString();
1316

1417

1518
//// [betterErrorForAccidentallyCallingTypeAssertionExpressions.js]
1619
foo()(1).toString();
1720
foo()(1).toString();
1821
foo()(1).toString();
1922
foo()(1).toString();
23+
foo()(1).toString();

tests/baselines/reference/betterErrorForAccidentallyCallingTypeAssertionExpressions.symbols

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ foo()
1818

1919
(1 as number).toString();
2020

21+
foo()
22+
>foo : Symbol(foo, Decl(betterErrorForAccidentallyCallingTypeAssertionExpressions.ts, 0, 0))
23+
24+
(<number>1).toString();
25+

tests/baselines/reference/betterErrorForAccidentallyCallingTypeAssertionExpressions.types

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,15 @@ foo()
4646
>1 : 1
4747
>toString : any
4848

49+
foo()
50+
>foo() (<number>1).toString() : any
51+
>foo() (<number>1).toString : any
52+
>foo() (<number>1) : any
53+
>foo() : string
54+
>foo : () => string
55+
56+
(<number>1).toString();
57+
><number>1 : number
58+
>1 : 1
59+
>toString : any
60+

tests/cases/compiler/betterErrorForAccidentallyCallingTypeAssertionExpressions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ foo()
99

1010
foo()
1111
(1 as number).toString();
12+
13+
foo()
14+
(<number>1).toString();

0 commit comments

Comments
 (0)