Refactor the tracing namespace organization

Also changes the `tracingEnabled.Mode.*` enum to a string union.
This commit is contained in:
Eli Barzilay 2021-02-09 13:16:37 -05:00
parent b7922147d3
commit 4fc9c8446d
3 changed files with 257 additions and 265 deletions

View file

@ -5,16 +5,10 @@ namespace ts { // eslint-disable-line one-namespace-per-file
// should be used as tracing?.___
export let tracing: typeof tracingEnabled | undefined;
// enable the above using startTracing()
}
// `tracingEnabled` should never be used directly, only through the above
/* @internal */
namespace ts.tracingEnabled { // eslint-disable-line one-namespace-per-file
export const enum Mode {
Project,
Build,
Server,
}
// `tracingEnabled` should never be used directly, only through the above
namespace tracingEnabled { // eslint-disable-line one-namespace-per-file
type Mode = "project" | "build" | "server";
let fs: typeof import("fs");
@ -56,8 +50,8 @@ namespace ts.tracingEnabled { // eslint-disable-line one-namespace-per-file
}
const countPart =
mode === Mode.Build ? `.${process.pid}-${++traceCount}`
: mode === Mode.Server ? `.${process.pid}`
mode === "build" ? `.${process.pid}-${++traceCount}`
: mode === "server" ? `.${process.pid}`
: ``;
const tracePath = combinePaths(traceDir, `trace${countPart}.json`);
const typesPath = combinePaths(traceDir, `types${countPart}.json`);
@ -84,7 +78,7 @@ namespace ts.tracingEnabled { // eslint-disable-line one-namespace-per-file
/** Stops tracing for the in-progress project and dumps the type catalog. */
export function stopTracing(typeCatalog?: readonly Type[]) {
Debug.assert(tracing, "Tracing is not in progress");
Debug.assert(!!typeCatalog === (mode !== Mode.Server)); // Have a type catalog iff not in server mode
Debug.assert(!!typeCatalog === (mode !== "server")); // Have a type catalog iff not in server mode
fs.writeSync(traceFd, `\n]\n`);
fs.closeSync(traceFd);
@ -157,7 +151,7 @@ namespace ts.tracingEnabled { // eslint-disable-line one-namespace-per-file
time: number = 1000 * timestamp()) {
// In server mode, there's no easy way to dump type information, so we drop events that would require it.
if (mode === Mode.Server && phase === Phase.CheckTypes) return;
if (mode === "server" && phase === Phase.CheckTypes) return;
performance.mark("beginTracing");
fs.writeSync(traceFd, `,\n{"pid":1,"tid":1,"ph":"${eventType}","cat":"${phase}","ts":${time},"name":"${name}"`);
@ -294,10 +288,8 @@ namespace ts.tracingEnabled { // eslint-disable-line one-namespace-per-file
tracePath: string;
typesPath?: string;
}
}
}
/*@internal*/
namespace ts { // eslint-disable-line one-namespace-per-file
// define after tracingEnabled is initialized
export const startTracing = tracingEnabled.startTracing;
}

View file

@ -666,7 +666,7 @@ namespace ts {
}
if (canTrace(system, compilerOptions)) {
startTracing(isBuildMode ? tracingEnabled.Mode.Build : tracingEnabled.Mode.Project,
startTracing(isBuildMode ? "build" : "project",
compilerOptions.generateTrace!, compilerOptions.configFilePath);
}
}

View file

@ -861,7 +861,7 @@ namespace ts.server {
? stripQuotes(commandLineTraceDir)
: process.env.TSS_TRACE;
if (traceDir) {
startTracing(tracingEnabled.Mode.Server, traceDir);
startTracing("server", traceDir);
}
const ioSession = new IOSession();