Skip to content

Commit 0933155

Browse files
author
Andy Hanson
committed
Always emit related diagnostic when a call expression can be fixed by adding a semicolon
1 parent 07966dc commit 0933155

8 files changed

+86
-85
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20046,7 +20046,7 @@ namespace ts {
2004620046
}
2004720047
else {
2004820048
let relatedInformation: DiagnosticRelatedInformation | undefined;
20049-
if (node.arguments.length === 1 && isTypeAssertion(first(node.arguments))) {
20049+
if (node.arguments.length === 1) {
2005020050
const text = getSourceFileOfNode(node).text;
2005120051
if (isLineBreak(text.charCodeAt(skipTrivia(text, node.expression.end, /* stopAfterLineBreak */ true) - 1))) {
2005220052
relatedInformation = createDiagnosticForNode(node.expression, Diagnostics.It_is_highly_likely_that_you_are_missing_a_semicolon);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
tests/cases/compiler/betterErrorForAccidentalCall.ts(3,1): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
2+
tests/cases/compiler/betterErrorForAccidentalCall.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/betterErrorForAccidentalCall.ts(7,1): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
4+
tests/cases/compiler/betterErrorForAccidentalCall.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/betterErrorForAccidentalCall.ts(13,1): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
6+
7+
8+
==== tests/cases/compiler/betterErrorForAccidentalCall.ts (5 errors) ====
9+
declare function foo(): string;
10+
11+
foo()(1 as number).toString();
12+
~~~~~~~~~~~~~~~~~~
13+
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
14+
15+
foo() (1 as number).toString();
16+
~~~~~~~~~~~~~~~~~~~~~
17+
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
18+
19+
foo()
20+
~~~~~
21+
(1 as number).toString();
22+
~~~~~~~~~~~~~
23+
!!! 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/betterErrorForAccidentalCall.ts:7:1: It is highly likely that you are missing a semicolon.
25+
26+
foo()
27+
~~~~~
28+
(1 + 2).toString();
29+
~~~~~~~~~~~
30+
!!! 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/betterErrorForAccidentalCall.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/betterErrorForAccidentalCall.ts:13:1: It is highly likely that you are missing a semicolon.
39+
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//// [betterErrorForAccidentallyCallingTypeAssertionExpressions.ts]
1+
//// [betterErrorForAccidentalCall.ts]
22
declare function foo(): string;
33

44
foo()(1 as number).toString();
@@ -8,16 +8,16 @@ foo() (1 as number).toString();
88
foo()
99
(1 as number).toString();
1010

11-
foo()
12-
(1 as number).toString();
11+
foo()
12+
(1 + 2).toString();
1313

14-
foo()
14+
foo()
1515
(<number>1).toString();
1616

1717

18-
//// [betterErrorForAccidentallyCallingTypeAssertionExpressions.js]
19-
foo()(1).toString();
18+
//// [betterErrorForAccidentalCall.js]
2019
foo()(1).toString();
2120
foo()(1).toString();
2221
foo()(1).toString();
22+
foo()(1 + 2).toString();
2323
foo()(1).toString();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/compiler/betterErrorForAccidentalCall.ts ===
2+
declare function foo(): string;
3+
>foo : Symbol(foo, Decl(betterErrorForAccidentalCall.ts, 0, 0))
4+
5+
foo()(1 as number).toString();
6+
>foo : Symbol(foo, Decl(betterErrorForAccidentalCall.ts, 0, 0))
7+
8+
foo() (1 as number).toString();
9+
>foo : Symbol(foo, Decl(betterErrorForAccidentalCall.ts, 0, 0))
10+
11+
foo()
12+
>foo : Symbol(foo, Decl(betterErrorForAccidentalCall.ts, 0, 0))
13+
14+
(1 as number).toString();
15+
16+
foo()
17+
>foo : Symbol(foo, Decl(betterErrorForAccidentalCall.ts, 0, 0))
18+
19+
(1 + 2).toString();
20+
21+
foo()
22+
>foo : Symbol(foo, Decl(betterErrorForAccidentalCall.ts, 0, 0))
23+
24+
(<number>1).toString();
25+
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
=== tests/cases/compiler/betterErrorForAccidentallyCallingTypeAssertionExpressions.ts ===
1+
=== tests/cases/compiler/betterErrorForAccidentalCall.ts ===
22
declare function foo(): string;
33
>foo : () => string
44

@@ -34,22 +34,23 @@ foo()
3434
>1 : 1
3535
>toString : any
3636

37-
foo()
38-
>foo() (1 as number).toString() : any
39-
>foo() (1 as number).toString : any
40-
>foo() (1 as number) : any
37+
foo()
38+
>foo() (1 + 2).toString() : any
39+
>foo() (1 + 2).toString : any
40+
>foo() (1 + 2) : any
4141
>foo() : string
4242
>foo : () => string
4343

44-
(1 as number).toString();
45-
>1 as number : number
44+
(1 + 2).toString();
45+
>1 + 2 : number
4646
>1 : 1
47+
>2 : 2
4748
>toString : any
4849

49-
foo()
50-
>foo() (<number>1).toString() : any
51-
>foo() (<number>1).toString : any
52-
>foo() (<number>1) : any
50+
foo()
51+
>foo() (<number>1).toString() : any
52+
>foo() (<number>1).toString : any
53+
>foo() (<number>1) : any
5354
>foo() : string
5455
>foo : () => string
5556

tests/baselines/reference/betterErrorForAccidentallyCallingTypeAssertionExpressions.errors.txt

Lines changed: 0 additions & 39 deletions
This file was deleted.

tests/baselines/reference/betterErrorForAccidentallyCallingTypeAssertionExpressions.symbols

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ foo() (1 as number).toString();
77
foo()
88
(1 as number).toString();
99

10-
foo()
11-
(1 as number).toString();
10+
foo()
11+
(1 + 2).toString();
1212

13-
foo()
13+
foo()
1414
(<number>1).toString();

0 commit comments

Comments
 (0)