Skip to content

Commit db8eacd

Browse files
authored
fix(59397): JsDoc is missing/duplicated in declarations for overloads declared in classes declared in functions (#59675)
1 parent 6a90111 commit db8eacd

File tree

5 files changed

+156
-5
lines changed

5 files changed

+156
-5
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7369,7 +7369,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
73697369
const signatures = getSignaturesOfType(filterType(propertyType, t => !(t.flags & TypeFlags.Undefined)), SignatureKind.Call);
73707370
for (const signature of signatures) {
73717371
const methodDeclaration = signatureToSignatureDeclarationHelper(signature, SyntaxKind.MethodSignature, context, { name: propertyName, questionToken: optionalToken }) as MethodSignature;
7372-
typeElements.push(preserveCommentsOn(methodDeclaration));
7372+
typeElements.push(preserveCommentsOn(methodDeclaration, signature.declaration || propertySymbol.valueDeclaration));
73737373
}
73747374
if (signatures.length || !optionalToken) {
73757375
return;
@@ -7401,19 +7401,19 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
74017401
propertyTypeNode,
74027402
);
74037403

7404-
typeElements.push(preserveCommentsOn(propertySignature));
7404+
typeElements.push(preserveCommentsOn(propertySignature, propertySymbol.valueDeclaration));
74057405

7406-
function preserveCommentsOn<T extends Node>(node: T) {
7406+
function preserveCommentsOn<T extends Node>(node: T, range: Node | undefined) {
74077407
const jsdocPropertyTag = propertySymbol.declarations?.find((d): d is JSDocPropertyTag => d.kind === SyntaxKind.JSDocPropertyTag);
74087408
if (jsdocPropertyTag) {
74097409
const commentText = getTextOfJSDocComment(jsdocPropertyTag.comment);
74107410
if (commentText) {
74117411
setSyntheticLeadingComments(node, [{ kind: SyntaxKind.MultiLineCommentTrivia, text: "*\n * " + commentText.replace(/\n/g, "\n * ") + "\n ", pos: -1, end: -1, hasTrailingNewLine: true }]);
74127412
}
74137413
}
7414-
else if (propertySymbol.valueDeclaration) {
7414+
else if (range) {
74157415
// Copy comments to node for declaration emit
7416-
setCommentRange(context, node, propertySymbol.valueDeclaration);
7416+
setCommentRange(context, node, range);
74177417
}
74187418
return node;
74197419
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//// [tests/cases/compiler/signatureOverloadsWithComments.ts] ////
2+
3+
//// [signatureOverloadsWithComments.ts]
4+
/**
5+
* Docs
6+
*/
7+
function Foo() {
8+
return class Bar {
9+
/**
10+
* comment 1
11+
*/
12+
foo(bar: string): void;
13+
/**
14+
* @deprecated This signature is deprecated
15+
*
16+
* comment 2
17+
*/
18+
foo(): string;
19+
foo(bar?: string): string | void {
20+
return 'hi'
21+
}
22+
}
23+
}
24+
25+
26+
27+
28+
//// [signatureOverloadsWithComments.d.ts]
29+
/**
30+
* Docs
31+
*/
32+
declare function Foo(): {
33+
new (): {
34+
/**
35+
* comment 1
36+
*/
37+
foo(bar: string): void;
38+
/**
39+
* @deprecated This signature is deprecated
40+
*
41+
* comment 2
42+
*/
43+
foo(): string;
44+
};
45+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//// [tests/cases/compiler/signatureOverloadsWithComments.ts] ////
2+
3+
=== signatureOverloadsWithComments.ts ===
4+
/**
5+
* Docs
6+
*/
7+
function Foo() {
8+
>Foo : Symbol(Foo, Decl(signatureOverloadsWithComments.ts, 0, 0))
9+
10+
return class Bar {
11+
>Bar : Symbol(Bar, Decl(signatureOverloadsWithComments.ts, 4, 10))
12+
13+
/**
14+
* comment 1
15+
*/
16+
foo(bar: string): void;
17+
>foo : Symbol(Bar.foo, Decl(signatureOverloadsWithComments.ts, 4, 22), Decl(signatureOverloadsWithComments.ts, 8, 31), Decl(signatureOverloadsWithComments.ts, 14, 22))
18+
>bar : Symbol(bar, Decl(signatureOverloadsWithComments.ts, 8, 12))
19+
20+
/**
21+
* @deprecated This signature is deprecated
22+
*
23+
* comment 2
24+
*/
25+
foo(): string;
26+
>foo : Symbol(Bar.foo, Decl(signatureOverloadsWithComments.ts, 4, 22), Decl(signatureOverloadsWithComments.ts, 8, 31), Decl(signatureOverloadsWithComments.ts, 14, 22))
27+
28+
foo(bar?: string): string | void {
29+
>foo : Symbol(Bar.foo, Decl(signatureOverloadsWithComments.ts, 4, 22), Decl(signatureOverloadsWithComments.ts, 8, 31), Decl(signatureOverloadsWithComments.ts, 14, 22))
30+
>bar : Symbol(bar, Decl(signatureOverloadsWithComments.ts, 15, 12))
31+
32+
return 'hi'
33+
}
34+
}
35+
}
36+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//// [tests/cases/compiler/signatureOverloadsWithComments.ts] ////
2+
3+
=== signatureOverloadsWithComments.ts ===
4+
/**
5+
* Docs
6+
*/
7+
function Foo() {
8+
>Foo : () => typeof Bar
9+
> : ^^^^^^^^^^^^^^^^
10+
11+
return class Bar {
12+
>class Bar { /** * comment 1 */ foo(bar: string): void; /** * @deprecated This signature is deprecated * * comment 2 */ foo(): string; foo(bar?: string): string | void { return 'hi' } } : typeof Bar
13+
> : ^^^^^^^^^^
14+
>Bar : typeof Bar
15+
> : ^^^^^^^^^^
16+
17+
/**
18+
* comment 1
19+
*/
20+
foo(bar: string): void;
21+
>foo : { (bar: string): void; (): string; }
22+
> : ^^^ ^^ ^^^ ^^^^^^ ^^^
23+
>bar : string
24+
> : ^^^^^^
25+
26+
/**
27+
* @deprecated This signature is deprecated
28+
*
29+
* comment 2
30+
*/
31+
foo(): string;
32+
>foo : { (bar: string): void; (): string; }
33+
> : ^^^ ^^ ^^^ ^^^^^^ ^^^
34+
35+
foo(bar?: string): string | void {
36+
>foo : { (bar: string): void; (): string; }
37+
> : ^^^ ^^ ^^^ ^^^^^^ ^^^
38+
>bar : string
39+
> : ^^^^^^
40+
41+
return 'hi'
42+
>'hi' : "hi"
43+
> : ^^^^
44+
}
45+
}
46+
}
47+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// @declaration: true
2+
// @emitDeclarationOnly: true
3+
4+
/**
5+
* Docs
6+
*/
7+
function Foo() {
8+
return class Bar {
9+
/**
10+
* comment 1
11+
*/
12+
foo(bar: string): void;
13+
/**
14+
* @deprecated This signature is deprecated
15+
*
16+
* comment 2
17+
*/
18+
foo(): string;
19+
foo(bar?: string): string | void {
20+
return 'hi'
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)