Skip to content

Commit d4c2ef3

Browse files
committed
Don't add excess properties to type nodes in typeToTypeNode
1 parent f5c1ca1 commit d4c2ef3

File tree

5 files changed

+16
-29
lines changed

5 files changed

+16
-29
lines changed

src/compiler/binder.ts

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ import {
5757
canHaveJSDoc,
5858
canHaveLocals,
5959
canHaveModifiers,
60-
canUsePropertyAccess,
6160
canHaveSymbol,
61+
canUsePropertyAccess,
6262
cartesianProduct,
6363
CaseBlock,
6464
CaseClause,
@@ -6597,7 +6597,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
65976597
context.truncating = true;
65986598
}
65996599
context.approximateLength += cachedResult.addedLength;
6600-
return deepCloneOrReuseNode(cachedResult) as TypeNode as T;
6600+
return deepCloneOrReuseNode(cachedResult.node) as T;
66016601
}
66026602

66036603
let depth: number | undefined;
@@ -6613,11 +6613,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
66136613
const result = transform(type);
66146614
const addedLength = context.approximateLength - startLength;
66156615
if (!context.reportedDiagnostic && !context.encounteredError) {
6616-
if (context.truncating) {
6617-
(result as any).truncating = true;
6618-
}
6619-
(result as any).addedLength = addedLength;
6620-
links?.serializedTypes?.set(key, result as TypeNode as TypeNode & {truncating?: boolean, addedLength: number});
6616+
links?.serializedTypes?.set(key, { node: result, truncating: context.truncating, addedLength });
66216617
}
66226618
context.visitedTypes.delete(typeId);
66236619
if (id) {

src/compiler/factory/nodeFactory.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,9 +2319,6 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
23192319
const node = createBaseDeclaration<TypeLiteralNode>(SyntaxKind.TypeLiteral);
23202320
node.members = createNodeArray(members);
23212321
node.transformFlags = TransformFlags.ContainsTypeScript;
2322-
2323-
// node.locals = undefined; // initialized by binder (LocalsContainer)
2324-
// node.nextContainer = undefined; // initialized by binder (LocalsContainer)
23252322
return node;
23262323
}
23272324

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

27502747
node.jsDoc = undefined; // initialized by parser (JsDocContainer)
27512748
node.jsDocCache = undefined; // initialized by parser (JsDocContainer)
2752-
// node.locals = undefined; // initialized by binder (LocalsContainer)
2753-
// node.nextContainer = undefined; // initialized by binder (LocalsContainer)
27542749
return node;
27552750
}
27562751

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

35863581
node.jsDoc = undefined; // initialized by parser (JsDocContainer)
35873582
node.jsDocCache = undefined; // initialized by parser (JsDocContainer)
3588-
// node.locals = undefined; // initialized by binder (LocalsContainer)
3589-
// node.nextContainer = undefined; // initialized by binder (LocalsContainer)
35903583
return node;
35913584
}
35923585

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

43744367
node.jsDoc = undefined; // initialized by parser (JsDocContainer)
43754368
node.jsDocCache = undefined; // initialized by parser (JsDocContainer)
4376-
// node.locals = undefined; // initialized by binder (LocalsContainer)
4377-
// node.nextContainer = undefined; // initialized by binder (LocalsContainer)
43784369
return node;
43794370
}
43804371

@@ -4415,8 +4406,6 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
44154406
node.illegalDecorators = undefined; // initialized by parser for grammar errors
44164407
node.jsDoc = undefined; // initialized by parser (JsDocContainer)
44174408
node.jsDocCache = undefined; // initialized by parser (JsDocContainer)
4418-
// node.locals = undefined; // initialized by binder (LocalsContainer)
4419-
// node.nextContainer = undefined; // initialized by binder (LocalsContainer)
44204409
return node;
44214410
}
44224411

@@ -4510,8 +4499,6 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
45104499
node.illegalDecorators = undefined; // initialized by parser for grammar errors
45114500
node.jsDoc = undefined; // initialized by parser (JsDocContainer)
45124501
node.jsDocCache = undefined; // initialized by parser (JsDocContainer)
4513-
// node.locals = undefined; // initialized by binder (LocalsContainer)
4514-
// node.nextContainer = undefined; // initialized by binder (LocalsContainer)
45154502
return node;
45164503
}
45174504

@@ -5142,8 +5129,6 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
51425129
const node = createBaseDeclaration<JSDocTypeLiteral>(SyntaxKind.JSDocTypeLiteral);
51435130
node.jsDocPropertyTags = asNodeArray(propertyTags);
51445131
node.isArrayType = isArrayType;
5145-
// node.locals = undefined; // initialized by binder (LocalsContainer)
5146-
// node.nextContainer = undefined; // initialized by binder (LocalsContainer)
51475132
return node;
51485133
}
51495134

@@ -5738,8 +5723,6 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
57385723
node.transformFlags |=
57395724
propagateChildrenFlags(node.properties) |
57405725
TransformFlags.ContainsJsx;
5741-
// node.locals = undefined; // initialized by binder (LocalsContainer)
5742-
// node.nextContainer = undefined; // initialized by binder (LocalsContainer)
57435726
return node;
57445727
}
57455728

@@ -6097,7 +6080,8 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
60976080
}
60986081

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

src/compiler/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1580,7 +1580,7 @@ namespace Parser {
15801580
// Prime the scanner.
15811581
nextToken();
15821582
const pos = getNodePos();
1583-
let statements, endOfFileToken: EndOfFileToken;
1583+
let statements, endOfFileToken;
15841584
if (token() === SyntaxKind.EndOfFileToken) {
15851585
statements = createNodeArray([], pos, pos);
15861586
endOfFileToken = parseTokenNode<EndOfFileToken>();

src/compiler/types.ts

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

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

5962+
/** @internal */
5963+
export interface SerializedTypeEntry {
5964+
node: TypeNode;
5965+
truncating?: boolean;
5966+
addedLength: number;
5967+
}
5968+
59625969
export const enum TypeFlags {
59635970
Any = 1 << 0,
59645971
Unknown = 1 << 1,

0 commit comments

Comments
 (0)