Codefix jsdoc types for anything with a .type

That means type parameters and type arguments are still not handled.
This commit is contained in:
Nathan Shively-Sanders 2017-08-29 10:38:16 -07:00
parent b082c27fbe
commit 63cb84f3d1
15 changed files with 75 additions and 2 deletions

View File

@ -8,11 +8,27 @@ namespace ts.codefix {
function getActionsForJSDocTypes(context: CodeFixContext): CodeAction[] | undefined {
const sourceFile = context.sourceFile;
const node = getTokenAtPosition(sourceFile, context.span.start, /*includeJsDocComment*/ false);
// NOTE: Some locations are not handled yet:
// MappedTypeNode.typeParameters and SignatureDeclaration.typeParameters, as well as CallExpression.typeArguments
const decl = ts.findAncestor(node,
n => n.kind === SyntaxKind.VariableDeclaration ||
n =>
n.kind === SyntaxKind.AsExpression ||
n.kind === SyntaxKind.CallSignature ||
n.kind === SyntaxKind.ConstructSignature ||
n.kind === SyntaxKind.FunctionDeclaration ||
n.kind === SyntaxKind.GetAccessor ||
n.kind === SyntaxKind.IndexSignature ||
n.kind === SyntaxKind.MappedType ||
n.kind === SyntaxKind.MethodDeclaration ||
n.kind === SyntaxKind.MethodSignature ||
n.kind === SyntaxKind.Parameter ||
n.kind === SyntaxKind.PropertyDeclaration ||
n.kind === SyntaxKind.PropertyAssignment);
n.kind === SyntaxKind.PropertySignature ||
n.kind === SyntaxKind.SetAccessor ||
n.kind === SyntaxKind.TypeAliasDeclaration ||
n.kind === SyntaxKind.TypeAssertionExpression ||
n.kind === SyntaxKind.VariableDeclaration);
if (!decl) return;
const checker = context.program.getTypeChecker();

View File

@ -0,0 +1,5 @@
// @strict: true
/// <reference path='fourslash.ts' />
//// var x = 12 as [|number?|];
verify.rangeAfterCodeFix("number | null", /*includeWhiteSpace*/ false, /*errorCode*/ 8020, 0);

View File

@ -0,0 +1,5 @@
/// <reference path='fourslash.ts' />
//// var f = <[|function(number?): number|]>(x => x);
// note: without --strict, number? --> number, not number | null
verify.rangeAfterCodeFix("(arg0: number) => number", /*includeWhiteSpace*/ false, /*errorCode*/ 8020, 0);

View File

@ -0,0 +1,4 @@
/// <reference path='fourslash.ts' />
//// var f: { [K in keyof number]: [|*|] };
verify.rangeAfterCodeFix("any");

View File

@ -0,0 +1,3 @@
/// <reference path='fourslash.ts' />
//// declare function index(ix: number): [|*|];
verify.rangeAfterCodeFix("any");

View File

@ -0,0 +1,3 @@
/// <reference path='fourslash.ts' />
//// var index: { (ix: number): [|?|] };
verify.rangeAfterCodeFix("any");

View File

@ -0,0 +1,3 @@
/// <reference path='fourslash.ts' />
//// var index: { new (ix: number): [|?|] };
verify.rangeAfterCodeFix("any");

View File

@ -0,0 +1,3 @@
/// <reference path='fourslash.ts' />
//// var index = { get p(): [|*|] { return 12 } };
verify.rangeAfterCodeFix("any");

View File

@ -0,0 +1,3 @@
/// <reference path='fourslash.ts' />
//// var index = { set p(x: [|*|]) { } };
verify.rangeAfterCodeFix("any");

View File

@ -0,0 +1,3 @@
/// <reference path='fourslash.ts' />
//// var index: { [s: string]: [|*|] };
verify.rangeAfterCodeFix("any");

View File

@ -0,0 +1,6 @@
/// <reference path='fourslash.ts' />
////class C {
//// m(): [|*|] {
//// }
////}
verify.rangeAfterCodeFix("any");

View File

@ -0,0 +1,5 @@
/// <reference path='fourslash.ts' />
////declare class C {
//// m(): [|*|];
////}
verify.rangeAfterCodeFix("any");

View File

@ -0,0 +1,5 @@
/// <reference path='fourslash.ts' />
////declare class C {
//// p: [|*|];
////}
verify.rangeAfterCodeFix("any");

View File

@ -0,0 +1,5 @@
/// <reference path='fourslash.ts' />
////class C {
//// p: [|*|] = 12;
////}
verify.rangeAfterCodeFix("any");

View File

@ -0,0 +1,4 @@
// @strict: true
/// <reference path='fourslash.ts' />
////type T = [|...number?|];
verify.rangeAfterCodeFix("(number | null)[]");