Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ export default [
],
},
...baseConfig,
{
rules: {
"unicorn/no-array-for-each": "off",
},
},
];
34 changes: 17 additions & 17 deletions src/core/precompile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,38 +61,38 @@ export async function precompile(
assert(matchedTestFiles.size > 0, "No matched testname");
}

const sourceFunctions = new Map<string, SourceFunctionInfo[]>();
let sourceFunctions: Map<string, SourceFunctionInfo[]> | undefined = undefined;
if (collectCoverage) {
const sourceCodePaths = getRelatedFiles(includes, excludes, (path: string) => !path.endsWith(".test.ts"));
const sourceTransformFunction = join(projectRoot, "transform", "listFunctions.mjs");
// The batchSize = 2 is empirical data after benchmarking
const batchSize = 2;
for (let i = 0; i < sourceCodePaths.length; i += batchSize) {
await Promise.all(
sourceCodePaths.slice(i, i + batchSize).map((sourcePath) =>
transform(sourceTransformFunction, sourcePath, flags, () => {
sourceFunctions.set(sourcePath, functionInfos);
})
)
);
}
globalThis.__functionInfos = undefined;
sourceFunctions = await transform(sourceTransformFunction, sourceCodePaths, flags, () => __functionInfos);
}

return {
testCodePaths: matchedTestFiles.size > 0 ? Array.from(matchedTestFiles) : testCodePaths,
matchedTestNames,
sourceFunctions,
sourceFunctions: sourceFunctions || new Map<string, SourceFunctionInfo[]>(),
};
}

async function transform(transformFunction: string, codePath: string, flags: string, collectCallback: () => void) {
let ascArgv = [codePath, "--noEmit", "--disableWarning", "--transform", transformFunction, "-O0"];
async function transform<T>(
transformFunction: string,
codePath: string | string[],
flags: string,
collectCallback: () => T
) {
let ascArgv = ["--noEmit", "--disableWarning", "--transform", transformFunction, "-O0"];
if (typeof codePath === "string") {
ascArgv.push(codePath);
} else {
ascArgv.push(...codePath);
}
if (flags) {
const argv = flags.split(" ");
ascArgv = ascArgv.concat(argv);
}
await ascMain(ascArgv, true);
collectCallback();
return collectCallback();
}

// a. include in config
Expand Down
1 change: 0 additions & 1 deletion src/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ export class Parser {
this.functionCovTraceMap.set(name, originTraces.concat(traces));
}
const lineInfoMap: LineInfoMap = new Map();
// eslint-disable-next-line unicorn/no-array-for-each
info.lineInfo.forEach((ranges: LineRange | null, index: number) => {
// If instrument return basic block == null, ignore it.
if (ranges === null) return;
Expand Down
7 changes: 5 additions & 2 deletions src/type/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { SourceFunctionInfo } from "../interface.ts";

declare global {
// why use var: [Remove block-scoped bindings from globalThis](https://github.com/microsoft/TypeScript/issues/30547)
// store listFunctions transform results in global
let functionInfos: SourceFunctionInfo[];
// eslint-disable-next-line no-var
var __functionInfos: Map<string, SourceFunctionInfo[]> | undefined;
// store listTestNames transform results in global
let testNames: string[];
// eslint-disable-next-line no-var
var testNames: string[];
}

export {};
17 changes: 11 additions & 6 deletions transform/listFunctions.mts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,17 @@ class SourceFunctionTransform extends Transform {
afterInitialize(program: Program) {
this.#elementsByDeclaration = program.elementsByDeclaration;
// There will be two sources with SourceKind.UserEntry, ~lib/rt/index-incremental.ts should be filtered
const entrySource = program.sources.find(
(source) => source.sourceKind === SourceKind.UserEntry && !source.normalizedPath.startsWith("~lib/")
);
this.visitNode(entrySource);
this.functionInfos.reverse();
globalThis.functionInfos = this.functionInfos;
program.sources
.filter((source) => source.sourceKind === SourceKind.UserEntry && !source.normalizedPath.startsWith("~lib/"))
.forEach((source) => {
this.functionInfos = [];
this.visitNode(source);
this.functionInfos.reverse();
const functionInfos =
(globalThis.__functionInfos as Map<string, SourceFunctionInfo[]>) || new Map<string, SourceFunctionInfo[]>();
functionInfos.set(source.normalizedPath, this.functionInfos);
globalThis.__functionInfos = functionInfos;
});
throw new Error("TransformDone");
}

Expand Down