diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 16dd1663fb5d5..6a32d4f86e3ab 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -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", diff --git a/src/services/codefixes/removeAccidentalCallParentheses.ts b/src/services/codefixes/removeAccidentalCallParentheses.ts new file mode 100644 index 0000000000000..32a7d4606f02a --- /dev/null +++ b/src/services/codefixes/removeAccidentalCallParentheses.ts @@ -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], + }); +} diff --git a/src/services/tsconfig.json b/src/services/tsconfig.json index 1fd5ba3058165..9567185c7a9ac 100644 --- a/src/services/tsconfig.json +++ b/src/services/tsconfig.json @@ -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", diff --git a/tests/cases/fourslash/codeFixRemoveAccidentalCallParentheses.ts b/tests/cases/fourslash/codeFixRemoveAccidentalCallParentheses.ts new file mode 100644 index 0000000000000..b44720e59e090 --- /dev/null +++ b/tests/cases/fourslash/codeFixRemoveAccidentalCallParentheses.ts @@ -0,0 +1,41 @@ +/// + +//// 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; +}` +});