From f35764d4ecf69e5c6be292fa7018fdc6f27f70c9 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Thu, 12 Oct 2017 14:28:34 -0700 Subject: [PATCH] Fix duplicated JSDoc comments Incorporate suppressLeadingAndTrailingTrivia just added by @amcasey. --- src/services/refactors/annotateWithTypeFromJSDoc.ts | 10 +++++++--- tests/cases/fourslash/annotateWithTypeFromJSDoc12.ts | 3 --- tests/cases/fourslash/annotateWithTypeFromJSDoc5.ts | 1 - tests/cases/fourslash/annotateWithTypeFromJSDoc6.ts | 1 - 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/services/refactors/annotateWithTypeFromJSDoc.ts b/src/services/refactors/annotateWithTypeFromJSDoc.ts index bc6dcd073bf..1e32df4a51a 100644 --- a/src/services/refactors/annotateWithTypeFromJSDoc.ts +++ b/src/services/refactors/annotateWithTypeFromJSDoc.ts @@ -62,14 +62,16 @@ namespace ts.refactor.annotateWithTypeFromJSDoc { const sourceFile = context.file; const token = getTokenAtPosition(sourceFile, context.startPosition, /*includeJsDocComment*/ false); const decl = findAncestor(token, isDeclarationWithType); - const jsdocType = getJSDocReturnType(decl) || getJSDocType(decl); + const jsdocType = getJSDocType(decl); if (!decl || !jsdocType || decl.type) { Debug.fail(`!decl || !jsdocType || decl.type: !${decl} || !${jsdocType} || ${decl.type}`); return undefined; } const changeTracker = textChanges.ChangeTracker.fromContext(context); - changeTracker.replaceRange(sourceFile, { pos: decl.getStart(), end: decl.end }, addType(decl, transformJSDocType(jsdocType) as TypeNode)); + const declarationWithType = addType(decl, transformJSDocType(jsdocType) as TypeNode); + suppressLeadingAndTrailingTrivia(declarationWithType); + changeTracker.replaceRange(sourceFile, { pos: decl.getStart(), end: decl.end }, declarationWithType); return { edits: changeTracker.getChanges(), renameFilename: undefined, @@ -87,7 +89,9 @@ namespace ts.refactor.annotateWithTypeFromJSDoc { const token = getTokenAtPosition(sourceFile, context.startPosition, /*includeJsDocComment*/ false); const decl = findAncestor(token, isFunctionLikeDeclaration); const changeTracker = textChanges.ChangeTracker.fromContext(context); - changeTracker.replaceRange(sourceFile, { pos: decl.getStart(), end: decl.end }, addTypesToFunctionLike(decl)); + const functionWithType = addTypesToFunctionLike(decl); + suppressLeadingAndTrailingTrivia(functionWithType); + changeTracker.replaceRange(sourceFile, { pos: decl.getStart(), end: decl.end }, functionWithType); return { edits: changeTracker.getChanges(), renameFilename: undefined, diff --git a/tests/cases/fourslash/annotateWithTypeFromJSDoc12.ts b/tests/cases/fourslash/annotateWithTypeFromJSDoc12.ts index c3c1aad5a90..e541bad4e4f 100644 --- a/tests/cases/fourslash/annotateWithTypeFromJSDoc12.ts +++ b/tests/cases/fourslash/annotateWithTypeFromJSDoc12.ts @@ -10,9 +10,6 @@ verify.applicableRefactorAvailableAtMarker('1'); verify.fileAfterApplyingRefactorAtMarker('1', `class C { - /** - * @return {...*} - */ /** * @return {...*} */ diff --git a/tests/cases/fourslash/annotateWithTypeFromJSDoc5.ts b/tests/cases/fourslash/annotateWithTypeFromJSDoc5.ts index 1f15bf59924..83e0888ddf5 100644 --- a/tests/cases/fourslash/annotateWithTypeFromJSDoc5.ts +++ b/tests/cases/fourslash/annotateWithTypeFromJSDoc5.ts @@ -9,7 +9,6 @@ verify.applicableRefactorAvailableAtMarker('1'); verify.fileAfterApplyingRefactorAtMarker('1', `class C { - /** @type {number | null} */ /** @type {number | null} */ p: number | null = null; }`, 'Annotate with type from JSDoc', 'annotate'); diff --git a/tests/cases/fourslash/annotateWithTypeFromJSDoc6.ts b/tests/cases/fourslash/annotateWithTypeFromJSDoc6.ts index 91bc1523ea3..f6fce6c449f 100644 --- a/tests/cases/fourslash/annotateWithTypeFromJSDoc6.ts +++ b/tests/cases/fourslash/annotateWithTypeFromJSDoc6.ts @@ -9,7 +9,6 @@ verify.applicableRefactorAvailableAtMarker('1'); verify.fileAfterApplyingRefactorAtMarker('1', `declare class C { - /** @type {number | null} */ /** @type {number | null} */ p: number | null; }`, 'Annotate with type from JSDoc', 'annotate');