Skip to content

Commit b84e6bc

Browse files
authored
Merge pull request #18096 from Microsoft/fix-codefix-jsdoc-variablelike-decls
Fix the jsdoc codefix for VariableLike declarations
2 parents 7541c70 + 63cb84f commit b84e6bc

19 files changed

+101
-1
lines changed

src/services/codefixes/fixJSDocTypes.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,32 @@ namespace ts.codefix {
88
function getActionsForJSDocTypes(context: CodeFixContext): CodeAction[] | undefined {
99
const sourceFile = context.sourceFile;
1010
const node = getTokenAtPosition(sourceFile, context.span.start, /*includeJsDocComment*/ false);
11-
const decl = ts.findAncestor(node, n => n.kind === SyntaxKind.VariableDeclaration);
11+
12+
// NOTE: Some locations are not handled yet:
13+
// MappedTypeNode.typeParameters and SignatureDeclaration.typeParameters, as well as CallExpression.typeArguments
14+
const decl = ts.findAncestor(node,
15+
n =>
16+
n.kind === SyntaxKind.AsExpression ||
17+
n.kind === SyntaxKind.CallSignature ||
18+
n.kind === SyntaxKind.ConstructSignature ||
19+
n.kind === SyntaxKind.FunctionDeclaration ||
20+
n.kind === SyntaxKind.GetAccessor ||
21+
n.kind === SyntaxKind.IndexSignature ||
22+
n.kind === SyntaxKind.MappedType ||
23+
n.kind === SyntaxKind.MethodDeclaration ||
24+
n.kind === SyntaxKind.MethodSignature ||
25+
n.kind === SyntaxKind.Parameter ||
26+
n.kind === SyntaxKind.PropertyDeclaration ||
27+
n.kind === SyntaxKind.PropertySignature ||
28+
n.kind === SyntaxKind.SetAccessor ||
29+
n.kind === SyntaxKind.TypeAliasDeclaration ||
30+
n.kind === SyntaxKind.TypeAssertionExpression ||
31+
n.kind === SyntaxKind.VariableDeclaration);
1232
if (!decl) return;
1333
const checker = context.program.getTypeChecker();
1434

1535
const jsdocType = (decl as VariableDeclaration).type;
36+
if (!jsdocType) return;
1637
const original = getTextOfNode(jsdocType);
1738
const type = checker.getTypeFromTypeNode(jsdocType);
1839
const actions = [createAction(jsdocType, sourceFile.fileName, original, checker.typeToString(type, /*enclosingDeclaration*/ undefined, TypeFormatFlags.NoTruncation))];
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @strict: true
2+
/// <reference path='fourslash.ts' />
3+
//// function f(x: [|number?|]) {
4+
//// }
5+
verify.rangeAfterCodeFix("number | null", /*includeWhiteSpace*/ false, /*errorCode*/ 8020, 0);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @strict: true
2+
/// <reference path='fourslash.ts' />
3+
//// var f = function f(x: [|string?|]) {
4+
//// }
5+
verify.rangeAfterCodeFix("string | null | undefined", /*includeWhiteSpace*/ false, /*errorCode*/ 8020, 1);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @strict: true
2+
/// <reference path='fourslash.ts' />
3+
////class C {
4+
//// p: [|*|]
5+
////}
6+
verify.rangeAfterCodeFix("any", /*includeWhiteSpace*/ false, /*errorCode*/ 8020, 0);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @strict: true
2+
/// <reference path='fourslash.ts' />
3+
////class C {
4+
//// p: [|*|] = 12
5+
////}
6+
verify.rangeAfterCodeFix("any", /*includeWhiteSpace*/ false, /*errorCode*/ 8020, 0);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @strict: true
2+
/// <reference path='fourslash.ts' />
3+
//// var x = 12 as [|number?|];
4+
5+
verify.rangeAfterCodeFix("number | null", /*includeWhiteSpace*/ false, /*errorCode*/ 8020, 0);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// <reference path='fourslash.ts' />
2+
//// var f = <[|function(number?): number|]>(x => x);
3+
4+
// note: without --strict, number? --> number, not number | null
5+
verify.rangeAfterCodeFix("(arg0: number) => number", /*includeWhiteSpace*/ false, /*errorCode*/ 8020, 0);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/// <reference path='fourslash.ts' />
2+
//// var f: { [K in keyof number]: [|*|] };
3+
4+
verify.rangeAfterCodeFix("any");
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/// <reference path='fourslash.ts' />
2+
//// declare function index(ix: number): [|*|];
3+
verify.rangeAfterCodeFix("any");
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/// <reference path='fourslash.ts' />
2+
//// var index: { (ix: number): [|?|] };
3+
verify.rangeAfterCodeFix("any");

0 commit comments

Comments
 (0)