Skip to content

Code fix for accidental calls to get-accessors #38749

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

Merged
merged 10 commits into from
Jun 18, 2020
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -5737,6 +5737,10 @@
"category": "Message",
"code": 95125
},
"Remove parentheses": {
"category": "Message",
"code": 95126
},

"No value exists in scope for the shorthand property '{0}'. Either declare one or provide an initializer.": {
"category": "Error",
Expand Down
21 changes: 21 additions & 0 deletions src/services/codefixes/removeAccidentalCallParentheses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* @internal */
namespace ts.codefix {
const fixId = "removeAccidentalCallParentheses";
const errorCodes = [
Diagnostics.This_expression_is_not_callable_because_it_is_a_get_accessor_Did_you_mean_to_use_it_without.code,
];
registerCodeFix({
errorCodes,
getCodeActions(context) {
const callExpression = findAncestor(getTokenAtPosition(context.sourceFile, context.span.start), isCallExpression);
if (!callExpression) {
return undefined;
}
const changes = textChanges.ChangeTracker.with(context, t => {
t.deleteRange(context.sourceFile, { pos: callExpression.expression.end, end: callExpression.end });
});
return [createCodeFixActionWithoutFixAll(fixId, changes, Diagnostics.Remove_parentheses)];
},
fixIds: [fixId],
});
}
1 change: 1 addition & 0 deletions src/services/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"codefixes/fixAddModuleReferTypeMissingTypeof.ts",
"codefixes/wrapJsxInFragment.ts",
"codefixes/convertToMappedObjectType.ts",
"codefixes/removeAccidentalCallParentheses.ts",
"codefixes/removeUnnecessaryAwait.ts",
"codefixes/splitTypeOnlyImport.ts",
"codefixes/convertConstToLet.ts",
Expand Down
41 changes: 41 additions & 0 deletions tests/cases/fourslash/codeFixRemoveAccidentalCallParentheses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/// <reference path='fourslash.ts' />

//// class Test24554 {
//// get property(): number { return 1; }
//// }
//// function test24554(x: Test24554) {
//// return x.property();
//// }
//// function test_2(x: { y: Test24554 }) {
//// return x.y.property ( /* bye */ );
//// }

verify.codeFix({
description: "Remove parentheses",
index: 0,
newFileContent:
`class Test24554 {
get property(): number { return 1; }
}
function test24554(x: Test24554) {
return x.property;
}
function test_2(x: { y: Test24554 }) {
return x.y.property ( /* bye */ );
}`
});

verify.codeFix({
description: "Remove parentheses",
index: 1,
newFileContent:
`class Test24554 {
get property(): number { return 1; }
}
function test24554(x: Test24554) {
return x.property();
}
function test_2(x: { y: Test24554 }) {
return x.y.property;
}`
});