Skip to content

Commit f92edb7

Browse files
committed
Better error message for accidental calls to get-accessors
1 parent eac0738 commit f92edb7

9 files changed

+111
-6
lines changed

src/compiler/checker.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25771,14 +25771,21 @@ namespace ts {
2577125771
error(node, Diagnostics.Value_of_type_0_is_not_callable_Did_you_mean_to_include_new, typeToString(funcType));
2577225772
}
2577325773
else {
25774-
let relatedInformation: DiagnosticRelatedInformation | undefined;
25775-
if (node.arguments.length === 1) {
25774+
const relatedInformation: DiagnosticRelatedInformation[] = [];
25775+
if (node.arguments.length === 0) {
25776+
// Diagnose get accessors incorrectly called as functions
25777+
const { resolvedSymbol } = getNodeLinks(node.expression);
25778+
if (resolvedSymbol && resolvedSymbol.flags & SymbolFlags.GetAccessor) {
25779+
relatedInformation.push(createDiagnosticForNode(node.expression, Diagnostics._0_is_a_get_accessor_did_you_mean_to_use_it_without, getTextOfNode(node.expression)));
25780+
}
25781+
}
25782+
else if (node.arguments.length === 1) {
2577625783
const text = getSourceFileOfNode(node).text;
2577725784
if (isLineBreak(text.charCodeAt(skipTrivia(text, node.expression.end, /* stopAfterLineBreak */ true) - 1))) {
25778-
relatedInformation = createDiagnosticForNode(node.expression, Diagnostics.Are_you_missing_a_semicolon);
25785+
relatedInformation.push(createDiagnosticForNode(node.expression, Diagnostics.Are_you_missing_a_semicolon));
2577925786
}
2578025787
}
25781-
invocationError(node.expression, apparentType, SignatureKind.Call, relatedInformation);
25788+
invocationError(node.expression, apparentType, SignatureKind.Call, ...relatedInformation);
2578225789
}
2578325790
return resolveErrorCall(node);
2578425791
}
@@ -26046,7 +26053,7 @@ namespace ts {
2604626053
relatedMessage: maybeMissingAwait ? Diagnostics.Did_you_forget_to_use_await : undefined,
2604726054
};
2604826055
}
26049-
function invocationError(errorTarget: Node, apparentType: Type, kind: SignatureKind, relatedInformation?: DiagnosticRelatedInformation) {
26056+
function invocationError(errorTarget: Node, apparentType: Type, kind: SignatureKind, ...relatedInformation: DiagnosticRelatedInformation[]) {
2605026057
const { messageChain, relatedMessage: relatedInfo } = invocationErrorDetails(apparentType, kind);
2605126058
const diagnostic = createDiagnosticForNodeFromMessageChain(errorTarget, messageChain);
2605226059
if (relatedInfo) {
@@ -26058,7 +26065,7 @@ namespace ts {
2605826065
diagnostic.length = length;
2605926066
}
2606026067
diagnostics.add(diagnostic);
26061-
invocationErrorRecovery(apparentType, kind, relatedInformation ? addRelatedInfo(diagnostic, relatedInformation) : diagnostic);
26068+
invocationErrorRecovery(apparentType, kind, addRelatedInfo(diagnostic, ...relatedInformation));
2606226069
}
2606326070

2606426071
function invocationErrorRecovery(apparentType: Type, kind: SignatureKind, diagnostic: Diagnostic) {

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4384,6 +4384,10 @@
43844384
"category": "Error",
43854385
"code": 6231
43864386
},
4387+
"'{0}' is a 'get' accessor; did you mean to use it without '()'?": {
4388+
"category": "Message",
4389+
"code": 6232
4390+
},
43874391

43884392
"Projects to reference": {
43894393
"category": "Message",
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests/cases/compiler/accessorAccidentalCallDiagnostic.ts(6,14): error TS2349: This expression is not callable.
2+
Type 'Number' has no call signatures.
3+
4+
5+
==== tests/cases/compiler/accessorAccidentalCallDiagnostic.ts (1 errors) ====
6+
// https://github.com/microsoft/TypeScript/issues/24554
7+
class Test24554 {
8+
get property(): number { return 1; }
9+
}
10+
function test24554(x: Test24554) {
11+
return x.property();
12+
~~~~~~~~
13+
!!! error TS2349: This expression is not callable.
14+
!!! error TS2349: Type 'Number' has no call signatures.
15+
!!! related TS6232 tests/cases/compiler/accessorAccidentalCallDiagnostic.ts:6:12: 'x.property' is a 'get' accessor; did you mean to use it without '()'?
16+
}
17+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//// [accessorAccidentalCallDiagnostic.ts]
2+
// https://github.com/microsoft/TypeScript/issues/24554
3+
class Test24554 {
4+
get property(): number { return 1; }
5+
}
6+
function test24554(x: Test24554) {
7+
return x.property();
8+
}
9+
10+
11+
//// [accessorAccidentalCallDiagnostic.js]
12+
// https://github.com/microsoft/TypeScript/issues/24554
13+
var Test24554 = /** @class */ (function () {
14+
function Test24554() {
15+
}
16+
Object.defineProperty(Test24554.prototype, "property", {
17+
get: function () { return 1; },
18+
enumerable: false,
19+
configurable: true
20+
});
21+
return Test24554;
22+
}());
23+
function test24554(x) {
24+
return x.property();
25+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/compiler/accessorAccidentalCallDiagnostic.ts ===
2+
// https://github.com/microsoft/TypeScript/issues/24554
3+
class Test24554 {
4+
>Test24554 : Symbol(Test24554, Decl(accessorAccidentalCallDiagnostic.ts, 0, 0))
5+
6+
get property(): number { return 1; }
7+
>property : Symbol(Test24554.property, Decl(accessorAccidentalCallDiagnostic.ts, 1, 17))
8+
}
9+
function test24554(x: Test24554) {
10+
>test24554 : Symbol(test24554, Decl(accessorAccidentalCallDiagnostic.ts, 3, 1))
11+
>x : Symbol(x, Decl(accessorAccidentalCallDiagnostic.ts, 4, 19))
12+
>Test24554 : Symbol(Test24554, Decl(accessorAccidentalCallDiagnostic.ts, 0, 0))
13+
14+
return x.property();
15+
>x.property : Symbol(Test24554.property, Decl(accessorAccidentalCallDiagnostic.ts, 1, 17))
16+
>x : Symbol(x, Decl(accessorAccidentalCallDiagnostic.ts, 4, 19))
17+
>property : Symbol(Test24554.property, Decl(accessorAccidentalCallDiagnostic.ts, 1, 17))
18+
}
19+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== tests/cases/compiler/accessorAccidentalCallDiagnostic.ts ===
2+
// https://github.com/microsoft/TypeScript/issues/24554
3+
class Test24554 {
4+
>Test24554 : Test24554
5+
6+
get property(): number { return 1; }
7+
>property : number
8+
>1 : 1
9+
}
10+
function test24554(x: Test24554) {
11+
>test24554 : (x: Test24554) => any
12+
>x : Test24554
13+
14+
return x.property();
15+
>x.property() : any
16+
>x.property : number
17+
>x : Test24554
18+
>property : number
19+
}
20+

tests/baselines/reference/instancePropertiesInheritedIntoClassType.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIn
3535
~
3636
!!! error TS2349: This expression is not callable.
3737
!!! error TS2349: Type 'Number' has no call signatures.
38+
!!! 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 '()'?
3839

3940
}
4041

@@ -64,4 +65,5 @@ tests/cases/conformance/classes/members/classTypes/instancePropertiesInheritedIn
6465
~
6566
!!! error TS2349: This expression is not callable.
6667
!!! error TS2349: Type 'String' has no call signatures.
68+
!!! 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 '()'?
6769
}

tests/baselines/reference/instancePropertyInClassType.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.t
3333
~
3434
!!! error TS2349: This expression is not callable.
3535
!!! error TS2349: Type 'Number' has no call signatures.
36+
!!! 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 '()'?
3637

3738
}
3839

@@ -60,4 +61,5 @@ tests/cases/conformance/classes/members/classTypes/instancePropertyInClassType.t
6061
~
6162
!!! error TS2349: This expression is not callable.
6263
!!! error TS2349: Type 'String' has no call signatures.
64+
!!! 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 '()'?
6365
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @target: es5
2+
3+
// https://github.com/microsoft/TypeScript/issues/24554
4+
class Test24554 {
5+
get property(): number { return 1; }
6+
}
7+
function test24554(x: Test24554) {
8+
return x.property();
9+
}

0 commit comments

Comments
 (0)