Compare commits

...

2 commits

Author SHA1 Message Date
Nathan Shively-Sanders 416323d967 Merge branch 'main' into dump-variances 2021-10-26 08:06:24 -07:00
Nathan Shively-Sanders 18b0fc0040 Dump variance to stdout 2021-10-21 14:57:14 -07:00
4 changed files with 37 additions and 2 deletions

View file

@ -621,6 +621,7 @@ namespace ts {
return tryFindAmbientModule(moduleName, /*withAugmentations*/ false);
},
getApparentType,
dumpVariances,
getUnionType,
isTypeAssignableTo,
createAnonymousType,
@ -3999,7 +4000,7 @@ namespace ts {
typeCount++;
result.id = typeCount;
if (produceDiagnostics) { // Only record types from one checker
tracing?.recordType(result);
tracingEnabled.recordType(result);
}
return result;
}
@ -20209,6 +20210,24 @@ namespace ts {
return variances;
}
function getVariancesUltra(type: GenericType, refid: number): { variances: string[], how: 'static' | 'check' | 'dump', refid: number, targetid: number } {
// Arrays and tuples are known to be covariant, no need to spend time computing this.
if (type === globalArrayType || type === globalReadonlyArrayType || type.objectFlags & ObjectFlags.Tuple) {
return { variances: arrayVariances.map(translateEnum), how: 'static', targetid: type.id, refid };
}
return {
how: type.variances ? 'check' : 'dump',
variances: getVariancesWorker(type.typeParameters, type, getMarkerTypeReference).map(translateEnum),
targetid: type.id,
refid,
}
}
function translateEnum(flag: VarianceFlags): string {
return flag.toString() // laters! (once I find out the distribution)
}
function getVariances(type: GenericType): VarianceFlags[] {
// Arrays and tuples are known to be covariant, no need to spend time computing this.
if (type === globalArrayType || type === globalReadonlyArrayType || type.objectFlags & ObjectFlags.Tuple) {
@ -40303,6 +40322,14 @@ namespace ts {
return diagnostics.getDiagnostics();
}
function dumpVariances() {
tracingEnabled.iterTypes(t => {
if (getObjectFlags(t) & ObjectFlags.Reference) {
console.log(JSON.stringify(getVariancesUltra((t as TypeReference).target, t.id)))
}
})
}
function getGlobalDiagnostics(): Diagnostic[] {
throwIfNonDiagnosticsProducing();
return diagnostics.getGlobalDiagnostics();

View file

@ -7,7 +7,7 @@ namespace ts { // eslint-disable-line one-namespace-per-file
// enable the above using startTracing()
// `tracingEnabled` should never be used directly, only through the above
namespace tracingEnabled { // eslint-disable-line one-namespace-per-file
export namespace tracingEnabled { // eslint-disable-line one-namespace-per-file
type Mode = "project" | "build" | "server";
let fs: typeof import("fs");
@ -103,6 +103,12 @@ namespace ts { // eslint-disable-line one-namespace-per-file
}
}
export function iterTypes(action: (t: Type) => void): void {
for (const t of typeCatalog) {
action(t)
}
}
export const enum Phase {
Parse = "parse",
Program = "program",

View file

@ -4152,6 +4152,7 @@ namespace ts {
}
export interface TypeChecker {
dumpVariances(): void;
getTypeOfSymbolAtLocation(symbol: Symbol, node: Node): Type;
getDeclaredTypeOfSymbol(symbol: Symbol): Type;
getPropertiesOfType(type: Type): Symbol[];

View file

@ -327,6 +327,7 @@ namespace ts {
if (allDiagnostics.length === configFileParsingDiagnosticsLength) {
addRange(allDiagnostics, program.getSemanticDiagnostics(/*sourceFile*/ undefined, cancellationToken));
(program as Program).getTypeChecker().dumpVariances();
}
}
}