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 6, 2022
1 parent 8e7a080 commit e1ab7ed
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 28 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
8 changes: 2 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6589,7 +6589,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 @@ -6605,11 +6605,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 @@ -2316,9 +2316,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 @@ -2746,8 +2743,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 @@ -3582,8 +3577,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 @@ -4370,8 +4363,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 @@ -4412,8 +4403,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 @@ -4507,8 +4496,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 @@ -5139,8 +5126,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 @@ -5735,8 +5720,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 @@ -6094,7 +6077,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 @@ -1578,7 +1578,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 @@ -5944,12 +5944,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 e1ab7ed

Please sign in to comment.