From 093315570619526234a690ee626b6af6b262b0bb Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 9 Oct 2018 16:03:31 -0700 Subject: [PATCH] Always emit related diagnostic when a call expression can be fixed by adding a semicolon --- src/compiler/checker.ts | 2 +- .../betterErrorForAccidentalCall.errors.txt | 39 +++++++++++++++++++ ...ons.js => betterErrorForAccidentalCall.js} | 12 +++--- .../betterErrorForAccidentalCall.symbols | 25 ++++++++++++ ...pes => betterErrorForAccidentalCall.types} | 23 +++++------ ...CallingTypeAssertionExpressions.errors.txt | 39 ------------------- ...llyCallingTypeAssertionExpressions.symbols | 25 ------------ ...ons.ts => betterErrorForAccidentalCall.ts} | 6 +-- 8 files changed, 86 insertions(+), 85 deletions(-) create mode 100644 tests/baselines/reference/betterErrorForAccidentalCall.errors.txt rename tests/baselines/reference/{betterErrorForAccidentallyCallingTypeAssertionExpressions.js => betterErrorForAccidentalCall.js} (53%) create mode 100644 tests/baselines/reference/betterErrorForAccidentalCall.symbols rename tests/baselines/reference/{betterErrorForAccidentallyCallingTypeAssertionExpressions.types => betterErrorForAccidentalCall.types} (65%) delete mode 100644 tests/baselines/reference/betterErrorForAccidentallyCallingTypeAssertionExpressions.errors.txt delete mode 100644 tests/baselines/reference/betterErrorForAccidentallyCallingTypeAssertionExpressions.symbols rename tests/cases/compiler/{betterErrorForAccidentallyCallingTypeAssertionExpressions.ts => betterErrorForAccidentalCall.ts} (77%) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index fe2ce42652af4..0632af424aac0 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -20046,7 +20046,7 @@ namespace ts { } else { let relatedInformation: DiagnosticRelatedInformation | undefined; - if (node.arguments.length === 1 && isTypeAssertion(first(node.arguments))) { + if (node.arguments.length === 1) { const text = getSourceFileOfNode(node).text; if (isLineBreak(text.charCodeAt(skipTrivia(text, node.expression.end, /* stopAfterLineBreak */ true) - 1))) { relatedInformation = createDiagnosticForNode(node.expression, Diagnostics.It_is_highly_likely_that_you_are_missing_a_semicolon); diff --git a/tests/baselines/reference/betterErrorForAccidentalCall.errors.txt b/tests/baselines/reference/betterErrorForAccidentalCall.errors.txt new file mode 100644 index 0000000000000..f5358b45a4fa3 --- /dev/null +++ b/tests/baselines/reference/betterErrorForAccidentalCall.errors.txt @@ -0,0 +1,39 @@ +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. +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. +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. +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. +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. + + +==== tests/cases/compiler/betterErrorForAccidentalCall.ts (5 errors) ==== + declare function foo(): string; + + foo()(1 as number).toString(); + ~~~~~~~~~~~~~~~~~~ +!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures. + + foo() (1 as number).toString(); + ~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures. + + foo() + ~~~~~ + (1 as number).toString(); + ~~~~~~~~~~~~~ +!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures. +!!! related TS2734 tests/cases/compiler/betterErrorForAccidentalCall.ts:7:1: It is highly likely that you are missing a semicolon. + + foo() + ~~~~~ + (1 + 2).toString(); + ~~~~~~~~~~~ +!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures. +!!! related TS2734 tests/cases/compiler/betterErrorForAccidentalCall.ts:10:1: It is highly likely that you are missing a semicolon. + + foo() + ~~~~~ + (1).toString(); + ~~~~~~~~~~~~~~~ +!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures. +!!! related TS2734 tests/cases/compiler/betterErrorForAccidentalCall.ts:13:1: It is highly likely that you are missing a semicolon. + \ No newline at end of file diff --git a/tests/baselines/reference/betterErrorForAccidentallyCallingTypeAssertionExpressions.js b/tests/baselines/reference/betterErrorForAccidentalCall.js similarity index 53% rename from tests/baselines/reference/betterErrorForAccidentallyCallingTypeAssertionExpressions.js rename to tests/baselines/reference/betterErrorForAccidentalCall.js index 877ed539e7175..deb2b22d2f608 100644 --- a/tests/baselines/reference/betterErrorForAccidentallyCallingTypeAssertionExpressions.js +++ b/tests/baselines/reference/betterErrorForAccidentalCall.js @@ -1,4 +1,4 @@ -//// [betterErrorForAccidentallyCallingTypeAssertionExpressions.ts] +//// [betterErrorForAccidentalCall.ts] declare function foo(): string; foo()(1 as number).toString(); @@ -8,16 +8,16 @@ foo() (1 as number).toString(); foo() (1 as number).toString(); -foo() - (1 as number).toString(); +foo() + (1 + 2).toString(); -foo() +foo() (1).toString(); -//// [betterErrorForAccidentallyCallingTypeAssertionExpressions.js] -foo()(1).toString(); +//// [betterErrorForAccidentalCall.js] foo()(1).toString(); foo()(1).toString(); foo()(1).toString(); +foo()(1 + 2).toString(); foo()(1).toString(); diff --git a/tests/baselines/reference/betterErrorForAccidentalCall.symbols b/tests/baselines/reference/betterErrorForAccidentalCall.symbols new file mode 100644 index 0000000000000..9471b0bb06055 --- /dev/null +++ b/tests/baselines/reference/betterErrorForAccidentalCall.symbols @@ -0,0 +1,25 @@ +=== tests/cases/compiler/betterErrorForAccidentalCall.ts === +declare function foo(): string; +>foo : Symbol(foo, Decl(betterErrorForAccidentalCall.ts, 0, 0)) + +foo()(1 as number).toString(); +>foo : Symbol(foo, Decl(betterErrorForAccidentalCall.ts, 0, 0)) + +foo() (1 as number).toString(); +>foo : Symbol(foo, Decl(betterErrorForAccidentalCall.ts, 0, 0)) + +foo() +>foo : Symbol(foo, Decl(betterErrorForAccidentalCall.ts, 0, 0)) + +(1 as number).toString(); + +foo() +>foo : Symbol(foo, Decl(betterErrorForAccidentalCall.ts, 0, 0)) + + (1 + 2).toString(); + +foo() +>foo : Symbol(foo, Decl(betterErrorForAccidentalCall.ts, 0, 0)) + + (1).toString(); + diff --git a/tests/baselines/reference/betterErrorForAccidentallyCallingTypeAssertionExpressions.types b/tests/baselines/reference/betterErrorForAccidentalCall.types similarity index 65% rename from tests/baselines/reference/betterErrorForAccidentallyCallingTypeAssertionExpressions.types rename to tests/baselines/reference/betterErrorForAccidentalCall.types index 54564d7462c50..1a3dfbd47f36c 100644 --- a/tests/baselines/reference/betterErrorForAccidentallyCallingTypeAssertionExpressions.types +++ b/tests/baselines/reference/betterErrorForAccidentalCall.types @@ -1,4 +1,4 @@ -=== tests/cases/compiler/betterErrorForAccidentallyCallingTypeAssertionExpressions.ts === +=== tests/cases/compiler/betterErrorForAccidentalCall.ts === declare function foo(): string; >foo : () => string @@ -34,22 +34,23 @@ foo() >1 : 1 >toString : any -foo() ->foo() (1 as number).toString() : any ->foo() (1 as number).toString : any ->foo() (1 as number) : any +foo() +>foo() (1 + 2).toString() : any +>foo() (1 + 2).toString : any +>foo() (1 + 2) : any >foo() : string >foo : () => string - (1 as number).toString(); ->1 as number : number + (1 + 2).toString(); +>1 + 2 : number >1 : 1 +>2 : 2 >toString : any -foo() ->foo() (1).toString() : any ->foo() (1).toString : any ->foo() (1) : any +foo() +>foo() (1).toString() : any +>foo() (1).toString : any +>foo() (1) : any >foo() : string >foo : () => string diff --git a/tests/baselines/reference/betterErrorForAccidentallyCallingTypeAssertionExpressions.errors.txt b/tests/baselines/reference/betterErrorForAccidentallyCallingTypeAssertionExpressions.errors.txt deleted file mode 100644 index 023e40a70dad3..0000000000000 --- a/tests/baselines/reference/betterErrorForAccidentallyCallingTypeAssertionExpressions.errors.txt +++ /dev/null @@ -1,39 +0,0 @@ -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. -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. -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. -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. -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. - - -==== tests/cases/compiler/betterErrorForAccidentallyCallingTypeAssertionExpressions.ts (5 errors) ==== - declare function foo(): string; - - foo()(1 as number).toString(); - ~~~~~~~~~~~~~~~~~~ -!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures. - - foo() (1 as number).toString(); - ~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures. - - foo() - ~~~~~ - (1 as number).toString(); - ~~~~~~~~~~~~~ -!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures. -!!! related TS2734 tests/cases/compiler/betterErrorForAccidentallyCallingTypeAssertionExpressions.ts:7:1: It is highly likely that you are missing a semicolon. - - foo() - ~~~~~~~~ - (1 as number).toString(); - ~~~~~~~~~~~~~~~~~ -!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures. -!!! related TS2734 tests/cases/compiler/betterErrorForAccidentallyCallingTypeAssertionExpressions.ts:10:1: It is highly likely that you are missing a semicolon. - - foo() - ~~~~~~~~ - (1).toString(); - ~~~~~~~~~~~~~~~ -!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures. -!!! related TS2734 tests/cases/compiler/betterErrorForAccidentallyCallingTypeAssertionExpressions.ts:13:1: It is highly likely that you are missing a semicolon. - \ No newline at end of file diff --git a/tests/baselines/reference/betterErrorForAccidentallyCallingTypeAssertionExpressions.symbols b/tests/baselines/reference/betterErrorForAccidentallyCallingTypeAssertionExpressions.symbols deleted file mode 100644 index 9dc2e6769372b..0000000000000 --- a/tests/baselines/reference/betterErrorForAccidentallyCallingTypeAssertionExpressions.symbols +++ /dev/null @@ -1,25 +0,0 @@ -=== tests/cases/compiler/betterErrorForAccidentallyCallingTypeAssertionExpressions.ts === -declare function foo(): string; ->foo : Symbol(foo, Decl(betterErrorForAccidentallyCallingTypeAssertionExpressions.ts, 0, 0)) - -foo()(1 as number).toString(); ->foo : Symbol(foo, Decl(betterErrorForAccidentallyCallingTypeAssertionExpressions.ts, 0, 0)) - -foo() (1 as number).toString(); ->foo : Symbol(foo, Decl(betterErrorForAccidentallyCallingTypeAssertionExpressions.ts, 0, 0)) - -foo() ->foo : Symbol(foo, Decl(betterErrorForAccidentallyCallingTypeAssertionExpressions.ts, 0, 0)) - -(1 as number).toString(); - -foo() ->foo : Symbol(foo, Decl(betterErrorForAccidentallyCallingTypeAssertionExpressions.ts, 0, 0)) - - (1 as number).toString(); - -foo() ->foo : Symbol(foo, Decl(betterErrorForAccidentallyCallingTypeAssertionExpressions.ts, 0, 0)) - - (1).toString(); - diff --git a/tests/cases/compiler/betterErrorForAccidentallyCallingTypeAssertionExpressions.ts b/tests/cases/compiler/betterErrorForAccidentalCall.ts similarity index 77% rename from tests/cases/compiler/betterErrorForAccidentallyCallingTypeAssertionExpressions.ts rename to tests/cases/compiler/betterErrorForAccidentalCall.ts index 42c3025c8e3f6..e8303965ce041 100644 --- a/tests/cases/compiler/betterErrorForAccidentallyCallingTypeAssertionExpressions.ts +++ b/tests/cases/compiler/betterErrorForAccidentalCall.ts @@ -7,8 +7,8 @@ foo() (1 as number).toString(); foo() (1 as number).toString(); -foo() - (1 as number).toString(); +foo() + (1 + 2).toString(); -foo() +foo() (1).toString();