Skip to content

Tracing: dump more information about types #42962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 51 additions & 12 deletions src/compiler/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,22 @@ namespace ts { // eslint-disable-line one-namespace-per-file
performance.measure("Tracing", "beginTracing", "endTracing");
}

function indexFromOne(lc: LineAndCharacter): LineAndCharacter {
return {
line: lc.line + 1,
character: lc.character + 1,
};
function getLocation(node: Node | undefined) {
const file = getSourceFileOfNode(node);
return !file
? undefined
: {
path: file.path,
start: indexFromOne(getLineAndCharacterOfPosition(file, node!.pos)),
end: indexFromOne(getLineAndCharacterOfPosition(file, node!.end)),
};

function indexFromOne(lc: LineAndCharacter): LineAndCharacter {
return {
line: lc.line + 1,
character: lc.character + 1,
};
}
}

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

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

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

let substitutionProperties: object = {};
if (type.flags & TypeFlags.Substitution) {
const substitutionType = type as SubstitutionType;
substitutionProperties = {
substitutionBaseType: substitutionType.baseType?.id,
substituteType: substitutionType.substitute?.id,
};
}

let reverseMappedProperties: object = {};
if (objectFlags & ObjectFlags.ReverseMapped) {
const reverseMappedType = type as ReverseMappedType;
reverseMappedProperties = {
reverseMappedSourceType: reverseMappedType.source?.id,
reverseMappedMappedType: reverseMappedType.mappedType?.id,
reverseMappedConstraintType: reverseMappedType.constraintType?.id,
};
}

let evolvingArrayProperties: object = {};
if (objectFlags & ObjectFlags.EvolvingArray) {
const evolvingArrayType = type as EvolvingArrayType;
evolvingArrayProperties = {
evolvingArrayElementType: evolvingArrayType.elementType.id,
evolvingArrayFinalType: evolvingArrayType.finalArrayType?.id,
};
}

// We can't print out an arbitrary object, so just assign each one a unique number.
// Don't call it an "id" so people don't treat it as a type id.
let recursionToken: number | undefined;
Expand All @@ -245,18 +283,19 @@ namespace ts { // eslint-disable-line one-namespace-per-file
intrinsicName: (type as any).intrinsicName,
symbolName: symbol?.escapedName && unescapeLeadingUnderscores(symbol.escapedName),
recursionId: recursionToken,
isTuple: objectFlags & ObjectFlags.Tuple ? true : undefined,
unionTypes: (type.flags & TypeFlags.Union) ? (type as UnionType).types?.map(t => t.id) : undefined,
intersectionTypes: (type.flags & TypeFlags.Intersection) ? (type as IntersectionType).types.map(t => t.id) : undefined,
aliasTypeArguments: type.aliasTypeArguments?.map(t => t.id),
keyofType: (type.flags & TypeFlags.Index) ? (type as IndexType).type?.id : undefined,
...indexedAccessProperties,
...referenceProperties,
...conditionalProperties,
firstDeclaration: firstDeclaration && {
path: firstFile.path,
start: indexFromOne(getLineAndCharacterOfPosition(firstFile, firstDeclaration.pos)),
end: indexFromOne(getLineAndCharacterOfPosition(getSourceFileOfNode(firstDeclaration), firstDeclaration.end)),
},
...substitutionProperties,
...reverseMappedProperties,
...evolvingArrayProperties,
destructuringPattern: getLocation(type.pattern),
firstDeclaration: getLocation(symbol?.declarations?.[0]),
flags: Debug.formatTypeFlags(type.flags).split("|"),
display,
};
Expand Down