From efdba54deb3de6a1864a079c5bb623dde6736645 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Fri, 14 Jul 2017 16:27:36 -0700 Subject: [PATCH] Add codefix for jsdoc types in Typescript It only handles a few simple types right now, but the skeleton is there. --- .../codefixes/disableJsDiagnostics.ts | 4 +- src/services/codefixes/fixJSDocTypes.ts | 52 +++++++++++++++++++ src/services/codefixes/fixes.ts | 1 + 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/services/codefixes/fixJSDocTypes.ts diff --git a/src/services/codefixes/disableJsDiagnostics.ts b/src/services/codefixes/disableJsDiagnostics.ts index b5f9e5587a3..291dc61c32f 100644 --- a/src/services/codefixes/disableJsDiagnostics.ts +++ b/src/services/codefixes/disableJsDiagnostics.ts @@ -32,7 +32,7 @@ namespace ts.codefix { } } - // If all fails, add an extra new line immediatlly before the error span. + // If all fails, add an extra new line immediately before the error span. return { span: { start: position, length: 0 }, newText: `${position === startPosition ? "" : newLineCharacter}// @ts-ignore${newLineCharacter}` @@ -67,4 +67,4 @@ namespace ts.codefix { }] }]; } -} \ No newline at end of file +} diff --git a/src/services/codefixes/fixJSDocTypes.ts b/src/services/codefixes/fixJSDocTypes.ts new file mode 100644 index 00000000000..db70985dd51 --- /dev/null +++ b/src/services/codefixes/fixJSDocTypes.ts @@ -0,0 +1,52 @@ +/* @internal */ +namespace ts.codefix { + registerCodeFix({ + errorCodes: [Diagnostics.JSDoc_types_can_only_be_used_inside_documentation_comments.code], + getCodeActions: getActionsForJSDocTypes + }); + + function getActionsForJSDocTypes(context: CodeFixContext): CodeAction[] | undefined { + const sourceFile = context.sourceFile; + + const node = getTokenAtPosition(sourceFile, context.span.start, /*includeJsDocComment*/ false); + if (node.kind !== SyntaxKind.VariableDeclaration) return; + + const type = (node as VariableDeclaration).type; + if (containsJSDocType(type)) { + const tsType = getTypeFromJSDocType(type); + return [{ + description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Change_0_to_1), [getTextOfNode(type), tsType]), + changes: [{ + fileName: sourceFile.fileName, + textChanges: [{ + span: { start: type.getStart(), length: type.getWidth() }, + newText: tsType + }], + }], + }]; + } + } + + function containsJSDocType(type: TypeNode): boolean { + switch (type.kind) { + case SyntaxKind.JSDocUnknownType: + case SyntaxKind.JSDocAllType: + case SyntaxKind.JSDocVariadicType: + return true; + // TODO: Of course you can put JSDoc types inside normal types, like number?[] and so on + } + } + + function getTypeFromJSDocType(type: TypeNode): string { + switch (type.kind) { + case SyntaxKind.JSDocUnknownType: + case SyntaxKind.JSDocAllType: + return "any"; + case SyntaxKind.JSDocVariadicType: + // this will surely work! + return getTypeFromJSDocType((type as JSDocVariadicType).type) + "[]"; + // TODO: Of course you can put JSDoc types inside normal types, like number?[] and so on + } + return getTextOfNode(type); + } +} diff --git a/src/services/codefixes/fixes.ts b/src/services/codefixes/fixes.ts index c38820231b0..ef670c81ba9 100644 --- a/src/services/codefixes/fixes.ts +++ b/src/services/codefixes/fixes.ts @@ -7,6 +7,7 @@ /// /// /// +/// /// /// ///