Correctly transform jsdoc parameter types

And give a better name for rest params
This commit is contained in:
Nathan Shively-Sanders 2017-09-28 11:40:56 -07:00
parent 724a813105
commit d797b4ab76
3 changed files with 16 additions and 4 deletions

View File

@ -578,10 +578,10 @@ namespace ts {
return emitLiteralType(<LiteralTypeNode>node);
case SyntaxKind.JSDocAllType:
write("*");
break;
return;
case SyntaxKind.JSDocUnknownType:
write("?");
break;
return;
case SyntaxKind.JSDocNullableType:
return emitJSDocNullableType(node as JSDocNullableType);
case SyntaxKind.JSDocNonNullableType:

View File

@ -189,8 +189,11 @@ namespace ts.refactor.annotateWithTypeFromJSDoc {
}
function visitJSDocParameter(node: ParameterDeclaration) {
const name = node.name || "arg" + node.parent.parameters.indexOf(node);
return createParameter(node.decorators, node.modifiers, node.dotDotDotToken, name, node.questionToken, node.type, node.initializer);
const index = node.parent.parameters.indexOf(node);
const isRest = node.type.kind === SyntaxKind.JSDocVariadicType && index === node.parent.parameters.length - 1;
const name = node.name || (isRest ? "rest" : "arg" + index);
const dotdotdot = isRest ? createToken(SyntaxKind.DotDotDotToken) : node.dotDotDotToken;
return createParameter(node.decorators, node.modifiers, dotdotdot, name, node.questionToken, visitNode(node.type, transformJSDocType), node.initializer);
}
function visitJSDocTypeReference(node: TypeReferenceNode) {

View File

@ -0,0 +1,9 @@
/// <reference path='fourslash.ts' />
// @strict: true
/////** @type {function(*, ...number, ...boolean): void} */
////var /*1*/x;
verify.applicableRefactorAvailableAtMarker('1');
verify.fileAfterApplyingRefactorAtMarker('1',
`/** @type {function(*, ...number, ...boolean): void} */
var x: (arg0: any, arg1: number[], ...rest: boolean[]) => void;`, 'Annotate with type from JSDoc', 'annotate');