Skip to content

Commit

Permalink
Better error message for accidental calls to get-accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
jtbandes committed Apr 5, 2020
1 parent eac0738 commit f92edb7
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25771,14 +25771,21 @@ namespace ts {
error(node, Diagnostics.Value_of_type_0_is_not_callable_Did_you_mean_to_include_new, typeToString(funcType));
}
else {
let relatedInformation: DiagnosticRelatedInformation | undefined;
if (node.arguments.length === 1) {
const relatedInformation: DiagnosticRelatedInformation[] = [];
if (node.arguments.length === 0) {
// Diagnose get accessors incorrectly called as functions
const { resolvedSymbol } = getNodeLinks(node.expression);
if (resolvedSymbol && resolvedSymbol.flags & SymbolFlags.GetAccessor) {
relatedInformation.push(createDiagnosticForNode(node.expression, Diagnostics._0_is_a_get_accessor_did_you_mean_to_use_it_without, getTextOfNode(node.expression)));
}
}
else 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.Are_you_missing_a_semicolon);
relatedInformation.push(createDiagnosticForNode(node.expression, Diagnostics.Are_you_missing_a_semicolon));
}
}
invocationError(node.expression, apparentType, SignatureKind.Call, relatedInformation);
invocationError(node.expression, apparentType, SignatureKind.Call, ...relatedInformation);
}
return resolveErrorCall(node);
}
Expand Down Expand Up @@ -26046,7 +26053,7 @@ namespace ts {
relatedMessage: maybeMissingAwait ? Diagnostics.Did_you_forget_to_use_await : undefined,
};
}
function invocationError(errorTarget: Node, apparentType: Type, kind: SignatureKind, relatedInformation?: DiagnosticRelatedInformation) {
function invocationError(errorTarget: Node, apparentType: Type, kind: SignatureKind, ...relatedInformation: DiagnosticRelatedInformation[]) {
const { messageChain, relatedMessage: relatedInfo } = invocationErrorDetails(apparentType, kind);
const diagnostic = createDiagnosticForNodeFromMessageChain(errorTarget, messageChain);
if (relatedInfo) {
Expand All @@ -26058,7 +26065,7 @@ namespace ts {
diagnostic.length = length;
}
diagnostics.add(diagnostic);
invocationErrorRecovery(apparentType, kind, relatedInformation ? addRelatedInfo(diagnostic, relatedInformation) : diagnostic);
invocationErrorRecovery(apparentType, kind, addRelatedInfo(diagnostic, ...relatedInformation));
}

function invocationErrorRecovery(apparentType: Type, kind: SignatureKind, diagnostic: Diagnostic) {
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4384,6 +4384,10 @@
"category": "Error",
"code": 6231
},
"'{0}' is a 'get' accessor; did you mean to use it without '()'?": {
"category": "Message",
"code": 6232
},

"Projects to reference": {
"category": "Message",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
tests/cases/compiler/accessorAccidentalCallDiagnostic.ts(6,14): error TS2349: This expression is not callable.
Type 'Number' has no call signatures.


==== tests/cases/compiler/accessorAccidentalCallDiagnostic.ts (1 errors) ====
// https://github.com/microsoft/TypeScript/issues/24554
class Test24554 {
get property(): number { return 1; }
}
function test24554(x: Test24554) {
return x.property();
~~~~~~~~
!!! error TS2349: This expression is not callable.
!!! error TS2349: Type 'Number' has no call signatures.
!!! related TS6232 tests/cases/compiler/accessorAccidentalCallDiagnostic.ts:6:12: 'x.property' is a 'get' accessor; did you mean to use it without '()'?
}

25 changes: 25 additions & 0 deletions tests/baselines/reference/accessorAccidentalCallDiagnostic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//// [accessorAccidentalCallDiagnostic.ts]
// https://github.com/microsoft/TypeScript/issues/24554
class Test24554 {
get property(): number { return 1; }
}
function test24554(x: Test24554) {
return x.property();
}


//// [accessorAccidentalCallDiagnostic.js]
// https://github.com/microsoft/TypeScript/issues/24554
var Test24554 = /** @class */ (function () {
function Test24554() {
}
Object.defineProperty(Test24554.prototype, "property", {
get: function () { return 1; },
enumerable: false,
configurable: true
});
return Test24554;
}());
function test24554(x) {
return x.property();
}
19 changes: 19 additions & 0 deletions tests/baselines/reference/accessorAccidentalCallDiagnostic.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
=== tests/cases/compiler/accessorAccidentalCallDiagnostic.ts ===
// https://github.com/microsoft/TypeScript/issues/24554
class Test24554 {
>Test24554 : Symbol(Test24554, Decl(accessorAccidentalCallDiagnostic.ts, 0, 0))

get property(): number { return 1; }
>property : Symbol(Test24554.property, Decl(accessorAccidentalCallDiagnostic.ts, 1, 17))
}
function test24554(x: Test24554) {
>test24554 : Symbol(test24554, Decl(accessorAccidentalCallDiagnostic.ts, 3, 1))
>x : Symbol(x, Decl(accessorAccidentalCallDiagnostic.ts, 4, 19))
>Test24554 : Symbol(Test24554, Decl(accessorAccidentalCallDiagnostic.ts, 0, 0))

return x.property();
>x.property : Symbol(Test24554.property, Decl(accessorAccidentalCallDiagnostic.ts, 1, 17))
>x : Symbol(x, Decl(accessorAccidentalCallDiagnostic.ts, 4, 19))
>property : Symbol(Test24554.property, Decl(accessorAccidentalCallDiagnostic.ts, 1, 17))
}

20 changes: 20 additions & 0 deletions tests/baselines/reference/accessorAccidentalCallDiagnostic.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=== tests/cases/compiler/accessorAccidentalCallDiagnostic.ts ===
// https://github.com/microsoft/TypeScript/issues/24554
class Test24554 {
>Test24554 : Test24554

get property(): number { return 1; }
>property : number
>1 : 1
}
function test24554(x: Test24554) {
>test24554 : (x: Test24554) => any
>x : Test24554

return x.property();
>x.property() : any
>x.property : number
>x : Test24554
>property : number
}

Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIn
~
!!! error TS2349: This expression is not callable.
!!! error TS2349: Type 'Number' has no call signatures.
!!! related TS6232 tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIntoClassType.ts:19:14: 'd.y' is a 'get' accessor; did you mean to use it without '()'?

}

Expand Down Expand Up @@ -64,4 +65,5 @@ tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIn
~
!!! error TS2349: This expression is not callable.
!!! error TS2349: Type 'String' has no call signatures.
!!! related TS6232 tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIntoClassType.ts:41:14: 'd.y' is a 'get' accessor; did you mean to use it without '()'?
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.t
~
!!! error TS2349: This expression is not callable.
!!! error TS2349: Type 'Number' has no call signatures.
!!! related TS6232 tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.ts:17:14: 'c.y' is a 'get' accessor; did you mean to use it without '()'?

}

Expand Down Expand Up @@ -60,4 +61,5 @@ tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.t
~
!!! error TS2349: This expression is not callable.
!!! error TS2349: Type 'String' has no call signatures.
!!! related TS6232 tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.ts:37:14: 'c.y' is a 'get' accessor; did you mean to use it without '()'?
}
9 changes: 9 additions & 0 deletions tests/cases/compiler/accessorAccidentalCallDiagnostic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @target: es5

// https://github.com/microsoft/TypeScript/issues/24554
class Test24554 {
get property(): number { return 1; }
}
function test24554(x: Test24554) {
return x.property();
}

0 comments on commit f92edb7

Please sign in to comment.