Skip to content

Commit e1ab7ed

Browse files
committed
Don't add excess properties to type nodes in typeToTypeNode
1 parent 8e7a080 commit e1ab7ed

File tree

5 files changed

+15
-28
lines changed

5 files changed

+15
-28
lines changed

src/compiler/binder.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2348,8 +2348,8 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
23482348
const saveCurrentFlow = currentFlow;
23492349
for (const typeAlias of delayedTypeAliases) {
23502350
const host = typeAlias.parent.parent;
2351-
container = findAncestor(host.parent, n => !!(getContainerFlags(n) & ContainerFlags.IsContainer)) as IsContainer | undefined || file;
2352-
blockScopeContainer = getEnclosingBlockScopeContainer(host) as IsBlockScopedContainer | undefined || file;
2351+
container = (findAncestor(host.parent, n => !!(getContainerFlags(n) & ContainerFlags.IsContainer)) as IsContainer | undefined) || file;
2352+
blockScopeContainer = (getEnclosingBlockScopeContainer(host) as IsBlockScopedContainer | undefined) || file;
23532353
currentFlow = initFlowNode({ flags: FlowFlags.Start });
23542354
parent = typeAlias;
23552355
bind(typeAlias.typeExpression);

src/compiler/checker.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -6589,7 +6589,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
65896589
context.truncating = true;
65906590
}
65916591
context.approximateLength += cachedResult.addedLength;
6592-
return deepCloneOrReuseNode(cachedResult) as TypeNode as T;
6592+
return deepCloneOrReuseNode(cachedResult.node) as T;
65936593
}
65946594

65956595
let depth: number | undefined;
@@ -6605,11 +6605,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
66056605
const result = transform(type);
66066606
const addedLength = context.approximateLength - startLength;
66076607
if (!context.reportedDiagnostic && !context.encounteredError) {
6608-
if (context.truncating) {
6609-
(result as any).truncating = true;
6610-
}
6611-
(result as any).addedLength = addedLength;
6612-
links?.serializedTypes?.set(key, result as TypeNode as TypeNode & {truncating?: boolean, addedLength: number});
6608+
links?.serializedTypes?.set(key, { node: result, truncating: context.truncating, addedLength });
66136609
}
66146610
context.visitedTypes.delete(typeId);
66156611
if (id) {

src/compiler/factory/nodeFactory.ts

+2-18
Original file line numberDiff line numberDiff line change
@@ -2316,9 +2316,6 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
23162316
const node = createBaseDeclaration<TypeLiteralNode>(SyntaxKind.TypeLiteral);
23172317
node.members = createNodeArray(members);
23182318
node.transformFlags = TransformFlags.ContainsTypeScript;
2319-
2320-
// node.locals = undefined; // initialized by binder (LocalsContainer)
2321-
// node.nextContainer = undefined; // initialized by binder (LocalsContainer)
23222319
return node;
23232320
}
23242321

@@ -2746,8 +2743,6 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
27462743

27472744
node.jsDoc = undefined; // initialized by parser (JsDocContainer)
27482745
node.jsDocCache = undefined; // initialized by parser (JsDocContainer)
2749-
// node.locals = undefined; // initialized by binder (LocalsContainer)
2750-
// node.nextContainer = undefined; // initialized by binder (LocalsContainer)
27512746
return node;
27522747
}
27532748

@@ -3582,8 +3577,6 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
35823577

35833578
node.jsDoc = undefined; // initialized by parser (JsDocContainer)
35843579
node.jsDocCache = undefined; // initialized by parser (JsDocContainer)
3585-
// node.locals = undefined; // initialized by binder (LocalsContainer)
3586-
// node.nextContainer = undefined; // initialized by binder (LocalsContainer)
35873580
return node;
35883581
}
35893582

@@ -4370,8 +4363,6 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
43704363

43714364
node.jsDoc = undefined; // initialized by parser (JsDocContainer)
43724365
node.jsDocCache = undefined; // initialized by parser (JsDocContainer)
4373-
// node.locals = undefined; // initialized by binder (LocalsContainer)
4374-
// node.nextContainer = undefined; // initialized by binder (LocalsContainer)
43754366
return node;
43764367
}
43774368

@@ -4412,8 +4403,6 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
44124403
node.illegalDecorators = undefined; // initialized by parser for grammar errors
44134404
node.jsDoc = undefined; // initialized by parser (JsDocContainer)
44144405
node.jsDocCache = undefined; // initialized by parser (JsDocContainer)
4415-
// node.locals = undefined; // initialized by binder (LocalsContainer)
4416-
// node.nextContainer = undefined; // initialized by binder (LocalsContainer)
44174406
return node;
44184407
}
44194408

@@ -4507,8 +4496,6 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
45074496
node.illegalDecorators = undefined; // initialized by parser for grammar errors
45084497
node.jsDoc = undefined; // initialized by parser (JsDocContainer)
45094498
node.jsDocCache = undefined; // initialized by parser (JsDocContainer)
4510-
// node.locals = undefined; // initialized by binder (LocalsContainer)
4511-
// node.nextContainer = undefined; // initialized by binder (LocalsContainer)
45124499
return node;
45134500
}
45144501

@@ -5139,8 +5126,6 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
51395126
const node = createBaseDeclaration<JSDocTypeLiteral>(SyntaxKind.JSDocTypeLiteral);
51405127
node.jsDocPropertyTags = asNodeArray(propertyTags);
51415128
node.isArrayType = isArrayType;
5142-
// node.locals = undefined; // initialized by binder (LocalsContainer)
5143-
// node.nextContainer = undefined; // initialized by binder (LocalsContainer)
51445129
return node;
51455130
}
51465131

@@ -5735,8 +5720,6 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
57355720
node.transformFlags |=
57365721
propagateChildrenFlags(node.properties) |
57375722
TransformFlags.ContainsJsx;
5738-
// node.locals = undefined; // initialized by binder (LocalsContainer)
5739-
// node.nextContainer = undefined; // initialized by binder (LocalsContainer)
57405723
return node;
57415724
}
57425725

@@ -6094,7 +6077,8 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
60946077
}
60956078

60966079
function cloneSourceFileWorker(source: SourceFile) {
6097-
// TODO: explicit property assignments instead of for..in
6080+
// TODO: This mechanism for cloning results in megamorphic property reads and writes. In future perf-related
6081+
// work, we should consider switching explicit property assignments instead of using `for..in`.
60986082
const node = baseFactory.createBaseSourceFileNode(SyntaxKind.SourceFile) as Mutable<SourceFile>;
60996083
node.flags |= source.flags & ~NodeFlags.Synthesized;
61006084
for (const p in source) {

src/compiler/parser.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1578,7 +1578,7 @@ namespace Parser {
15781578
// Prime the scanner.
15791579
nextToken();
15801580
const pos = getNodePos();
1581-
let statements, endOfFileToken: EndOfFileToken;
1581+
let statements, endOfFileToken;
15821582
if (token() === SyntaxKind.EndOfFileToken) {
15831583
statements = createNodeArray([], pos, pos);
15841584
endOfFileToken = parseTokenNode<EndOfFileToken>();

src/compiler/types.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -5944,12 +5944,19 @@ export interface NodeLinks {
59445944
isExhaustive?: boolean | 0; // Is node an exhaustive switch statement (0 indicates in-process resolution)
59455945
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
59465946
declarationRequiresScopeChange?: boolean; // Set by `useOuterVariableScopeInParameter` in checker when downlevel emit would change the name resolution scope inside of a parameter.
5947-
serializedTypes?: Map<string, TypeNode & {truncating?: boolean, addedLength: number}>; // Collection of types serialized at this location
5947+
serializedTypes?: Map<string, SerializedTypeEntry>; // Collection of types serialized at this location
59485948

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

5953+
/** @internal */
5954+
export interface SerializedTypeEntry {
5955+
node: TypeNode;
5956+
truncating?: boolean;
5957+
addedLength: number;
5958+
}
5959+
59535960
export const enum TypeFlags {
59545961
Any = 1 << 0,
59555962
Unknown = 1 << 1,

0 commit comments

Comments
 (0)