Skip to content

Commit 41b5abf

Browse files
authored
Tracing: dump more information about types (microsoft#42962)
* Tracing: dump more info about substitution types * Tracing: dump more info about reverse-mapped types * Tracing: dump more info about destructuring types * Tracing: dump more info about evolving-array types * Tracing: dump more info about tuple types * Tracing: dump more info about type references * Fix lint errors * Tracing: extract getLocation helper
1 parent c44b3ba commit 41b5abf

File tree

1 file changed

+51
-12
lines changed

1 file changed

+51
-12
lines changed

src/compiler/tracing.ts

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,22 @@ namespace ts { // eslint-disable-line one-namespace-per-file
162162
performance.measure("Tracing", "beginTracing", "endTracing");
163163
}
164164

165-
function indexFromOne(lc: LineAndCharacter): LineAndCharacter {
166-
return {
167-
line: lc.line + 1,
168-
character: lc.character + 1,
169-
};
165+
function getLocation(node: Node | undefined) {
166+
const file = getSourceFileOfNode(node);
167+
return !file
168+
? undefined
169+
: {
170+
path: file.path,
171+
start: indexFromOne(getLineAndCharacterOfPosition(file, node!.pos)),
172+
end: indexFromOne(getLineAndCharacterOfPosition(file, node!.end)),
173+
};
174+
175+
function indexFromOne(lc: LineAndCharacter): LineAndCharacter {
176+
return {
177+
line: lc.line + 1,
178+
character: lc.character + 1,
179+
};
180+
}
170181
}
171182

172183
function dumpTypes(types: readonly Type[]) {
@@ -185,8 +196,6 @@ namespace ts { // eslint-disable-line one-namespace-per-file
185196
const type = types[i];
186197
const objectFlags = (type as any).objectFlags;
187198
const symbol = type.aliasSymbol ?? type.symbol;
188-
const firstDeclaration = symbol?.declarations?.[0];
189-
const firstFile = firstDeclaration && getSourceFileOfNode(firstDeclaration);
190199

191200
// It's slow to compute the display text, so skip it unless it's really valuable (or cheap)
192201
let display: string | undefined;
@@ -214,6 +223,7 @@ namespace ts { // eslint-disable-line one-namespace-per-file
214223
referenceProperties = {
215224
instantiatedType: referenceType.target?.id,
216225
typeArguments: referenceType.resolvedTypeArguments?.map(t => t.id),
226+
referenceLocation: getLocation(referenceType.node),
217227
};
218228
}
219229

@@ -228,6 +238,34 @@ namespace ts { // eslint-disable-line one-namespace-per-file
228238
};
229239
}
230240

241+
let substitutionProperties: object = {};
242+
if (type.flags & TypeFlags.Substitution) {
243+
const substitutionType = type as SubstitutionType;
244+
substitutionProperties = {
245+
substitutionBaseType: substitutionType.baseType?.id,
246+
substituteType: substitutionType.substitute?.id,
247+
};
248+
}
249+
250+
let reverseMappedProperties: object = {};
251+
if (objectFlags & ObjectFlags.ReverseMapped) {
252+
const reverseMappedType = type as ReverseMappedType;
253+
reverseMappedProperties = {
254+
reverseMappedSourceType: reverseMappedType.source?.id,
255+
reverseMappedMappedType: reverseMappedType.mappedType?.id,
256+
reverseMappedConstraintType: reverseMappedType.constraintType?.id,
257+
};
258+
}
259+
260+
let evolvingArrayProperties: object = {};
261+
if (objectFlags & ObjectFlags.EvolvingArray) {
262+
const evolvingArrayType = type as EvolvingArrayType;
263+
evolvingArrayProperties = {
264+
evolvingArrayElementType: evolvingArrayType.elementType.id,
265+
evolvingArrayFinalType: evolvingArrayType.finalArrayType?.id,
266+
};
267+
}
268+
231269
// We can't print out an arbitrary object, so just assign each one a unique number.
232270
// Don't call it an "id" so people don't treat it as a type id.
233271
let recursionToken: number | undefined;
@@ -245,18 +283,19 @@ namespace ts { // eslint-disable-line one-namespace-per-file
245283
intrinsicName: (type as any).intrinsicName,
246284
symbolName: symbol?.escapedName && unescapeLeadingUnderscores(symbol.escapedName),
247285
recursionId: recursionToken,
286+
isTuple: objectFlags & ObjectFlags.Tuple ? true : undefined,
248287
unionTypes: (type.flags & TypeFlags.Union) ? (type as UnionType).types?.map(t => t.id) : undefined,
249288
intersectionTypes: (type.flags & TypeFlags.Intersection) ? (type as IntersectionType).types.map(t => t.id) : undefined,
250289
aliasTypeArguments: type.aliasTypeArguments?.map(t => t.id),
251290
keyofType: (type.flags & TypeFlags.Index) ? (type as IndexType).type?.id : undefined,
252291
...indexedAccessProperties,
253292
...referenceProperties,
254293
...conditionalProperties,
255-
firstDeclaration: firstDeclaration && {
256-
path: firstFile.path,
257-
start: indexFromOne(getLineAndCharacterOfPosition(firstFile, firstDeclaration.pos)),
258-
end: indexFromOne(getLineAndCharacterOfPosition(getSourceFileOfNode(firstDeclaration), firstDeclaration.end)),
259-
},
294+
...substitutionProperties,
295+
...reverseMappedProperties,
296+
...evolvingArrayProperties,
297+
destructuringPattern: getLocation(type.pattern),
298+
firstDeclaration: getLocation(symbol?.declarations?.[0]),
260299
flags: Debug.formatTypeFlags(type.flags).split("|"),
261300
display,
262301
};

0 commit comments

Comments
 (0)