Skip to content

Commit efdba54

Browse files
committed
Add codefix for jsdoc types in Typescript
It only handles a few simple types right now, but the skeleton is there.
1 parent a3a6862 commit efdba54

3 files changed

Lines changed: 55 additions & 2 deletions

File tree

src/services/codefixes/disableJsDiagnostics.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace ts.codefix {
3232
}
3333
}
3434

35-
// If all fails, add an extra new line immediatlly before the error span.
35+
// If all fails, add an extra new line immediately before the error span.
3636
return {
3737
span: { start: position, length: 0 },
3838
newText: `${position === startPosition ? "" : newLineCharacter}// @ts-ignore${newLineCharacter}`
@@ -67,4 +67,4 @@ namespace ts.codefix {
6767
}]
6868
}];
6969
}
70-
}
70+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* @internal */
2+
namespace ts.codefix {
3+
registerCodeFix({
4+
errorCodes: [Diagnostics.JSDoc_types_can_only_be_used_inside_documentation_comments.code],
5+
getCodeActions: getActionsForJSDocTypes
6+
});
7+
8+
function getActionsForJSDocTypes(context: CodeFixContext): CodeAction[] | undefined {
9+
const sourceFile = context.sourceFile;
10+
11+
const node = getTokenAtPosition(sourceFile, context.span.start, /*includeJsDocComment*/ false);
12+
if (node.kind !== SyntaxKind.VariableDeclaration) return;
13+
14+
const type = (node as VariableDeclaration).type;
15+
if (containsJSDocType(type)) {
16+
const tsType = getTypeFromJSDocType(type);
17+
return [{
18+
description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Change_0_to_1), [getTextOfNode(type), tsType]),
19+
changes: [{
20+
fileName: sourceFile.fileName,
21+
textChanges: [{
22+
span: { start: type.getStart(), length: type.getWidth() },
23+
newText: tsType
24+
}],
25+
}],
26+
}];
27+
}
28+
}
29+
30+
function containsJSDocType(type: TypeNode): boolean {
31+
switch (type.kind) {
32+
case SyntaxKind.JSDocUnknownType:
33+
case SyntaxKind.JSDocAllType:
34+
case SyntaxKind.JSDocVariadicType:
35+
return true;
36+
// TODO: Of course you can put JSDoc types inside normal types, like number?[] and so on
37+
}
38+
}
39+
40+
function getTypeFromJSDocType(type: TypeNode): string {
41+
switch (type.kind) {
42+
case SyntaxKind.JSDocUnknownType:
43+
case SyntaxKind.JSDocAllType:
44+
return "any";
45+
case SyntaxKind.JSDocVariadicType:
46+
// this will surely work!
47+
return getTypeFromJSDocType((type as JSDocVariadicType).type) + "[]";
48+
// TODO: Of course you can put JSDoc types inside normal types, like number?[] and so on
49+
}
50+
return getTextOfNode(type);
51+
}
52+
}

src/services/codefixes/fixes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
/// <reference path="fixExtendsInterfaceBecomesImplements.ts" />
88
/// <reference path="fixForgottenThisPropertyAccess.ts" />
99
/// <reference path='fixUnusedIdentifier.ts' />
10+
/// <reference path='fixJSDocTypes.ts' />
1011
/// <reference path='importFixes.ts' />
1112
/// <reference path='disableJsDiagnostics.ts' />
1213
/// <reference path='helpers.ts' />

0 commit comments

Comments
 (0)