Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7368,7 +7368,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;
Expand Down Expand Up @@ -7400,19 +7400,19 @@ 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);
if (commentText) {
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;
}
Expand Down
45 changes: 45 additions & 0 deletions tests/baselines/reference/signatureOverloadsWithComments.js
Original file line number Diff line number Diff line change
@@ -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;
};
};
36 changes: 36 additions & 0 deletions tests/baselines/reference/signatureOverloadsWithComments.symbols
Original file line number Diff line number Diff line change
@@ -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'
}
}
}

47 changes: 47 additions & 0 deletions tests/baselines/reference/signatureOverloadsWithComments.types
Original file line number Diff line number Diff line change
@@ -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"
> : ^^^^
}
}
}

23 changes: 23 additions & 0 deletions tests/cases/compiler/signatureOverloadsWithComments.ts
Original file line number Diff line number Diff line change
@@ -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'
}
}
}