get appropriate parent jsdoc node for arrow functions

This commit is contained in:
xiaofa 2019-01-26 23:59:32 +08:00
parent c9c9f859f3
commit ddbac8f3fe
3 changed files with 25 additions and 8 deletions

View File

@ -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 <HasJSDoc>signature.parent;
}
return <HasJSDoc>signature.parent.parent;
}
function tryMergeJsdocTags(oldTag: JSDocTag, newTag: JSDocTag): JSDocTag | undefined {

View File

@ -340,9 +340,7 @@ namespace ts.textChanges {
}
public insertJsdocCommentBefore(sourceFile: SourceFile, node: HasJSDoc, tag: JSDoc): void {
const fnStart = node.kind === SyntaxKind.ArrowFunction
? node.parent.parent.getStart(sourceFile)
: node.getStart(sourceFile);
const fnStart = node.getStart(sourceFile);
if (node.jsDoc) {
for (const jsdoc of node.jsDoc) {
this.deleteRange(sourceFile, {

View File

@ -6,13 +6,22 @@
// @Filename: test.js
////const foo = x => x.y + 1;
////class C {
//// m = x => x.y + 1;
////}
verify.codeFix({
description: "Infer parameter types from usage",
index: 0,
verify.codeFixAll({
fixId: "inferFromUsage",
fixAllDescription: "Infer all types from usage",
newFileContent:
`/**
* @param {{ y: number; }} x
*/
const foo = x => x.y + 1;`,
const foo = x => x.y + 1;
class C {
/**
* @param {{ y: number; }} x
*/
m = x => x.y + 1;
}`,
});