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

Improve behaviour of ... inside JSDoc functions #22809

Merged
merged 3 commits into from
Mar 22, 2018
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
12 changes: 11 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24495,8 +24495,15 @@ namespace ts {
checkJSDocTypeIsInJsFile(node);
checkSourceElement(node.type);

// Only legal location is in the *last* parameter tag.
// Only legal location is in the *last* parameter tag or last parameter of a JSDoc function.
const { parent } = node;
if (isParameter(parent) && isJSDocFunctionType(parent.parent)) {
if (last(parent.parent.parameters) !== parent) {
error(node, Diagnostics.A_rest_parameter_must_be_last_in_a_parameter_list);
}
return;
}

if (!isJSDocTypeExpression(parent)) {
error(node, Diagnostics.JSDoc_may_only_appear_in_the_last_parameter_of_a_signature);
}
Expand Down Expand Up @@ -24542,6 +24549,9 @@ namespace ts {
}
}
}
if (isParameter(parent) && isJSDocFunctionType(parent.parent)) {
return createArrayType(type);
}
return addOptionality(type);
}

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1861,7 +1861,7 @@ namespace ts {
}

export function isRestParameter(node: ParameterDeclaration): boolean {
return node.dotDotDotToken !== undefined;
return node.dotDotDotToken !== undefined || node.type && node.type.kind === SyntaxKind.JSDocVariadicType;
}

export const enum AssignmentKind {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
=== tests/cases/conformance/jsdoc/a.js ===
// from bcryptjs
/** @param {function(...[*])} callback */
function g(callback) {
>g : Symbol(g, Decl(a.js, 0, 0))
>callback : Symbol(callback, Decl(a.js, 2, 11))

callback([1], [2], [3])
>callback : Symbol(callback, Decl(a.js, 2, 11))
}

/**
* @type {!function(...number):string}
* @inner
*/
var stringFromCharCode = String.fromCharCode;
>stringFromCharCode : Symbol(stringFromCharCode, Decl(a.js, 10, 3))
>String.fromCharCode : Symbol(StringConstructor.fromCharCode, Decl(lib.d.ts, --, --))
>String : Symbol(String, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>fromCharCode : Symbol(StringConstructor.fromCharCode, Decl(lib.d.ts, --, --))

28 changes: 28 additions & 0 deletions tests/baselines/reference/jsdocParseDotDotDotInJSDocFunction.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
=== tests/cases/conformance/jsdoc/a.js ===
// from bcryptjs
/** @param {function(...[*])} callback */
function g(callback) {
>g : (callback: (...arg0: [any][]) => any) => void
Copy link

Choose a reason for hiding this comment

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

Was the intention to have a single-element tuple type in an array?

Copy link
Member

Choose a reason for hiding this comment

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

Looks like it, given the implementation.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, this came from some real (but quite confused) jsdoc in bcryptjs. I think the author's intent was any[][] but they wrote ...[*]

>callback : (...arg0: [any][]) => any

callback([1], [2], [3])
>callback([1], [2], [3]) : any
>callback : (...arg0: [any][]) => any
>[1] : [number]
>1 : 1
>[2] : [number]
>2 : 2
>[3] : [number]
>3 : 3
}

/**
* @type {!function(...number):string}
* @inner
*/
var stringFromCharCode = String.fromCharCode;
>stringFromCharCode : (...arg0: number[]) => string
>String.fromCharCode : (...codes: number[]) => string
>String : StringConstructor
>fromCharCode : (...codes: number[]) => string

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// @noEmit: true
// @allowJs: true
// @checkJs: true
// @strict: true
// @Filename: a.js

// from bcryptjs
/** @param {function(...[*])} callback */
function g(callback) {
callback([1], [2], [3])
}

/**
* @type {!function(...number):string}
* @inner
*/
var stringFromCharCode = String.fromCharCode;