Skip to content

Commit 74db34e

Browse files
committed
Store tracked symbols when caching type so that it is handled by declaration emitter to add imports
1 parent 6d58d32 commit 74db34e

File tree

4 files changed

+21
-137
lines changed

4 files changed

+21
-137
lines changed

src/compiler/checker.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,7 @@ import {
987987
tokenToString,
988988
tracing,
989989
TracingNode,
990+
TrackedSymbol,
990991
TransientSymbol,
991992
TransientSymbolLinks,
992993
tryAddToSet,
@@ -6256,7 +6257,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
62566257
visitedTypes: undefined,
62576258
symbolDepth: undefined,
62586259
inferTypeParameters: undefined,
6259-
approximateLength: 0
6260+
approximateLength: 0,
6261+
trackedSymbols: undefined!,
62606262
};
62616263
context.tracker = new SymbolTrackerImpl(context, tracker, moduleResolverHost);
62626264
const resultingNode = cb(context);
@@ -6709,6 +6711,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
67096711
}
67106712
const cachedResult = links?.serializedTypes?.get(key);
67116713
if (cachedResult) {
6714+
// TODO:: check if we instead store late painted statements associated with this?
6715+
cachedResult.trackedSymbols?.forEach(
6716+
([symbol, enclosingDeclaration, meaning]) =>
6717+
context.tracker.trackSymbol(symbol, enclosingDeclaration, meaning)
6718+
);
67126719
if (cachedResult.truncating) {
67136720
context.truncating = true;
67146721
}
@@ -6729,7 +6736,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
67296736
const result = transform(type);
67306737
const addedLength = context.approximateLength - startLength;
67316738
if (!context.reportedDiagnostic && !context.encounteredError) {
6732-
links?.serializedTypes?.set(key, { node: result, truncating: context.truncating, addedLength });
6739+
links?.serializedTypes?.set(key, { node: result, truncating: context.truncating, addedLength, trackedSymbols: context.trackedSymbols });
67336740
}
67346741
context.visitedTypes.delete(typeId);
67356742
if (id) {
@@ -8477,6 +8484,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
84778484
if (context.reportedDiagnostic) {
84788485
oldcontext.reportedDiagnostic = context.reportedDiagnostic; // hoist diagnostic result into outer context
84798486
}
8487+
if (context.trackedSymbols) {
8488+
if (!oldContext.trackedSymbols) oldContext.trackedSymbols = context.trackedSymbols;
8489+
else oldContext.trackedSymbols.push(...context.trackedSymbols);
8490+
}
84808491
context = oldContext;
84818492
}
84828493
}
@@ -48352,6 +48363,7 @@ interface NodeBuilderContext {
4835248363
// State
4835348364
encounteredError: boolean;
4835448365
reportedDiagnostic: boolean;
48366+
trackedSymbols: TrackedSymbol[] | undefined;
4835548367
visitedTypes: Set<number> | undefined;
4835648368
symbolDepth: Map<string, number> | undefined;
4835748369
inferTypeParameters: TypeParameter[] | undefined;
@@ -48386,6 +48398,7 @@ class SymbolTrackerImpl implements SymbolTracker {
4838648398
}
4838748399

4838848400
trackSymbol(symbol: Symbol, enclosingDeclaration: Node | undefined, meaning: SymbolFlags): boolean {
48401+
(this.context.trackedSymbols ??= []).push([symbol, enclosingDeclaration, meaning]);
4838948402
if (this.inner?.trackSymbol && !this.disableTrackSymbol) {
4839048403
if (this.inner.trackSymbol(symbol, enclosingDeclaration, meaning)) {
4839148404
this.onDiagnosticReported();

src/compiler/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5997,11 +5997,14 @@ export interface NodeLinks {
59975997
decoratorSignature?: Signature; // Signature for decorator as if invoked by the runtime.
59985998
}
59995999

6000+
/** @internal */
6001+
export type TrackedSymbol = [symbol: Symbol, enclosingDeclaration: Node | undefined, meaning: SymbolFlags];
60006002
/** @internal */
60016003
export interface SerializedTypeEntry {
60026004
node: TypeNode;
60036005
truncating?: boolean;
60046006
addedLength: number;
6007+
trackedSymbols: readonly TrackedSymbol[] | undefined;
60056008
}
60066009

60076010
export const enum TypeFlags {

tests/baselines/reference/tsc/incremental/generates-typerefs-correctly-discrepancies.js

Lines changed: 0 additions & 127 deletions
This file was deleted.

tests/baselines/reference/tsc/incremental/generates-typerefs-correctly.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ export const bug: W.Wrap<{
239239
n: B.Box<number>;
240240
}>;
241241
export const something: 1;
242+
import * as B from "./box.js";
242243
import * as W from "./wrap.js";
243244

244245

@@ -268,7 +269,7 @@ exports.something = 1;
268269

269270

270271
//// [/src/project/outDir/tsconfig.tsbuildinfo]
271-
{"program":{"fileNames":["../../../lib/lib.d.ts","../src/box.ts","../src/wrap.ts","../src/bug.js"],"fileInfos":[{"version":"3858781397-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\ndeclare const console: { log(msg: any): void; };","affectsGlobalScope":true},{"version":"-14267342128-export interface Box<T> {\n unbox(): T\n}\n","signature":"-15554117365-export interface Box<T> {\n unbox(): T;\n}\n"},{"version":"-7208318765-export type Wrap<C> = {\n [K in keyof C]: { wrapped: C[K] }\n}\n","signature":"-7604652776-export type Wrap<C> = {\n [K in keyof C]: {\n wrapped: C[K];\n };\n};\n"},{"version":"-25729561895-import * as B from \"./box.js\"\nimport * as W from \"./wrap.js\"\n\n/**\n * @template {object} C\n * @param {C} source\n * @returns {W.Wrap<C>}\n */\nconst wrap = source => {\nthrow source\n}\n\n/**\n * @returns {B.Box<number>}\n */\nconst box = (n = 0) => ({ unbox: () => n })\n\nexport const bug = wrap({ n: box(1) });\nexport const something = 1;","signature":"-7681488146-export const bug: W.Wrap<{\n n: B.Box<number>;\n}>;\nexport const something: 1;\nimport * as B from \"./box.js\";\nimport * as W from \"./wrap.js\";\n"}],"root":[[2,4]],"options":{"composite":true,"outDir":"./"},"fileIdsList":[[2,3]],"referencedMap":[[4,1]],"exportedModulesMap":[],"semanticDiagnosticsPerFile":[1,2,4,3],"emitSignatures":[[4,"1273570373-export const bug: W.Wrap<{\n n: B.Box<number>;\n}>;\nexport const something: 1;\nimport * as W from \"./wrap.js\";\n"]],"latestChangedDtsFile":"./src/bug.d.ts"},"version":"FakeTSVersion"}
272+
{"program":{"fileNames":["../../../lib/lib.d.ts","../src/box.ts","../src/wrap.ts","../src/bug.js"],"fileInfos":[{"version":"3858781397-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\ndeclare const console: { log(msg: any): void; };","affectsGlobalScope":true},{"version":"-14267342128-export interface Box<T> {\n unbox(): T\n}\n","signature":"-15554117365-export interface Box<T> {\n unbox(): T;\n}\n"},{"version":"-7208318765-export type Wrap<C> = {\n [K in keyof C]: { wrapped: C[K] }\n}\n","signature":"-7604652776-export type Wrap<C> = {\n [K in keyof C]: {\n wrapped: C[K];\n };\n};\n"},{"version":"-25729561895-import * as B from \"./box.js\"\nimport * as W from \"./wrap.js\"\n\n/**\n * @template {object} C\n * @param {C} source\n * @returns {W.Wrap<C>}\n */\nconst wrap = source => {\nthrow source\n}\n\n/**\n * @returns {B.Box<number>}\n */\nconst box = (n = 0) => ({ unbox: () => n })\n\nexport const bug = wrap({ n: box(1) });\nexport const something = 1;","signature":"-7681488146-export const bug: W.Wrap<{\n n: B.Box<number>;\n}>;\nexport const something: 1;\nimport * as B from \"./box.js\";\nimport * as W from \"./wrap.js\";\n"}],"root":[[2,4]],"options":{"composite":true,"outDir":"./"},"fileIdsList":[[2,3]],"referencedMap":[[4,1]],"exportedModulesMap":[],"semanticDiagnosticsPerFile":[1,2,4,3],"latestChangedDtsFile":"./src/bug.d.ts"},"version":"FakeTSVersion"}
272273

273274
//// [/src/project/outDir/tsconfig.tsbuildinfo.readable.baseline.txt]
274275
{
@@ -350,15 +351,9 @@ exports.something = 1;
350351
"../src/bug.js",
351352
"../src/wrap.ts"
352353
],
353-
"emitSignatures": [
354-
[
355-
"../src/bug.js",
356-
"1273570373-export const bug: W.Wrap<{\n n: B.Box<number>;\n}>;\nexport const something: 1;\nimport * as W from \"./wrap.js\";\n"
357-
]
358-
],
359354
"latestChangedDtsFile": "./src/bug.d.ts"
360355
},
361356
"version": "FakeTSVersion",
362-
"size": 1894
357+
"size": 1738
363358
}
364359

0 commit comments

Comments
 (0)