Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed @template tags used with @overloads #59983

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,7 @@ export type HasLocals =
| JSDocFunctionType
| JSDocSignature
| JSDocTypedefTag
| JSDocOverloadTag
| MappedTypeNode
| MethodDeclaration
| MethodSignature
Expand Down Expand Up @@ -4063,7 +4064,7 @@ export interface JSDocCallbackTag extends JSDocTag, NamedDeclaration, LocalsCont
readonly typeExpression: JSDocSignature;
}

export interface JSDocOverloadTag extends JSDocTag {
export interface JSDocOverloadTag extends JSDocTag, LocalsContainer {
readonly kind: SyntaxKind.JSDocOverloadTag;
readonly parent: JSDoc;
readonly typeExpression: JSDocSignature;
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4595,10 +4595,10 @@ export function getParameterSymbolFromJSDoc(node: JSDocParameterTag): Symbol | u
}

/** @internal */
export function getEffectiveContainerForJSDocTemplateTag(node: JSDocTemplateTag): SignatureDeclaration | JSDocTypedefTag | JSDocCallbackTag | JSDocEnumTag | undefined {
export function getEffectiveContainerForJSDocTemplateTag(node: JSDocTemplateTag): SignatureDeclaration | JSDocTypedefTag | JSDocCallbackTag | JSDocOverloadTag | JSDocEnumTag | undefined {
if (isJSDoc(node.parent) && node.parent.tags) {
// A @template tag belongs to any @typedef, @callback, or @enum tags in the same comment block, if they exist.
const typeAlias = find(node.parent.tags, isJSDocTypeAlias);
// A @template tag belongs to any @overload, @typedef, @callback, or @enum tags in the same comment block, if they exist.
const typeAlias = find(node.parent.tags, tag => isJSDocOverloadTag(tag) || isJSDocTypeAlias(tag));
if (typeAlias) {
return typeAlias;
}
Expand Down
1 change: 1 addition & 0 deletions src/compiler/utilitiesPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2288,6 +2288,7 @@ export function canHaveLocals(node: Node): node is HasLocals {
case SyntaxKind.JSDocFunctionType:
case SyntaxKind.JSDocSignature:
case SyntaxKind.JSDocTypedefTag:
case SyntaxKind.JSDocOverloadTag:
case SyntaxKind.MappedType:
case SyntaxKind.MethodDeclaration:
case SyntaxKind.MethodSignature:
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5782,7 +5782,7 @@ declare namespace ts {
readonly name?: Identifier;
readonly typeExpression: JSDocSignature;
}
interface JSDocOverloadTag extends JSDocTag {
interface JSDocOverloadTag extends JSDocTag, LocalsContainer {
readonly kind: SyntaxKind.JSDocOverloadTag;
readonly parent: JSDoc;
readonly typeExpression: JSDocSignature;
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/jsFileFunctionOverloads.types
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ function flatMap(array, iterable = identity) {
> : ^^^^^^^^^
>iterable : (x: unknown) => unknown
> : ^ ^^ ^^^^^
>identity : <T_1>(x: T_1) => T_1
> : ^^^^^^ ^^ ^^^^^
>identity : <T>(x: T) => T
> : ^ ^^ ^^ ^^^^^

/** @type {unknown[]} */
const result = [];
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/jsFileFunctionOverloads2.types
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ function flatMap(array, iterable = identity) {
> : ^^^^^^^^^
>iterable : (x: unknown) => unknown
> : ^ ^^ ^^^^^
>identity : <T_1>(x: T_1) => T_1
> : ^^^^^^ ^^ ^^^^^
>identity : <T>(x: T) => T
> : ^ ^^ ^^ ^^^^^

/** @type {unknown[]} */
const result = [];
Expand Down
54 changes: 54 additions & 0 deletions tests/baselines/reference/jsdocTemplateTagOverload1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//// [tests/cases/conformance/jsdoc/jsdocTemplateTagOverload1.ts] ////

//// [index.js]
/**
* @template {string} Element
* @overload
* @param {Element} element
* @returns {() => void}
*/

/**
* @template {boolean} Element
* @overload
* @param {Element} element
* @returns {() => void}
*/

/**
* @overload
* @param {number} element
* @returns {() => void}
*/

/**
* @param {any} element
*/
export function on(element) {
return () => {}
}




//// [index.d.ts]
/**
* @template {string} Element
* @overload
* @param {Element} element
* @returns {() => void}
*/
export function on<Element extends string>(element: Element): () => void;
/**
* @template {boolean} Element
* @overload
* @param {Element} element
* @returns {() => void}
*/
export function on<Element extends boolean>(element: Element): () => void;
/**
* @overload
* @param {number} element
* @returns {() => void}
*/
export function on(element: number): () => void;
33 changes: 33 additions & 0 deletions tests/baselines/reference/jsdocTemplateTagOverload1.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//// [tests/cases/conformance/jsdoc/jsdocTemplateTagOverload1.ts] ////

=== index.js ===
/**
* @template {string} Element
* @overload
* @param {Element} element
* @returns {() => void}
*/

/**
* @template {boolean} Element
* @overload
* @param {Element} element
* @returns {() => void}
*/

/**
* @overload
* @param {number} element
* @returns {() => void}
*/

/**
* @param {any} element
*/
export function on(element) {
>on : Symbol(on, Decl(index.js, 0, 0))
>element : Symbol(element, Decl(index.js, 23, 19))

return () => {}
}

36 changes: 36 additions & 0 deletions tests/baselines/reference/jsdocTemplateTagOverload1.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//// [tests/cases/conformance/jsdoc/jsdocTemplateTagOverload1.ts] ////

=== index.js ===
/**
* @template {string} Element
* @overload
* @param {Element} element
* @returns {() => void}
*/

/**
* @template {boolean} Element
* @overload
* @param {Element} element
* @returns {() => void}
*/

/**
* @overload
* @param {number} element
* @returns {() => void}
*/

/**
* @param {any} element
*/
export function on(element) {
>on : { <Element extends string>(element: Element): () => void; <Element extends boolean>(element: Element): () => void; (element: number): () => void; }
> : ^^^ ^^^^^^^^^ ^^ ^^ ^^^ ^^^ ^^^^^^^^^ ^^ ^^ ^^^ ^^^ ^^ ^^^ ^^^
>element : any

return () => {}
>() => {} : () => void
> : ^^^^^^^^^^
}

9 changes: 6 additions & 3 deletions tests/baselines/reference/templateInsideCallback.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error TS-1: Pre-emit (11) and post-emit (13) diagnostic counts do not match! This can indicate that a semantic _error_ was added by the emit resolver - such an error may not be reflected on the command line or in the editor, but may be captured in a baseline here!
error TS-1: Pre-emit (12) and post-emit (14) diagnostic counts do not match! This can indicate that a semantic _error_ was added by the emit resolver - such an error may not be reflected on the command line or in the editor, but may be captured in a baseline here!
templateInsideCallback.js(2,13): error TS8021: JSDoc '@typedef' tag should either have a type annotation or be followed by '@property' or '@member' tags.
templateInsideCallback.js(9,5): error TS8039: A JSDoc '@template' tag may not follow a '@typedef', '@callback', or '@overload' tag
templateInsideCallback.js(10,12): error TS2304: Cannot find name 'T'.
Expand All @@ -8,15 +8,16 @@ templateInsideCallback.js(23,5): error TS8039: A JSDoc '@template' tag may not f
templateInsideCallback.js(30,5): error TS8039: A JSDoc '@template' tag may not follow a '@typedef', '@callback', or '@overload' tag
templateInsideCallback.js(32,12): error TS2304: Cannot find name 'T'.
templateInsideCallback.js(33,16): error TS2304: Cannot find name 'T'.
templateInsideCallback.js(33,22): error TS2304: Cannot find name 'U'.
templateInsideCallback.js(38,5): error TS8039: A JSDoc '@template' tag may not follow a '@typedef', '@callback', or '@overload' tag
templateInsideCallback.js(39,12): error TS2304: Cannot find name 'T'.


!!! error TS-1: Pre-emit (11) and post-emit (13) diagnostic counts do not match! This can indicate that a semantic _error_ was added by the emit resolver - such an error may not be reflected on the command line or in the editor, but may be captured in a baseline here!
!!! error TS-1: Pre-emit (12) and post-emit (14) diagnostic counts do not match! This can indicate that a semantic _error_ was added by the emit resolver - such an error may not be reflected on the command line or in the editor, but may be captured in a baseline here!
!!! related TS-1: The excess diagnostics are:
!!! related TS7012 templateInsideCallback.js:29:5: This overload implicitly returns the type 'any' because it lacks a return type annotation.
!!! related TS7012 templateInsideCallback.js:37:5: This overload implicitly returns the type 'any' because it lacks a return type annotation.
==== templateInsideCallback.js (11 errors) ====
==== templateInsideCallback.js (12 errors) ====
/**
* @typedef Oops
~~~~
Expand Down Expand Up @@ -68,6 +69,8 @@ templateInsideCallback.js(39,12): error TS2304: Cannot find name 'T'.
* @param {(x: T) => U[]} iterable
~
!!! error TS2304: Cannot find name 'T'.
~
!!! error TS2304: Cannot find name 'U'.
* @returns {U[]}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised by this addition but since T can't be found here (and there is a @template for it) so it seems rather fine that U became an error now here too

*/
/**
Expand Down
35 changes: 35 additions & 0 deletions tests/cases/conformance/jsdoc/jsdocTemplateTagOverload1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// @strict: true
// @allowJs: true
// @checkJs: true
// @declaration: true
// @emitDeclarationOnly: true
// @outDir: dist

// @filename: index.js

/**
* @template {string} Element
* @overload
* @param {Element} element
* @returns {() => void}
*/

/**
* @template {boolean} Element
* @overload
* @param {Element} element
* @returns {() => void}
*/

/**
* @overload
* @param {number} element
* @returns {() => void}
*/

/**
* @param {any} element
*/
export function on(element) {
return () => {}
}
Loading