diff --git a/scripts/lib/risk-coverage.ts b/scripts/lib/risk-coverage.ts index 9a12908a..719c5890 100644 --- a/scripts/lib/risk-coverage.ts +++ b/scripts/lib/risk-coverage.ts @@ -1,12 +1,17 @@ -import { constants, type Dirent, type Stats } from "node:fs"; import { - lstat, - open, - readdir, - realpath, -} from "node:fs/promises"; + closeSync, + constants, + type Dirent, + fstatSync, + lstatSync, + openSync, + readdirSync, + readFileSync, + realpathSync, + type Stats, +} from "node:fs"; import path from "node:path"; -import babelParser from "@babel/eslint-parser"; +import { parseSync } from "@babel/core"; import { COVERAGE_SUMMARY_METRICS as coverageMetrics, @@ -426,19 +431,24 @@ export function hasCoverageCounterBearingStatements( source: string, relativePath: string, ): boolean { + // Babel's own parser and AST. @babel/eslint-parser ran the same parser and + // then converted the whole tree and its tokens to ESTree, about 2.8 times + // the cost over the repository, and the top-level statement types and + // flags read here are the same in both trees. Every production module is + // parsed once per inventory, and the inventory is built in the gate and in + // its tests. let parsed: unknown; try { - parsed = babelParser.parse(source, { + parsed = parseSync(source, { + filename: relativePath, + babelrc: false, + configFile: false, sourceType: "module", - requireConfigFile: false, - filePath: relativePath, - babelOptions: { - parserOpts: { - plugins: [ - "typescript", - ...(relativePath.endsWith(".tsx") ? ["jsx"] : []), - ], - }, + parserOpts: { + plugins: [ + "typescript", + ...(relativePath.endsWith(".tsx") ? (["jsx"] as const) : []), + ], }, }); } catch (error) { @@ -446,10 +456,11 @@ export function hasCoverageCounterBearingStatements( cause: error, }); } - if (!isRecord(parsed) || !Array.isArray(parsed.body)) { + const program = isRecord(parsed) ? parsed.program : undefined; + if (!isRecord(program) || !Array.isArray(program.body)) { throw new TypeError("TypeScript parser returned an invalid program"); } - return parsed.body.some(statementIsCoverageCounterBearing); + return program.body.some(statementIsCoverageCounterBearing); } export async function buildProductionModuleInventory( @@ -457,12 +468,25 @@ export async function buildProductionModuleInventory( ): Promise { const repositoryRoot = path.resolve(options.repositoryRoot ?? process.cwd()); const sourceRoot = path.join(repositoryRoot, "src"); + // The default file system makes the same calls in the same order as the + // fs/promises one, synchronously. The walk awaits about six calls per + // module one after another; through the thread pool each waited for an + // event-loop turn, and on a loaded CI machine that wait, not the work, made + // the inventory take seconds. Injected file systems keep the async shape. const readDirectory = options.readDirectory ?? - ((target: string) => readdir(target, { withFileTypes: true })); - const lstatPath = options.lstatPath ?? lstat; - const realpathPath = options.realpathPath ?? realpath; + (async (target: string) => readdirSync(target, { withFileTypes: true })); + const lstatPath = options.lstatPath ?? (async (target: string) => lstatSync(target)); + const realpathPath = options.realpathPath ?? + (async (target: string) => realpathSync.native(target)); const openFile = options.openFile ?? - ((target: string, flags: number) => open(target, flags)); + (async (target: string, flags: number): Promise => { + const descriptor = openSync(target, flags); + return { + stat: async () => fstatSync(descriptor), + readFile: async (encoding) => readFileSync(descriptor, encoding), + close: async () => closeSync(descriptor), + }; + }); const generatedPaths = uniquePaths(options.generatedPaths ?? [], "generatedPaths"); const generated = new Set(generatedPaths); const repositoryRealpath = await realpathPath(repositoryRoot);