Skip to content

Commit c099938

Browse files
author
Arthur Ozga
committed
move state to flags
1 parent 82109d3 commit c099938

File tree

2 files changed

+35
-34
lines changed

2 files changed

+35
-34
lines changed

src/compiler/checker.ts

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2229,7 +2229,7 @@ namespace ts {
22292229

22302230
return result;
22312231
}
2232-
2232+
22332233
function typeFormatFlagsToNodeBuilderFlags(flags: TypeFormatFlags): NodeBuilderFlags {
22342234
let result = NodeBuilderFlags.None;
22352235
if (flags & TypeFormatFlags.WriteArrayAsGenericType) {
@@ -2270,7 +2270,7 @@ namespace ts {
22702270
}
22712271

22722272
function typeToString(type: Type, enclosingDeclaration?: Node, flags?: TypeFormatFlags): string {
2273-
const typeNode = nodeBuilder.typeToTypeNode(type, enclosingDeclaration, typeFormatFlagsToNodeBuilderFlags(flags) | NodeBuilderFlags.ignoreErrors, !!(flags & TypeFormatFlags.InTypeAlias));
2273+
const typeNode = nodeBuilder.typeToTypeNode(type, enclosingDeclaration, typeFormatFlagsToNodeBuilderFlags(flags) | NodeBuilderFlags.ignoreErrors);
22742274
Debug.assert(typeNode !== undefined, "should always get typenode?");
22752275
const newLine = NewLineKind.None;
22762276
const options = { newLine, removeComments: true };
@@ -2289,9 +2289,8 @@ namespace ts {
22892289

22902290
function createNodeBuilder() {
22912291
return {
2292-
typeToTypeNode: (type: Type, enclosingDeclaration?: Node, flags?: NodeBuilderFlags, inTypeAlias?: boolean) => {
2292+
typeToTypeNode: (type: Type, enclosingDeclaration?: Node, flags?: NodeBuilderFlags) => {
22932293
const context = createNodeBuilderContext(enclosingDeclaration, flags);
2294-
context.InTypeAlias = inTypeAlias;
22952294
const resultingNode = typeToTypeNodeHelper(type, context);
22962295
const result = context.encounteredError ? undefined : resultingNode;
22972296
return result;
@@ -2312,14 +2311,10 @@ namespace ts {
23122311

23132312
interface NodeBuilderContext {
23142313
enclosingDeclaration: Node | undefined;
2315-
readonly flags: NodeBuilderFlags | undefined;
2314+
flags: NodeBuilderFlags | undefined;
23162315

23172316
// State
23182317
encounteredError: boolean;
2319-
inObjectTypeLiteral: boolean;
2320-
InElementType: boolean; // Writing an array or union element type
2321-
InFirstTypeArgument: boolean; // Writing first type argument of the instantiated type
2322-
InTypeAlias: boolean; // Writing type in type alias declaration
23232318
symbolStack: Symbol[] | undefined;
23242319
}
23252320

@@ -2328,21 +2323,15 @@ namespace ts {
23282323
enclosingDeclaration,
23292324
flags,
23302325
encounteredError: false,
2331-
inObjectTypeLiteral: false,
2332-
InElementType: false,
2333-
InFirstTypeArgument: false,
2334-
InTypeAlias: false,
23352326
symbolStack: undefined
23362327
};
23372328
}
23382329

23392330
function typeToTypeNodeHelper(type: Type, context: NodeBuilderContext): TypeNode {
2340-
const inElementType = context.InElementType;
2341-
context.InElementType = false;
2342-
const inTypeAlias = context.InTypeAlias;
2343-
context.InTypeAlias = false;
2344-
const inFirstTypeArgument = context.InFirstTypeArgument;
2345-
context.InFirstTypeArgument = false;
2331+
const inElementType = context.flags & NodeBuilderFlags.InElementType;
2332+
const inFirstTypeArgument = context.flags & NodeBuilderFlags.InFirstTypeArgument;
2333+
const inTypeAlias = context.flags & NodeBuilderFlags.InTypeAlias;
2334+
context.flags &= ~(NodeBuilderFlags.StateClearingFlags);
23462335

23472336
if (!type) {
23482337
context.encounteredError = true;
@@ -2400,7 +2389,7 @@ namespace ts {
24002389
return createKeywordTypeNode(SyntaxKind.ObjectKeyword);
24012390
}
24022391
if (type.flags & TypeFlags.TypeParameter && (type as TypeParameter).isThisType) {
2403-
if (context.inObjectTypeLiteral) {
2392+
if (context.flags & NodeBuilderFlags.inObjectTypeLiteral) {
24042393
if (!context.encounteredError && !(context.flags & NodeBuilderFlags.allowThisInObjectLiteral)) {
24052394
context.encounteredError = true;
24062395
}
@@ -2455,16 +2444,16 @@ namespace ts {
24552444

24562445
if (type.flags & TypeFlags.Index) {
24572446
const indexedType = (<IndexType>type).type;
2458-
context.InElementType = <boolean>true;
2447+
context.flags |= NodeBuilderFlags.InElementType;
24592448
const indexTypeNode = typeToTypeNodeHelper(indexedType, context);
2460-
Debug.assert(context.InElementType === false);
2449+
Debug.assert(!(context.flags & NodeBuilderFlags.InElementType));
24612450
return createTypeOperatorNode(indexTypeNode);
24622451
}
24632452

24642453
if (type.flags & TypeFlags.IndexedAccess) {
2465-
context.InElementType = <boolean>true;
2454+
context.flags |= NodeBuilderFlags.InElementType;
24662455
const objectTypeNode = typeToTypeNodeHelper((<IndexedAccessType>type).objectType, context);
2467-
Debug.assert(context.InElementType === false);
2456+
Debug.assert(!(context.flags & NodeBuilderFlags.InElementType));
24682457
const indexTypeNode = typeToTypeNodeHelper((<IndexedAccessType>type).indexType, context);
24692458
return createIndexedAccessTypeNode(objectTypeNode, indexTypeNode);
24702459
}
@@ -2567,10 +2556,10 @@ namespace ts {
25672556
}
25682557
}
25692558

2570-
const saveInObjectTypeLiteral = context.inObjectTypeLiteral;
2571-
context.inObjectTypeLiteral = true;
2559+
const savedFlags = context.flags;
2560+
context.flags |= NodeBuilderFlags.inObjectTypeLiteral;
25722561
const members = createTypeNodesFromResolvedType(resolved);
2573-
context.inObjectTypeLiteral = saveInObjectTypeLiteral;
2562+
context.flags = savedFlags;
25742563
const typeLiteralNode = createTypeLiteralNode(members);
25752564
return setEmitFlags(typeLiteralNode, EmitFlags.ToStringFormatting);
25762565
}
@@ -2606,9 +2595,11 @@ namespace ts {
26062595
const typeArgumentNode = typeToTypeNodeHelper(typeArguments[0], context);
26072596
return createTypeReferenceNode("Array", [typeArgumentNode]);
26082597
}
2609-
context.InElementType = true;
2598+
2599+
context.flags |= NodeBuilderFlags.InElementType;
26102600
const elementType = typeToTypeNodeHelper(typeArguments[0], context);
2611-
context.InElementType = false;
2601+
Debug.assert(!(context.flags & NodeBuilderFlags.InElementType));
2602+
26122603
return createArrayTypeNode(elementType);
26132604
}
26142605
else if (type.target.objectFlags & ObjectFlags.Tuple) {
@@ -2756,19 +2747,21 @@ namespace ts {
27562747

27572748
function mapToTypeNodeArray(types: Type[], context: NodeBuilderContext, addInElementTypeFlag: boolean, addInFirstTypeArgumentFlag: boolean): TypeNode[] {
27582749
const result = [];
2759-
Debug.assert(context.InElementType === false, "should be unset at the beginning of the helper");
2750+
Debug.assert(!(context.flags & NodeBuilderFlags.InElementType), "should be unset at the beginning of the helper");
27602751
for (let i = 0; i < types.length; ++i) {
27612752
const type = types[i];
2762-
context.InElementType = addInElementTypeFlag;
2763-
if (i === 0) {
2764-
context.InFirstTypeArgument = addInFirstTypeArgumentFlag;
2753+
if (addInElementTypeFlag) {
2754+
context.flags |= NodeBuilderFlags.InElementType;
2755+
}
2756+
if (i === 0 && addInFirstTypeArgumentFlag) {
2757+
context.flags |= NodeBuilderFlags.InFirstTypeArgument;
27652758
}
27662759
const typeNode = typeToTypeNodeHelper(type, context);
27672760
if (typeNode) {
27682761
result.push(typeNode);
27692762
}
27702763
}
2771-
Debug.assert(context.InElementType === false, "should be unset at the beginning of the helper");
2764+
Debug.assert(!(context.flags & NodeBuilderFlags.InElementType), "should be unset at the end of the helper");
27722765
return result;
27732766
}
27742767

src/compiler/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2589,6 +2589,14 @@ namespace ts {
25892589

25902590
ignoreErrors = allowThisInObjectLiteral | allowQualifedNameInPlaceOfIdentifier | allowTypeParameterInQualifiedName | allowAnonymousIdentifier | allowEmptyUnionOrIntersection | allowEmptyTuple,
25912591

2592+
// State
2593+
inObjectTypeLiteral = 1 << 20,
2594+
InElementType = 1 << 21, // Writing an array or union element type
2595+
InFirstTypeArgument = 1 << 22, // Writing first type argument of the instantiated type
2596+
InTypeAlias = 1 << 23, // Writing type in type alias declaration
2597+
2598+
/** Flags that should not be passed on to sub-nodes of the current node being built. */
2599+
StateClearingFlags = InElementType | InFirstTypeArgument | InTypeAlias
25922600
}
25932601

25942602
export interface SymbolDisplayBuilder {

0 commit comments

Comments
 (0)