improve Diagnostics for accidentally calling type-assertion expressions

This commit is contained in:
王文璐
2018-09-11 14:35:01 +08:00
parent af8e44ac85
commit 6b2ea463b2
7 changed files with 144 additions and 0 deletions

View File

@@ -19655,6 +19655,13 @@ namespace ts {
error(node, Diagnostics.Value_of_type_0_is_not_callable_Did_you_mean_to_include_new, typeToString(funcType));
}
else {
if (node.arguments.length === 1 && isTypeAssertion(first(node.arguments))) {
const text = getSourceFileOfNode(node).text;
const pos = skipTrivia(text, node.expression.end, /* stepAfterLineBreak */ true) - 1;
if (isLineBreak(text.charCodeAt(pos))) {
error(node.expression, Diagnostics.It_is_highly_likely_that_you_are_missing_a_semicolon);
}
}
invocationError(node, apparentType, SignatureKind.Call);
}
return resolveErrorCall(node);

View File

@@ -2457,6 +2457,10 @@
"category": "Error",
"code": 2733
},
"It is highly likely that you are missing a semicolon.": {
"category": "Error",
"code": 2734
},
"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",

View File

@@ -0,0 +1,35 @@
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 TS2734: It is highly likely that you are missing a semicolon.
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 TS2734: It is highly likely that you are missing a semicolon.
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 (6 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()
~~~~~
!!! error TS2734: It is highly likely that you are missing a semicolon.
~~~~~
(1 as number).toString();
~~~~~~~~~~~~~
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
foo()
~~~~~
!!! error TS2734: It is highly likely that you are missing a semicolon.
~~~~~~~~
(1 as number).toString();
~~~~~~~~~~~~~~~~~
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.

View File

@@ -0,0 +1,19 @@
//// [betterErrorForAccidentallyCallingTypeAssertionExpressions.ts]
declare function foo(): string;
foo()(1 as number).toString();
foo() (1 as number).toString();
foo()
(1 as number).toString();
foo()
(1 as number).toString();
//// [betterErrorForAccidentallyCallingTypeAssertionExpressions.js]
foo()(1).toString();
foo()(1).toString();
foo()(1).toString();
foo()(1).toString();

View File

@@ -0,0 +1,20 @@
=== 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();

View File

@@ -0,0 +1,48 @@
=== tests/cases/compiler/betterErrorForAccidentallyCallingTypeAssertionExpressions.ts ===
declare function foo(): string;
>foo : () => string
foo()(1 as number).toString();
>foo()(1 as number).toString() : any
>foo()(1 as number).toString : any
>foo()(1 as number) : any
>foo() : string
>foo : () => string
>1 as number : number
>1 : 1
>toString : any
foo() (1 as number).toString();
>foo() (1 as number).toString() : any
>foo() (1 as number).toString : any
>foo() (1 as number) : any
>foo() : string
>foo : () => string
>1 as number : number
>1 : 1
>toString : any
foo()
>foo()(1 as number).toString() : any
>foo()(1 as number).toString : any
>foo()(1 as number) : any
>foo() : string
>foo : () => string
(1 as number).toString();
>1 as number : number
>1 : 1
>toString : any
foo()
>foo() (1 as number).toString() : any
>foo() (1 as number).toString : any
>foo() (1 as number) : any
>foo() : string
>foo : () => string
(1 as number).toString();
>1 as number : number
>1 : 1
>toString : any

View File

@@ -0,0 +1,11 @@
declare function foo(): string;
foo()(1 as number).toString();
foo() (1 as number).toString();
foo()
(1 as number).toString();
foo()
(1 as number).toString();