From ffdd43908950d7ee03cea9258a04afab627c1b52 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Mon, 1 Mar 2021 19:54:06 +0200 Subject: [PATCH] fix(43006): skip trivia in a function name --- src/services/codefixes/fixAddMissingMember.ts | 2 +- src/services/codefixes/helpers.ts | 2 +- .../codeFixAddMissingFunctionDeclaration17.ts | 17 +++++++++++++++ .../codeFixAddMissingFunctionDeclaration18.ts | 21 +++++++++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 tests/cases/fourslash/codeFixAddMissingFunctionDeclaration17.ts create mode 100644 tests/cases/fourslash/codeFixAddMissingFunctionDeclaration18.ts diff --git a/src/services/codefixes/fixAddMissingMember.ts b/src/services/codefixes/fixAddMissingMember.ts index 3e696435aa967..0b240648a7b17 100644 --- a/src/services/codefixes/fixAddMissingMember.ts +++ b/src/services/codefixes/fixAddMissingMember.ts @@ -402,7 +402,7 @@ namespace ts.codefix { function addFunctionDeclaration(changes: textChanges.ChangeTracker, context: CodeFixContextBase, info: FunctionInfo) { const importAdder = createImportAdder(context.sourceFile, context.program, context.preferences, context.host); - const functionDeclaration = createSignatureDeclarationFromCallExpression(SyntaxKind.FunctionDeclaration, context, importAdder, info.call, info.token, info.modifierFlags, info.parentDeclaration) as FunctionDeclaration; + const functionDeclaration = createSignatureDeclarationFromCallExpression(SyntaxKind.FunctionDeclaration, context, importAdder, info.call, idText(info.token), info.modifierFlags, info.parentDeclaration) as FunctionDeclaration; changes.insertNodeAtEndOfScope(info.sourceFile, info.parentDeclaration, functionDeclaration); } } diff --git a/src/services/codefixes/helpers.ts b/src/services/codefixes/helpers.ts index d3d5a46e0ec2d..c6a982546fa22 100644 --- a/src/services/codefixes/helpers.ts +++ b/src/services/codefixes/helpers.ts @@ -252,7 +252,7 @@ namespace ts.codefix { context: CodeFixContextBase, importAdder: ImportAdder, call: CallExpression, - name: Identifier, + name: Identifier | string, modifierFlags: ModifierFlags, contextNode: Node ) { diff --git a/tests/cases/fourslash/codeFixAddMissingFunctionDeclaration17.ts b/tests/cases/fourslash/codeFixAddMissingFunctionDeclaration17.ts new file mode 100644 index 0000000000000..53c88c9ef7297 --- /dev/null +++ b/tests/cases/fourslash/codeFixAddMissingFunctionDeclaration17.ts @@ -0,0 +1,17 @@ +/// + +////// comment +////foo(); + +verify.codeFix({ + index: 0, + description: [ts.Diagnostics.Add_missing_function_declaration_0.message, "foo"], + newFileContent: +`// comment +foo(); + +function foo() { + throw new Error("Function not implemented."); +} +` +}); diff --git a/tests/cases/fourslash/codeFixAddMissingFunctionDeclaration18.ts b/tests/cases/fourslash/codeFixAddMissingFunctionDeclaration18.ts new file mode 100644 index 0000000000000..4a7fa06887d4a --- /dev/null +++ b/tests/cases/fourslash/codeFixAddMissingFunctionDeclaration18.ts @@ -0,0 +1,21 @@ +/// + +/////** +//// * comment +//// */ +////foo(); + +verify.codeFix({ + index: 0, + description: [ts.Diagnostics.Add_missing_function_declaration_0.message, "foo"], + newFileContent: +`/** + * comment + */ +foo(); + +function foo() { + throw new Error("Function not implemented."); +} +` +});