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
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
"dependencies": {
"@use-tusk/drift-core-node": "^0.1.8",
"acorn": "^8.14.0",
"acorn-typescript": "^1.4.13",
"ast-v8-to-istanbul": "^1.0.0",
"import-in-the-middle": "^1.14.4",
"js-yaml": "^4.1.0",
Expand Down
39 changes: 26 additions & 13 deletions src/core/coverageProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ function resolveSourceCode(scriptPath: string, projectRoot: string): {
}
}

// Fallback: try reading the .ts file directly (won't parse with acorn,
// but let it fail gracefully so processV8CoverageFile skips it)
// Fallback: read the .ts file directly — acorn-typescript can parse it.
// This handles ts-node, ts-node-dev, and --experimental-strip-types.
const code = fs.readFileSync(scriptPath, "utf-8");
return { code, resolvedPath: scriptPath, sourceMap: null };
}
Expand Down Expand Up @@ -226,21 +226,34 @@ export async function processV8CoverageFile(

// Try parsing as script first (CJS), fall back to module (ESM).
// Track which succeeded — CJS modules have a V8 wrapper that shifts byte offsets.
let ast;
// For .ts/.tsx files run via --experimental-strip-types, use acorn-typescript
// plugin since acorn can't parse TypeScript syntax natively.
let isCJS = false;
const isTypeScript = /\.(ts|tsx|mts|cts)$/.test(scriptPath);
const parserOptions: Record<string, unknown> = {
ecmaVersion: "latest",
locations: true,
};

// Resolve the parser: use acorn-typescript for .ts files, plain acorn for .js
let parser = acorn;
if (isTypeScript) {
try {
const { tsPlugin } = require("acorn-typescript");
parser = acorn.Parser.extend(tsPlugin()) as typeof acorn;
} catch {
// acorn-typescript not available — plain acorn will be used
// (may fail for TS files, but the outer try/catch handles that)
}
}

// Try script (CJS) first, fall back to module (ESM)
let ast;
try {
ast = acorn.parse(code, {
ecmaVersion: "latest",
sourceType: "script",
locations: true,
});
ast = parser.parse(code, { ...parserOptions, sourceType: "script" });
isCJS = true;
} catch {
ast = acorn.parse(code, {
ecmaVersion: "latest",
sourceType: "module",
locations: true,
});
ast = parser.parse(code, { ...parserOptions, sourceType: "module" });
}

// Strip sourceMappingURL from code passed to convert() — we already loaded
Expand Down
Loading