diff --git a/src/services/codefixes/inferFromUsage.ts b/src/services/codefixes/inferFromUsage.ts index 8e5b962116d..f47f618f79d 100644 --- a/src/services/codefixes/inferFromUsage.ts +++ b/src/services/codefixes/inferFromUsage.ts @@ -274,7 +274,17 @@ namespace ts.codefix { return !!merged; })); const tag = createJSDocComment(comments.join("\n"), createNodeArray([...(oldTags || emptyArray), ...unmergedNewTags])); - changes.insertJsdocCommentBefore(sourceFile, parent, tag); + const jsDocNode = parent.kind === SyntaxKind.ArrowFunction ? getJsDocNodeForArrowFunction(parent) : parent; + jsDocNode.jsDoc = parent.jsDoc; + jsDocNode.jsDocCache = parent.jsDocCache; + changes.insertJsdocCommentBefore(sourceFile, jsDocNode, tag); + } + + function getJsDocNodeForArrowFunction(signature: ArrowFunction): HasJSDoc { + if (signature.parent.kind === SyntaxKind.PropertyDeclaration) { + return signature.parent; + } + return signature.parent.parent; } function tryMergeJsdocTags(oldTag: JSDocTag, newTag: JSDocTag): JSDocTag | undefined { diff --git a/tests/cases/fourslash/codeFixInferFromUsageArrowJS.ts b/tests/cases/fourslash/codeFixInferFromUsageArrowJS.ts new file mode 100644 index 00000000000..0a800fd7d63 --- /dev/null +++ b/tests/cases/fourslash/codeFixInferFromUsageArrowJS.ts @@ -0,0 +1,27 @@ +/// +// @allowJs: true +// @checkJs: true +// @noEmit: true +// @noImplicitAny: true +// @Filename: test.js + +////const foo = x => x.y + 1; +////class C { +//// m = x => x.y + 1; +////} + +verify.codeFixAll({ + fixId: "inferFromUsage", + fixAllDescription: "Infer all types from usage", + newFileContent: +`/** + * @param {{ y: number; }} x + */ +const foo = x => x.y + 1; +class C { + /** + * @param {{ y: number; }} x + */ + m = x => x.y + 1; +}`, +}); \ No newline at end of file