fix(59397): JsDoc is missing/duplicated in declarations for overloads declared in classes declared in functions (#59675)

This commit is contained in:
Oleksandr T. 2024-10-23 21:34:23 +03:00 committed by GitHub
parent 6a90111d05
commit db8eacd7e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 156 additions and 5 deletions

View File

@ -7369,7 +7369,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const signatures = getSignaturesOfType(filterType(propertyType, t => !(t.flags & TypeFlags.Undefined)), SignatureKind.Call);
for (const signature of signatures) {
const methodDeclaration = signatureToSignatureDeclarationHelper(signature, SyntaxKind.MethodSignature, context, { name: propertyName, questionToken: optionalToken }) as MethodSignature;
typeElements.push(preserveCommentsOn(methodDeclaration));
typeElements.push(preserveCommentsOn(methodDeclaration, signature.declaration || propertySymbol.valueDeclaration));
}
if (signatures.length || !optionalToken) {
return;
@ -7401,9 +7401,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
propertyTypeNode,
);
typeElements.push(preserveCommentsOn(propertySignature));
typeElements.push(preserveCommentsOn(propertySignature, propertySymbol.valueDeclaration));
function preserveCommentsOn<T extends Node>(node: T) {
function preserveCommentsOn<T extends Node>(node: T, range: Node | undefined) {
const jsdocPropertyTag = propertySymbol.declarations?.find((d): d is JSDocPropertyTag => d.kind === SyntaxKind.JSDocPropertyTag);
if (jsdocPropertyTag) {
const commentText = getTextOfJSDocComment(jsdocPropertyTag.comment);
@ -7411,9 +7411,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
setSyntheticLeadingComments(node, [{ kind: SyntaxKind.MultiLineCommentTrivia, text: "*\n * " + commentText.replace(/\n/g, "\n * ") + "\n ", pos: -1, end: -1, hasTrailingNewLine: true }]);
}
}
else if (propertySymbol.valueDeclaration) {
else if (range) {
// Copy comments to node for declaration emit
setCommentRange(context, node, propertySymbol.valueDeclaration);
setCommentRange(context, node, range);
}
return node;
}

View File

@ -0,0 +1,45 @@
//// [tests/cases/compiler/signatureOverloadsWithComments.ts] ////
//// [signatureOverloadsWithComments.ts]
/**
* Docs
*/
function Foo() {
return class Bar {
/**
* comment 1
*/
foo(bar: string): void;
/**
* @deprecated This signature is deprecated
*
* comment 2
*/
foo(): string;
foo(bar?: string): string | void {
return 'hi'
}
}
}
//// [signatureOverloadsWithComments.d.ts]
/**
* Docs
*/
declare function Foo(): {
new (): {
/**
* comment 1
*/
foo(bar: string): void;
/**
* @deprecated This signature is deprecated
*
* comment 2
*/
foo(): string;
};
};

View File

@ -0,0 +1,36 @@
//// [tests/cases/compiler/signatureOverloadsWithComments.ts] ////
=== signatureOverloadsWithComments.ts ===
/**
* Docs
*/
function Foo() {
>Foo : Symbol(Foo, Decl(signatureOverloadsWithComments.ts, 0, 0))
return class Bar {
>Bar : Symbol(Bar, Decl(signatureOverloadsWithComments.ts, 4, 10))
/**
* comment 1
*/
foo(bar: string): void;
>foo : Symbol(Bar.foo, Decl(signatureOverloadsWithComments.ts, 4, 22), Decl(signatureOverloadsWithComments.ts, 8, 31), Decl(signatureOverloadsWithComments.ts, 14, 22))
>bar : Symbol(bar, Decl(signatureOverloadsWithComments.ts, 8, 12))
/**
* @deprecated This signature is deprecated
*
* comment 2
*/
foo(): string;
>foo : Symbol(Bar.foo, Decl(signatureOverloadsWithComments.ts, 4, 22), Decl(signatureOverloadsWithComments.ts, 8, 31), Decl(signatureOverloadsWithComments.ts, 14, 22))
foo(bar?: string): string | void {
>foo : Symbol(Bar.foo, Decl(signatureOverloadsWithComments.ts, 4, 22), Decl(signatureOverloadsWithComments.ts, 8, 31), Decl(signatureOverloadsWithComments.ts, 14, 22))
>bar : Symbol(bar, Decl(signatureOverloadsWithComments.ts, 15, 12))
return 'hi'
}
}
}

View File

@ -0,0 +1,47 @@
//// [tests/cases/compiler/signatureOverloadsWithComments.ts] ////
=== signatureOverloadsWithComments.ts ===
/**
* Docs
*/
function Foo() {
>Foo : () => typeof Bar
> : ^^^^^^^^^^^^^^^^
return class Bar {
>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
> : ^^^^^^^^^^
>Bar : typeof Bar
> : ^^^^^^^^^^
/**
* comment 1
*/
foo(bar: string): void;
>foo : { (bar: string): void; (): string; }
> : ^^^ ^^ ^^^ ^^^^^^ ^^^
>bar : string
> : ^^^^^^
/**
* @deprecated This signature is deprecated
*
* comment 2
*/
foo(): string;
>foo : { (bar: string): void; (): string; }
> : ^^^ ^^ ^^^ ^^^^^^ ^^^
foo(bar?: string): string | void {
>foo : { (bar: string): void; (): string; }
> : ^^^ ^^ ^^^ ^^^^^^ ^^^
>bar : string
> : ^^^^^^
return 'hi'
>'hi' : "hi"
> : ^^^^
}
}
}

View File

@ -0,0 +1,23 @@
// @declaration: true
// @emitDeclarationOnly: true
/**
* Docs
*/
function Foo() {
return class Bar {
/**
* comment 1
*/
foo(bar: string): void;
/**
* @deprecated This signature is deprecated
*
* comment 2
*/
foo(): string;
foo(bar?: string): string | void {
return 'hi'
}
}
}