Skip to content

Commit f35764d

Browse files
committed
Fix duplicated JSDoc comments
Incorporate suppressLeadingAndTrailingTrivia just added by @amcasey.
1 parent c2c18a8 commit f35764d

File tree

4 files changed

+7
-8
lines changed

4 files changed

+7
-8
lines changed

src/services/refactors/annotateWithTypeFromJSDoc.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@ namespace ts.refactor.annotateWithTypeFromJSDoc {
6262
const sourceFile = context.file;
6363
const token = getTokenAtPosition(sourceFile, context.startPosition, /*includeJsDocComment*/ false);
6464
const decl = findAncestor(token, isDeclarationWithType);
65-
const jsdocType = getJSDocReturnType(decl) || getJSDocType(decl);
65+
const jsdocType = getJSDocType(decl);
6666
if (!decl || !jsdocType || decl.type) {
6767
Debug.fail(`!decl || !jsdocType || decl.type: !${decl} || !${jsdocType} || ${decl.type}`);
6868
return undefined;
6969
}
7070

7171
const changeTracker = textChanges.ChangeTracker.fromContext(context);
72-
changeTracker.replaceRange(sourceFile, { pos: decl.getStart(), end: decl.end }, addType(decl, transformJSDocType(jsdocType) as TypeNode));
72+
const declarationWithType = addType(decl, transformJSDocType(jsdocType) as TypeNode);
73+
suppressLeadingAndTrailingTrivia(declarationWithType);
74+
changeTracker.replaceRange(sourceFile, { pos: decl.getStart(), end: decl.end }, declarationWithType);
7375
return {
7476
edits: changeTracker.getChanges(),
7577
renameFilename: undefined,
@@ -87,7 +89,9 @@ namespace ts.refactor.annotateWithTypeFromJSDoc {
8789
const token = getTokenAtPosition(sourceFile, context.startPosition, /*includeJsDocComment*/ false);
8890
const decl = findAncestor(token, isFunctionLikeDeclaration);
8991
const changeTracker = textChanges.ChangeTracker.fromContext(context);
90-
changeTracker.replaceRange(sourceFile, { pos: decl.getStart(), end: decl.end }, addTypesToFunctionLike(decl));
92+
const functionWithType = addTypesToFunctionLike(decl);
93+
suppressLeadingAndTrailingTrivia(functionWithType);
94+
changeTracker.replaceRange(sourceFile, { pos: decl.getStart(), end: decl.end }, functionWithType);
9195
return {
9296
edits: changeTracker.getChanges(),
9397
renameFilename: undefined,

tests/cases/fourslash/annotateWithTypeFromJSDoc12.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
verify.applicableRefactorAvailableAtMarker('1');
1111
verify.fileAfterApplyingRefactorAtMarker('1',
1212
`class C {
13-
/**
14-
* @return {...*}
15-
*/
1613
/**
1714
* @return {...*}
1815
*/

tests/cases/fourslash/annotateWithTypeFromJSDoc5.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
verify.applicableRefactorAvailableAtMarker('1');
1010
verify.fileAfterApplyingRefactorAtMarker('1',
1111
`class C {
12-
/** @type {number | null} */
1312
/** @type {number | null} */
1413
p: number | null = null;
1514
}`, 'Annotate with type from JSDoc', 'annotate');

tests/cases/fourslash/annotateWithTypeFromJSDoc6.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
verify.applicableRefactorAvailableAtMarker('1');
1010
verify.fileAfterApplyingRefactorAtMarker('1',
1111
`declare class C {
12-
/** @type {number | null} */
1312
/** @type {number | null} */
1413
p: number | null;
1514
}`, 'Annotate with type from JSDoc', 'annotate');

0 commit comments

Comments
 (0)