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

Fix getTypeFromJSDocVariadicType in callback tag #43562

Merged
merged 3 commits into from
Apr 7, 2021
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
7 changes: 5 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38404,15 +38404,18 @@ namespace ts {
if (isJSDocTypeExpression(node.parent) && isJSDocParameterTag(paramTag)) {
// Else we will add a diagnostic, see `checkJSDocVariadicType`.
const host = getHostSignatureFromJSDoc(paramTag);
if (host) {
const isCallbackTag = isJSDocCallbackTag(paramTag.parent.parent);
if (host || isCallbackTag) {
/*
Only return an array type if the corresponding parameter is marked as a rest parameter, or if there are no parameters.
So in the following situation we will not create an array type:
/** @param {...number} a * /
function f(a) {}
Because `a` will just be of type `number | undefined`. A synthetic `...args` will also be added, which *will* get an array type.
*/
const lastParamDeclaration = lastOrUndefined(host.parameters);
const lastParamDeclaration = isCallbackTag
? lastOrUndefined((paramTag.parent.parent as unknown as JSDocCallbackTag).typeExpression.parameters)
: lastOrUndefined(host!.parameters);
const symbol = getParameterSymbolFromJSDoc(paramTag);
if (!lastParamDeclaration ||
symbol && lastParamDeclaration.symbol === symbol && isRestParameter(lastParamDeclaration)) {
Expand Down
36 changes: 36 additions & 0 deletions tests/baselines/reference/callbackTagVariadicType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//// [callbackTagVariadicType.js]
/**
* @callback Foo
* @param {...string} args
* @returns {number}
*/

/** @type {Foo} */
export const x = () => 1
var res = x('a', 'b')


//// [callbackTagVariadicType.js]
"use strict";
/**
* @callback Foo
* @param {...string} args
* @returns {number}
*/
exports.__esModule = true;
exports.x = void 0;
/** @type {Foo} */
var x = function () { return 1; };
exports.x = x;
var res = (0, exports.x)('a', 'b');


//// [callbackTagVariadicType.d.ts]
/**
* @callback Foo
* @param {...string} args
* @returns {number}
*/
/** @type {Foo} */
export const x: Foo;
export type Foo = (...args: string[]) => number;
15 changes: 15 additions & 0 deletions tests/baselines/reference/callbackTagVariadicType.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
=== tests/cases/conformance/jsdoc/callbackTagVariadicType.js ===
/**
* @callback Foo
* @param {...string} args
* @returns {number}
*/

/** @type {Foo} */
export const x = () => 1
>x : Symbol(x, Decl(callbackTagVariadicType.js, 7, 12))

var res = x('a', 'b')
>res : Symbol(res, Decl(callbackTagVariadicType.js, 8, 3))
>x : Symbol(x, Decl(callbackTagVariadicType.js, 7, 12))

20 changes: 20 additions & 0 deletions tests/baselines/reference/callbackTagVariadicType.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=== tests/cases/conformance/jsdoc/callbackTagVariadicType.js ===
/**
* @callback Foo
* @param {...string} args
* @returns {number}
*/

/** @type {Foo} */
export const x = () => 1
>x : Foo
>() => 1 : () => number
>1 : 1

var res = x('a', 'b')
>res : number
>x('a', 'b') : number
>x : Foo
>'a' : "a"
>'b' : "b"

14 changes: 14 additions & 0 deletions tests/cases/conformance/jsdoc/callbackTagVariadicType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @declaration: true
// @outDir: bin/
// @checkJs: true
// @Filename: callbackTagVariadicType.js

/**
* @callback Foo
* @param {...string} args
* @returns {number}
*/

/** @type {Foo} */
export const x = () => 1
var res = x('a', 'b')