Skip to content

Commit 823f0f0

Browse files
committed
Fix duplicate JSDoc @typedef and @callback comments in declaration emit (#62453)
1 parent f5ccf43 commit 823f0f0

23 files changed

+15
-190
lines changed

src/compiler/emitter.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5992,15 +5992,25 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
59925992
}
59935993
}
59945994

5995-
function shouldWriteComment(text: string, pos: number) {
5995+
function shouldWriteComment(text: string, pos: number, end?: number) {
59965996
if (printerOptions.onlyPrintJsDocStyle) {
5997-
return (isJSDocLikeText(text, pos) || isPinnedComment(text, pos));
5997+
if (!isJSDocLikeText(text, pos) && !isPinnedComment(text, pos)) {
5998+
return false;
5999+
}
6000+
// Skip @typedef and @callback comments - they're converted to type declarations
6001+
if (end !== undefined) {
6002+
const commentText = text.slice(pos, end);
6003+
if (commentText.includes("@typedef") || commentText.includes("@callback")) {
6004+
return false;
6005+
}
6006+
}
6007+
return true;
59986008
}
59996009
return true;
60006010
}
60016011

60026012
function emitLeadingComment(commentPos: number, commentEnd: number, kind: SyntaxKind, hasTrailingNewLine: boolean, rangePos: number) {
6003-
if (!currentSourceFile || !shouldWriteComment(currentSourceFile.text, commentPos)) return;
6013+
if (!currentSourceFile || !shouldWriteComment(currentSourceFile.text, commentPos, commentEnd)) return;
60046014
if (!hasWrittenComment) {
60056015
emitNewLineBeforeLeadingCommentOfPosition(getCurrentLineMap(), writer, rangePos, commentPos);
60066016
hasWrittenComment = true;
@@ -6032,7 +6042,7 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
60326042
}
60336043

60346044
function emitTrailingComment(commentPos: number, commentEnd: number, _kind: SyntaxKind, hasTrailingNewLine: boolean) {
6035-
if (!currentSourceFile || !shouldWriteComment(currentSourceFile.text, commentPos)) return;
6045+
if (!currentSourceFile || !shouldWriteComment(currentSourceFile.text, commentPos, commentEnd)) return;
60366046
// trailing comments are emitted at space/*trailing comment1 */space/*trailing comment2*/
60376047
if (!writer.isAtStartOfLine()) {
60386048
writer.writeSpace(" ");
@@ -6135,7 +6145,7 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
61356145
}
61366146

61376147
function emitComment(text: string, lineMap: readonly number[], writer: EmitTextWriter, commentPos: number, commentEnd: number, newLine: string) {
6138-
if (!currentSourceFile || !shouldWriteComment(currentSourceFile.text, commentPos)) return;
6148+
if (!currentSourceFile || !shouldWriteComment(currentSourceFile.text, commentPos, commentEnd)) return;
61396149
emitPos(commentPos);
61406150
writeCommentRange(text, lineMap, writer, commentPos, commentEnd, newLine);
61416151
emitPos(commentEnd);

tests/baselines/reference/callbackTagNestedParameter.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,6 @@ function eachPerson(callback) {
2222

2323

2424
//// [cb_nested.d.ts]
25-
/**
26-
* @callback WorksWithPeopleCallback
27-
* @param {Object} person
28-
* @param {string} person.name
29-
* @param {number} [person.age]
30-
* @returns {void}
31-
*/
3225
/**
3326
* For each person, calls your callback.
3427
* @param {WorksWithPeopleCallback} callback

tests/baselines/reference/callbackTagVariadicType.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ var res = (0, exports.x)('a', 'b');
2828

2929

3030
//// [callbackTagVariadicType.d.ts]
31-
/**
32-
* @callback Foo
33-
* @param {...string} args
34-
* @returns {number}
35-
*/
3631
/** @type {Foo} */
3732
export const x: Foo;
3833
export type Foo = (...args: string[]) => number;

tests/baselines/reference/checkJsdocSatisfiesTag15.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ function fn7(uuid) { }
9696
/** @satisfies {(uuid: string) => void} */
9797
export function fn7(uuid: any): void;
9898
export function fn1(uuid: string): void;
99-
/** @typedef {Parameters<typeof fn1>} Foo */
10099
/** @type Foo */
101100
export const v1: Foo;
102101
/** @type Foo */

tests/baselines/reference/jsDeclarationEmitDoesNotRenameImport.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ export default X;
4848
export type Options = {
4949
test?: typeof import("./Test.js").default | undefined;
5050
};
51-
/**
52-
* @typedef {Object} Options
53-
* @property {typeof import("./Test.js").default} [test]
54-
*/
5551
declare class X extends Test {
5652
/**
5753
* @param {Options} options

tests/baselines/reference/jsDeclarationsFunctionClassesCjsExportAssignment.js

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -153,27 +153,6 @@ declare class Timer {
153153
}
154154
//// [context.d.ts]
155155
export = Context;
156-
/**
157-
* Imports
158-
*
159-
* @typedef {import("./timer")} Timer
160-
* @typedef {import("./hook")} Hook
161-
* @typedef {import("./hook").HookHandler} HookHandler
162-
*/
163-
/**
164-
* Input type definition
165-
*
166-
* @typedef {Object} Input
167-
* @prop {Timer} timer
168-
* @prop {Hook} hook
169-
*/
170-
/**
171-
* State type definition
172-
*
173-
* @typedef {Object} State
174-
* @prop {Timer} timer
175-
* @prop {Hook} hook
176-
*/
177156
/**
178157
* New `Context`
179158
*
@@ -182,27 +161,6 @@ export = Context;
182161
*/
183162
declare function Context(input: Input): Context;
184163
declare class Context {
185-
/**
186-
* Imports
187-
*
188-
* @typedef {import("./timer")} Timer
189-
* @typedef {import("./hook")} Hook
190-
* @typedef {import("./hook").HookHandler} HookHandler
191-
*/
192-
/**
193-
* Input type definition
194-
*
195-
* @typedef {Object} Input
196-
* @prop {Timer} timer
197-
* @prop {Hook} hook
198-
*/
199-
/**
200-
* State type definition
201-
*
202-
* @typedef {Object} State
203-
* @prop {Timer} timer
204-
* @prop {Hook} hook
205-
*/
206164
/**
207165
* New `Context`
208166
*
@@ -249,17 +207,11 @@ type State = {
249207
};
250208
//// [hook.d.ts]
251209
export = Hook;
252-
/**
253-
* @typedef {(arg: import("./context")) => void} HookHandler
254-
*/
255210
/**
256211
* @param {HookHandler} handle
257212
*/
258213
declare function Hook(handle: HookHandler): void;
259214
declare class Hook {
260-
/**
261-
* @typedef {(arg: import("./context")) => void} HookHandler
262-
*/
263215
/**
264216
* @param {HookHandler} handle
265217
*/

tests/baselines/reference/jsDeclarationsImportAliasExposedWithinNamespace.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ export const myTypes: {
8080
export namespace testFnTypes {
8181
type input = boolean | myTypes.typeC;
8282
}
83-
/** @typedef {boolean|myTypes.typeC} testFnTypes.input */
8483
/**
8584
* @function testFn
8685
* @description A test function.

tests/baselines/reference/jsDeclarationsImportAliasExposedWithinNamespaceCjs.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ export namespace myTypes {
7777
type typeC = myTypes.typeB | Function;
7878
}
7979
//// [file2.d.ts]
80-
/** @typedef {boolean|myTypes.typeC} testFnTypes.input */
8180
/**
8281
* @function testFn
8382
* @description A test function.

tests/baselines/reference/jsDeclarationsImportNamespacedType.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export var dummy = 1
1313

1414

1515
//// [mod1.d.ts]
16-
/** @typedef {number} Dotted.Name */
1716
export const dummy: number;
1817
export namespace Dotted {
1918
type Name = number;

tests/baselines/reference/jsDeclarationsImportTypeBundled.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ module.exports = items;
3131
//// [out.d.ts]
3232
declare module "folder/mod1" {
3333
export = x;
34-
/**
35-
* @typedef {{x: number}} Item
36-
*/
3734
/**
3835
* @type {Item};
3936
*/

0 commit comments

Comments
 (0)