Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit efdba54

Browse filesBrowse 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
Copy full SHA for efdba54

3 files changed

+55-2Lines changed: 55 additions & 2 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/services/codefixes/disableJsDiagnostics.ts‎

Copy file name to clipboardExpand all lines: src/services/codefixes/disableJsDiagnostics.ts
+2-2Lines 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+
}
Collapse file
+52Lines 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+
}
Collapse file

‎src/services/codefixes/fixes.ts‎

Copy file name to clipboardExpand all lines: src/services/codefixes/fixes.ts
+1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.