Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error message for accidental calls to get-accessors #37800

Merged
merged 8 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26383,7 +26383,7 @@ namespace ts {
return true;
}

function invocationErrorDetails(apparentType: Type, kind: SignatureKind): { messageChain: DiagnosticMessageChain, relatedMessage: DiagnosticMessage | undefined } {
function invocationErrorDetails(errorTarget: Node, apparentType: Type, kind: SignatureKind): { messageChain: DiagnosticMessageChain, relatedMessage: DiagnosticMessage | undefined } {
let errorInfo: DiagnosticMessageChain | undefined;
const isCall = kind === SignatureKind.Call;
const awaitedType = getAwaitedType(apparentType);
Expand Down Expand Up @@ -26452,16 +26452,24 @@ namespace ts {
typeToString(apparentType)
);
}

let headMessage = isCall ? Diagnostics.This_expression_is_not_callable : Diagnostics.This_expression_is_not_constructable;

// Diagnose get accessors incorrectly called as functions
if (isCallExpression(errorTarget.parent) && errorTarget.parent.arguments.length === 0) {
const { resolvedSymbol } = getNodeLinks(errorTarget);
if (resolvedSymbol && resolvedSymbol.flags & SymbolFlags.GetAccessor) {
headMessage = Diagnostics.This_expression_is_not_callable_because_it_is_a_get_accessor_Did_you_mean_to_use_it_without;
}
}

return {
messageChain: chainDiagnosticMessages(
errorInfo,
isCall ? Diagnostics.This_expression_is_not_callable : Diagnostics.This_expression_is_not_constructable
),
messageChain: chainDiagnosticMessages(errorInfo, headMessage),
relatedMessage: maybeMissingAwait ? Diagnostics.Did_you_forget_to_use_await : undefined,
};
}
function invocationError(errorTarget: Node, apparentType: Type, kind: SignatureKind, relatedInformation?: DiagnosticRelatedInformation) {
const { messageChain, relatedMessage: relatedInfo } = invocationErrorDetails(apparentType, kind);
const { messageChain, relatedMessage: relatedInfo } = invocationErrorDetails(errorTarget, apparentType, kind);
const diagnostic = createDiagnosticForNodeFromMessageChain(errorTarget, messageChain);
if (relatedInfo) {
addRelatedInfo(diagnostic, createDiagnosticForNode(errorTarget, relatedInfo));
Expand Down Expand Up @@ -26565,7 +26573,7 @@ namespace ts {

const headMessage = getDiagnosticHeadMessageForDecoratorResolution(node);
if (!callSignatures.length) {
const errorDetails = invocationErrorDetails(apparentType, SignatureKind.Call);
const errorDetails = invocationErrorDetails(node.expression, apparentType, SignatureKind.Call);
const messageChain = chainDiagnosticMessages(errorDetails.messageChain, headMessage);
const diag = createDiagnosticForNodeFromMessageChain(node.expression, messageChain);
if (errorDetails.relatedMessage) {
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4416,6 +4416,10 @@
"category": "Error",
"code": 6233
},
"This expression is not callable because it is a 'get' accessor. Did you mean to use it without '()'?": {
"category": "Message",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be an Error, not a Message.

"code": 6234
},

"Projects to reference": {
"category": "Message",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
tests/cases/compiler/accessorAccidentalCallDiagnostic.ts(6,14): message TS6234: This expression is not callable because it is a 'get' accessor. Did you mean to use it without '()'?
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();
~~~~~~~~
!!! message TS6234: This expression is not callable because it is a 'get' accessor. Did you mean to use it without '()'?
!!! message TS6234: Type 'Number' has no call signatures.
}

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
@@ -1,10 +1,10 @@
tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIntoClassType.ts(4,13): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIntoClassType.ts(7,13): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIntoClassType.ts(19,16): error TS2349: This expression is not callable.
tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIntoClassType.ts(19,16): message TS6234: This expression is not callable because it is a 'get' accessor. Did you mean to use it without '()'?
Type 'Number' has no call signatures.
tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIntoClassType.ts(26,13): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIntoClassType.ts(29,13): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIntoClassType.ts(41,16): error TS2349: This expression is not callable.
tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIntoClassType.ts(41,16): message TS6234: This expression is not callable because it is a 'get' accessor. Did you mean to use it without '()'?
Type 'String' has no call signatures.


Expand Down Expand Up @@ -33,8 +33,8 @@ tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIn
r.y = 4;
var r6 = d.y(); // error
~
!!! error TS2349: This expression is not callable.
!!! error TS2349: Type 'Number' has no call signatures.
!!! message TS6234: This expression is not callable because it is a 'get' accessor. Did you mean to use it without '()'?
!!! message TS6234: Type 'Number' has no call signatures.

}

Expand Down Expand Up @@ -62,6 +62,6 @@ tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIn
r.y = '';
var r6 = d.y(); // error
~
!!! error TS2349: This expression is not callable.
!!! error TS2349: Type 'String' has no call signatures.
!!! message TS6234: This expression is not callable because it is a 'get' accessor. Did you mean to use it without '()'?
!!! message TS6234: Type 'String' has no call signatures.
}
12 changes: 6 additions & 6 deletions tests/baselines/reference/instancePropertyInClassType.errors.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.ts(4,13): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.ts(7,13): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.ts(17,16): error TS2349: This expression is not callable.
tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.ts(17,16): message TS6234: This expression is not callable because it is a 'get' accessor. Did you mean to use it without '()'?
Type 'Number' has no call signatures.
tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.ts(24,13): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.ts(27,13): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.ts(37,16): error TS2349: This expression is not callable.
tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.ts(37,16): message TS6234: This expression is not callable because it is a 'get' accessor. Did you mean to use it without '()'?
Type 'String' has no call signatures.


Expand All @@ -31,8 +31,8 @@ tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.t
r.y = 4;
var r6 = c.y(); // error
~
!!! error TS2349: This expression is not callable.
!!! error TS2349: Type 'Number' has no call signatures.
sandersn marked this conversation as resolved.
Show resolved Hide resolved
!!! message TS6234: This expression is not callable because it is a 'get' accessor. Did you mean to use it without '()'?
!!! message TS6234: Type 'Number' has no call signatures.

}

Expand All @@ -58,6 +58,6 @@ tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.t
r.y = '';
var r6 = c.y(); // error
~
!!! error TS2349: This expression is not callable.
!!! error TS2349: Type 'String' has no call signatures.
!!! message TS6234: This expression is not callable because it is a 'get' accessor. Did you mean to use it without '()'?
!!! message TS6234: Type 'String' has no call signatures.
}
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();
}