Skip to content

Commit

Permalink
Don't add excess properties to type nodes in typeToTypeNode
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton committed Dec 13, 2022
1 parent f5c1ca1 commit d4c2ef3
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2348,8 +2348,8 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
const saveCurrentFlow = currentFlow;
for (const typeAlias of delayedTypeAliases) {
const host = typeAlias.parent.parent;
container = findAncestor(host.parent, n => !!(getContainerFlags(n) & ContainerFlags.IsContainer)) as IsContainer | undefined || file;
blockScopeContainer = getEnclosingBlockScopeContainer(host) as IsBlockScopedContainer | undefined || file;
container = (findAncestor(host.parent, n => !!(getContainerFlags(n) & ContainerFlags.IsContainer)) as IsContainer | undefined) || file;
blockScopeContainer = (getEnclosingBlockScopeContainer(host) as IsBlockScopedContainer | undefined) || file;
currentFlow = initFlowNode({ flags: FlowFlags.Start });
parent = typeAlias;
bind(typeAlias.typeExpression);
Expand Down
10 changes: 3 additions & 7 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ import {
canHaveJSDoc,
canHaveLocals,
canHaveModifiers,
canUsePropertyAccess,
canHaveSymbol,
canUsePropertyAccess,
cartesianProduct,
CaseBlock,
CaseClause,
Expand Down Expand Up @@ -6597,7 +6597,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
context.truncating = true;
}
context.approximateLength += cachedResult.addedLength;
return deepCloneOrReuseNode(cachedResult) as TypeNode as T;
return deepCloneOrReuseNode(cachedResult.node) as T;
}

let depth: number | undefined;
Expand All @@ -6613,11 +6613,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const result = transform(type);
const addedLength = context.approximateLength - startLength;
if (!context.reportedDiagnostic && !context.encounteredError) {
if (context.truncating) {
(result as any).truncating = true;
}
(result as any).addedLength = addedLength;
links?.serializedTypes?.set(key, result as TypeNode as TypeNode & {truncating?: boolean, addedLength: number});
links?.serializedTypes?.set(key, { node: result, truncating: context.truncating, addedLength });
}
context.visitedTypes.delete(typeId);
if (id) {
Expand Down
20 changes: 2 additions & 18 deletions src/compiler/factory/nodeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2319,9 +2319,6 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
const node = createBaseDeclaration<TypeLiteralNode>(SyntaxKind.TypeLiteral);
node.members = createNodeArray(members);
node.transformFlags = TransformFlags.ContainsTypeScript;

// node.locals = undefined; // initialized by binder (LocalsContainer)
// node.nextContainer = undefined; // initialized by binder (LocalsContainer)
return node;
}

Expand Down Expand Up @@ -2749,8 +2746,6 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode

node.jsDoc = undefined; // initialized by parser (JsDocContainer)
node.jsDocCache = undefined; // initialized by parser (JsDocContainer)
// node.locals = undefined; // initialized by binder (LocalsContainer)
// node.nextContainer = undefined; // initialized by binder (LocalsContainer)
return node;
}

Expand Down Expand Up @@ -3585,8 +3580,6 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode

node.jsDoc = undefined; // initialized by parser (JsDocContainer)
node.jsDocCache = undefined; // initialized by parser (JsDocContainer)
// node.locals = undefined; // initialized by binder (LocalsContainer)
// node.nextContainer = undefined; // initialized by binder (LocalsContainer)
return node;
}

Expand Down Expand Up @@ -4373,8 +4366,6 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode

node.jsDoc = undefined; // initialized by parser (JsDocContainer)
node.jsDocCache = undefined; // initialized by parser (JsDocContainer)
// node.locals = undefined; // initialized by binder (LocalsContainer)
// node.nextContainer = undefined; // initialized by binder (LocalsContainer)
return node;
}

Expand Down Expand Up @@ -4415,8 +4406,6 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
node.illegalDecorators = undefined; // initialized by parser for grammar errors
node.jsDoc = undefined; // initialized by parser (JsDocContainer)
node.jsDocCache = undefined; // initialized by parser (JsDocContainer)
// node.locals = undefined; // initialized by binder (LocalsContainer)
// node.nextContainer = undefined; // initialized by binder (LocalsContainer)
return node;
}

Expand Down Expand Up @@ -4510,8 +4499,6 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
node.illegalDecorators = undefined; // initialized by parser for grammar errors
node.jsDoc = undefined; // initialized by parser (JsDocContainer)
node.jsDocCache = undefined; // initialized by parser (JsDocContainer)
// node.locals = undefined; // initialized by binder (LocalsContainer)
// node.nextContainer = undefined; // initialized by binder (LocalsContainer)
return node;
}

Expand Down Expand Up @@ -5142,8 +5129,6 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
const node = createBaseDeclaration<JSDocTypeLiteral>(SyntaxKind.JSDocTypeLiteral);
node.jsDocPropertyTags = asNodeArray(propertyTags);
node.isArrayType = isArrayType;
// node.locals = undefined; // initialized by binder (LocalsContainer)
// node.nextContainer = undefined; // initialized by binder (LocalsContainer)
return node;
}

Expand Down Expand Up @@ -5738,8 +5723,6 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
node.transformFlags |=
propagateChildrenFlags(node.properties) |
TransformFlags.ContainsJsx;
// node.locals = undefined; // initialized by binder (LocalsContainer)
// node.nextContainer = undefined; // initialized by binder (LocalsContainer)
return node;
}

Expand Down Expand Up @@ -6097,7 +6080,8 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
}

function cloneSourceFileWorker(source: SourceFile) {
// TODO: explicit property assignments instead of for..in
// TODO: This mechanism for cloning results in megamorphic property reads and writes. In future perf-related
// work, we should consider switching explicit property assignments instead of using `for..in`.
const node = baseFactory.createBaseSourceFileNode(SyntaxKind.SourceFile) as Mutable<SourceFile>;
node.flags |= source.flags & ~NodeFlags.Synthesized;
for (const p in source) {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1580,7 +1580,7 @@ namespace Parser {
// Prime the scanner.
nextToken();
const pos = getNodePos();
let statements, endOfFileToken: EndOfFileToken;
let statements, endOfFileToken;
if (token() === SyntaxKind.EndOfFileToken) {
statements = createNodeArray([], pos, pos);
endOfFileToken = parseTokenNode<EndOfFileToken>();
Expand Down
9 changes: 8 additions & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5953,12 +5953,19 @@ export interface NodeLinks {
isExhaustive?: boolean | 0; // Is node an exhaustive switch statement (0 indicates in-process resolution)
skipDirectInference?: true; // Flag set by the API `getContextualType` call on a node when `Completions` is passed to force the checker to skip making inferences to a node's type
declarationRequiresScopeChange?: boolean; // Set by `useOuterVariableScopeInParameter` in checker when downlevel emit would change the name resolution scope inside of a parameter.
serializedTypes?: Map<string, TypeNode & {truncating?: boolean, addedLength: number}>; // Collection of types serialized at this location
serializedTypes?: Map<string, SerializedTypeEntry>; // Collection of types serialized at this location

contextualType?: Type; // Used to temporarily assign a contextual type during overload resolution
inferenceContext?: InferenceContext; // Inference context for contextual type
}

/** @internal */
export interface SerializedTypeEntry {
node: TypeNode;
truncating?: boolean;
addedLength: number;
}

export const enum TypeFlags {
Any = 1 << 0,
Unknown = 1 << 1,
Expand Down

0 comments on commit d4c2ef3

Please sign in to comment.