Skip to content

Commit a1d2049

Browse files
author
Arthur Ozga
committed
convert type format flags
1 parent b8bb0c2 commit a1d2049

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

src/compiler/checker.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2242,10 +2242,49 @@ namespace ts {
22422242
return result;
22432243
}
22442244

2245+
function typeFormatFlagsToNodeBuilderFlags(flags: TypeFormatFlags): NodeBuilderFlags {
2246+
let result = NodeBuilderFlags.None;
2247+
if (flags & TypeFormatFlags.WriteArrayAsGenericType) {
2248+
result |= NodeBuilderFlags.WriteArrayAsGenericType;
2249+
}
2250+
if (flags & TypeFormatFlags.UseTypeOfFunction) {
2251+
result |= NodeBuilderFlags.UseTypeOfFunction;
2252+
}
2253+
if (flags & TypeFormatFlags.NoTruncation) {
2254+
result |= NodeBuilderFlags.NoTruncation;
2255+
}
2256+
if (flags & TypeFormatFlags.WriteArrowStyleSignature) {
2257+
result |= NodeBuilderFlags.WriteArrowStyleSignature;
2258+
}
2259+
if (flags & TypeFormatFlags.WriteOwnNameForAnyLike) {
2260+
result |= NodeBuilderFlags.WriteOwnNameForAnyLike;
2261+
}
2262+
if (flags & TypeFormatFlags.WriteArrayAsGenericType) {
2263+
result |= NodeBuilderFlags.WriteArrayAsGenericType;
2264+
}
2265+
if (flags & TypeFormatFlags.WriteTypeArgumentsOfSignature) {
2266+
result |= NodeBuilderFlags.WriteTypeArgumentsOfSignature;
2267+
}
2268+
if (flags & TypeFormatFlags.UseFullyQualifiedType) {
2269+
result |= NodeBuilderFlags.UseFullyQualifiedType;
2270+
}
2271+
if (flags & TypeFormatFlags.UseTypeAliasValue) {
2272+
result |= NodeBuilderFlags.UseTypeAliasValue;
2273+
}
2274+
if (flags & TypeFormatFlags.SuppressAnyReturnType) {
2275+
result |= NodeBuilderFlags.SuppressAnyReturnType;
2276+
}
2277+
if (flags & TypeFormatFlags.AddUndefined) {
2278+
result |= NodeBuilderFlags.AddUndefined;
2279+
}
2280+
2281+
return result;
2282+
}
2283+
22452284
function typeToString(type: Type, enclosingDeclaration?: Node, flags?: TypeFormatFlags): string {
22462285
const str = oldTypeToString(type, enclosingDeclaration, flags); str;
22472286
const str2 = oldTypeToString(type, enclosingDeclaration, flags); str2;
2248-
const typeNode = nodeBuilder.typeToTypeNode(type, enclosingDeclaration, NodeBuilderFlags.ignoreErrors);
2287+
const typeNode = nodeBuilder.typeToTypeNode(type, enclosingDeclaration, typeFormatFlagsToNodeBuilderFlags(flags) | NodeBuilderFlags.ignoreErrors, !!(flags & TypeFormatFlags.InTypeAlias));
22492288
Debug.assert(typeNode !== undefined, "should always get typenode?");
22502289
const newLine = NewLineKind.None;
22512290
const options = { newLine, removeComments: true };
@@ -2264,8 +2303,9 @@ namespace ts {
22642303

22652304
function createNodeBuilder() {
22662305
return {
2267-
typeToTypeNode: (type: Type, enclosingDeclaration?: Node, flags?: NodeBuilderFlags) => {
2306+
typeToTypeNode: (type: Type, enclosingDeclaration?: Node, flags?: NodeBuilderFlags, inTypeAlias?: boolean) => {
22682307
const context = createNodeBuilderContext(enclosingDeclaration, flags);
2308+
context.InTypeAlias = inTypeAlias;
22692309
const resultingNode = typeToTypeNodeHelper(type, context);
22702310
const result = context.encounteredError ? undefined : resultingNode;
22712311
return result;
@@ -2403,6 +2443,7 @@ namespace ts {
24032443
if (type.flags & TypeFlags.TypeParameter) {
24042444
const name = symbolToName(type.symbol, /*expectsIdentifier*/ false, context);
24052445
// Ignore constraint/default when creating a usage (as opposed to declaration) of a type parameter.
2446+
// TODO: add constraint here?
24062447
return createTypeReferenceNode(name, /*typeArguments*/ undefined);
24072448
}
24082449

@@ -2585,6 +2626,10 @@ namespace ts {
25852626
function typeReferenceToTypeNode(type: TypeReference) {
25862627
const typeArguments: Type[] = type.typeArguments || emptyArray;
25872628
if (type.target === globalArrayType) {
2629+
if (context.flags & NodeBuilderFlags.WriteArrayAsGenericType) {
2630+
const typeArgumentNode = typeToTypeNodeHelper(typeArguments[0], context);
2631+
return createTypeReferenceNode("Array", [typeArgumentNode]);
2632+
}
25882633
context.InElementType = true;
25892634
const elementType = typeToTypeNodeHelper(typeArguments[0], context);
25902635
context.InElementType = false;

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2496,7 +2496,7 @@ namespace ts {
24962496

24972497
/** Note that the resulting nodes cannot be checked. */
24982498

2499-
typeToTypeNode(type: Type, enclosingDeclaration?: Node, flags?: NodeBuilderFlags): TypeNode;
2499+
typeToTypeNode(type: Type, enclosingDeclaration?: Node, flags?: NodeBuilderFlags, inTypeAlias?: boolean): TypeNode;
25002500
/** Note that the resulting nodes cannot be checked. */
25012501
signatureToSignatureDeclaration(signature: Signature, kind: SyntaxKind, enclosingDeclaration?: Node, flags?: NodeBuilderFlags): SignatureDeclaration;
25022502
/** Note that the resulting nodes cannot be checked. */

0 commit comments

Comments
 (0)