mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-15 03:23:08 -06:00
fix(51374): ts(80004): Quick fix... > Annotate with type from JSDoc :: object types (#51378)
* fix(51374): transform JSDocTypeLiteral * add additional tests * add additional tests
This commit is contained in:
parent
eac566b8c3
commit
fa4b49d541
@ -13007,14 +13007,6 @@ namespace ts {
|
||||
return isPropertyDeclaration(node) && !hasAccessorModifier(node) && node.questionToken;
|
||||
}
|
||||
|
||||
function isOptionalJSDocPropertyLikeTag(node: Node): node is JSDocPropertyLikeTag {
|
||||
if (!isJSDocPropertyLikeTag(node)) {
|
||||
return false;
|
||||
}
|
||||
const { isBracketed, typeExpression } = node;
|
||||
return isBracketed || !!typeExpression && typeExpression.type.kind === SyntaxKind.JSDocOptionalType;
|
||||
}
|
||||
|
||||
function createTypePredicate(kind: TypePredicateKind, parameterName: string | undefined, parameterIndex: number | undefined, type: Type | undefined): TypePredicate {
|
||||
return { kind, parameterName, parameterIndex, type } as TypePredicate;
|
||||
}
|
||||
|
||||
@ -7778,4 +7778,12 @@ namespace ts {
|
||||
return isEnumDeclaration(node) || isVariableStatement(node) || isFunctionDeclaration(node) || isClassDeclaration(node)
|
||||
|| isInterfaceDeclaration(node) || isTypeDeclaration(node) || (isModuleDeclaration(node) && !isExternalModuleAugmentation(node) && !isGlobalScopeAugmentation(node));
|
||||
}
|
||||
|
||||
export function isOptionalJSDocPropertyLikeTag(node: Node): node is JSDocPropertyLikeTag {
|
||||
if (!isJSDocPropertyLikeTag(node)) {
|
||||
return false;
|
||||
}
|
||||
const { isBracketed, typeExpression } = node;
|
||||
return isBracketed || !!typeExpression && typeExpression.type.kind === SyntaxKind.JSDocOptionalType;
|
||||
}
|
||||
}
|
||||
|
||||
@ -90,6 +90,8 @@ namespace ts.codefix {
|
||||
return transformJSDocFunctionType(node as JSDocFunctionType);
|
||||
case SyntaxKind.TypeReference:
|
||||
return transformJSDocTypeReference(node as TypeReferenceNode);
|
||||
case SyntaxKind.JSDocTypeLiteral:
|
||||
return transformJSDocTypeLiteral(node as JSDocTypeLiteral);
|
||||
default:
|
||||
const visited = visitEachChild(node, transformJSDocType, nullTransformationContext);
|
||||
setEmitFlags(visited, EmitFlags.SingleLine);
|
||||
@ -97,6 +99,17 @@ namespace ts.codefix {
|
||||
}
|
||||
}
|
||||
|
||||
function transformJSDocTypeLiteral(node: JSDocTypeLiteral) {
|
||||
const typeNode = factory.createTypeLiteralNode(map(node.jsDocPropertyTags, tag =>
|
||||
factory.createPropertySignature(
|
||||
/*modifiers*/ undefined,
|
||||
isIdentifier(tag.name) ? tag.name : tag.name.right,
|
||||
isOptionalJSDocPropertyLikeTag(tag) ? factory.createToken(SyntaxKind.QuestionToken) : undefined,
|
||||
tag.typeExpression && visitNode(tag.typeExpression.type, transformJSDocType) || factory.createKeywordTypeNode(SyntaxKind.AnyKeyword))));
|
||||
setEmitFlags(typeNode, EmitFlags.SingleLine);
|
||||
return typeNode;
|
||||
}
|
||||
|
||||
function transformJSDocOptionalType(node: JSDocOptionalType) {
|
||||
return factory.createUnionTypeNode([visitNode(node.type, transformJSDocType), factory.createTypeReferenceNode("undefined", emptyArray)]);
|
||||
}
|
||||
|
||||
31
tests/cases/fourslash/annotateWithTypeFromJSDoc24.ts
Normal file
31
tests/cases/fourslash/annotateWithTypeFromJSDoc24.ts
Normal file
@ -0,0 +1,31 @@
|
||||
/// <reference path="fourslash.ts" />
|
||||
// @strict: true
|
||||
|
||||
////class C {
|
||||
//// /**
|
||||
//// * @private
|
||||
//// * @param {number} foo
|
||||
//// * @param {Object} [bar]
|
||||
//// * @param {String} bar.a
|
||||
//// * @param {Number} [bar.b]
|
||||
//// * @param bar.c
|
||||
//// */
|
||||
//// m(foo, bar) { }
|
||||
////}
|
||||
|
||||
verify.codeFix({
|
||||
description: ts.Diagnostics.Annotate_with_type_from_JSDoc.message,
|
||||
index: 2,
|
||||
newFileContent:
|
||||
`class C {
|
||||
/**
|
||||
* @private
|
||||
* @param {number} foo
|
||||
* @param {Object} [bar]
|
||||
* @param {String} bar.a
|
||||
* @param {Number} [bar.b]
|
||||
* @param bar.c
|
||||
*/
|
||||
m(foo: number, bar: { a: string; b?: number; c: any; }) { }
|
||||
}`,
|
||||
});
|
||||
31
tests/cases/fourslash/annotateWithTypeFromJSDoc25.ts
Normal file
31
tests/cases/fourslash/annotateWithTypeFromJSDoc25.ts
Normal file
@ -0,0 +1,31 @@
|
||||
/// <reference path="fourslash.ts" />
|
||||
// @strict: true
|
||||
|
||||
////class C {
|
||||
//// /**
|
||||
//// * @private
|
||||
//// * @param {number} foo
|
||||
//// * @param {Object} [bar]
|
||||
//// * @param {String} bar.a
|
||||
//// * @param {Object} [baz]
|
||||
//// * @param {number} baz.c
|
||||
//// */
|
||||
//// m(foo, bar, baz) { }
|
||||
////}
|
||||
|
||||
verify.codeFix({
|
||||
description: ts.Diagnostics.Annotate_with_type_from_JSDoc.message,
|
||||
index: 3,
|
||||
newFileContent:
|
||||
`class C {
|
||||
/**
|
||||
* @private
|
||||
* @param {number} foo
|
||||
* @param {Object} [bar]
|
||||
* @param {String} bar.a
|
||||
* @param {Object} [baz]
|
||||
* @param {number} baz.c
|
||||
*/
|
||||
m(foo: number, bar: { a: string; }, baz: { c: number; }) { }
|
||||
}`,
|
||||
});
|
||||
27
tests/cases/fourslash/annotateWithTypeFromJSDoc26.ts
Normal file
27
tests/cases/fourslash/annotateWithTypeFromJSDoc26.ts
Normal file
@ -0,0 +1,27 @@
|
||||
/// <reference path="fourslash.ts" />
|
||||
// @strict: true
|
||||
|
||||
////class C {
|
||||
//// /**
|
||||
//// * @private
|
||||
//// * @param {Object} [foo]
|
||||
//// * @param {Object} foo.a
|
||||
//// * @param {String} [foo.a.b]
|
||||
//// */
|
||||
//// m(foo) { }
|
||||
////}
|
||||
|
||||
verify.codeFix({
|
||||
description: ts.Diagnostics.Annotate_with_type_from_JSDoc.message,
|
||||
index: 1,
|
||||
newFileContent:
|
||||
`class C {
|
||||
/**
|
||||
* @private
|
||||
* @param {Object} [foo]
|
||||
* @param {Object} foo.a
|
||||
* @param {String} [foo.a.b]
|
||||
*/
|
||||
m(foo: { a: { b?: string; }; }) { }
|
||||
}`,
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user