Skip to content

Commit 8136047

Browse files
authored
Code fix for accidental calls to get-accessors (microsoft#38749)
* add code fix * fix forEachChild and update some baselines * excluding them from forEachChild is easier! * cleanup * review feedback * fix whitespace
1 parent b977f86 commit 8136047

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5741,6 +5741,10 @@
57415741
"category": "Message",
57425742
"code": 95125
57435743
},
5744+
"Remove parentheses": {
5745+
"category": "Message",
5746+
"code": 95126
5747+
},
57445748

57455749
"No value exists in scope for the shorthand property '{0}'. Either declare one or provide an initializer.": {
57465750
"category": "Error",
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* @internal */
2+
namespace ts.codefix {
3+
const fixId = "removeAccidentalCallParentheses";
4+
const errorCodes = [
5+
Diagnostics.This_expression_is_not_callable_because_it_is_a_get_accessor_Did_you_mean_to_use_it_without.code,
6+
];
7+
registerCodeFix({
8+
errorCodes,
9+
getCodeActions(context) {
10+
const callExpression = findAncestor(getTokenAtPosition(context.sourceFile, context.span.start), isCallExpression);
11+
if (!callExpression) {
12+
return undefined;
13+
}
14+
const changes = textChanges.ChangeTracker.with(context, t => {
15+
t.deleteRange(context.sourceFile, { pos: callExpression.expression.end, end: callExpression.end });
16+
});
17+
return [createCodeFixActionWithoutFixAll(fixId, changes, Diagnostics.Remove_parentheses)];
18+
},
19+
fixIds: [fixId],
20+
});
21+
}

src/services/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
"codefixes/fixAddModuleReferTypeMissingTypeof.ts",
100100
"codefixes/wrapJsxInFragment.ts",
101101
"codefixes/convertToMappedObjectType.ts",
102+
"codefixes/removeAccidentalCallParentheses.ts",
102103
"codefixes/removeUnnecessaryAwait.ts",
103104
"codefixes/splitTypeOnlyImport.ts",
104105
"codefixes/convertConstToLet.ts",
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// class Test24554 {
4+
//// get property(): number { return 1; }
5+
//// }
6+
//// function test24554(x: Test24554) {
7+
//// return x.property();
8+
//// }
9+
//// function test_2(x: { y: Test24554 }) {
10+
//// return x.y.property ( /* bye */ );
11+
//// }
12+
13+
verify.codeFix({
14+
description: "Remove parentheses",
15+
index: 0,
16+
newFileContent:
17+
`class Test24554 {
18+
get property(): number { return 1; }
19+
}
20+
function test24554(x: Test24554) {
21+
return x.property;
22+
}
23+
function test_2(x: { y: Test24554 }) {
24+
return x.y.property ( /* bye */ );
25+
}`
26+
});
27+
28+
verify.codeFix({
29+
description: "Remove parentheses",
30+
index: 1,
31+
newFileContent:
32+
`class Test24554 {
33+
get property(): number { return 1; }
34+
}
35+
function test24554(x: Test24554) {
36+
return x.property();
37+
}
38+
function test_2(x: { y: Test24554 }) {
39+
return x.y.property;
40+
}`
41+
});

0 commit comments

Comments
 (0)