From 83c12e2f91eeecb4be55297c2653626d99169e51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Wed, 21 Oct 2026 13:28:59 +0300 Subject: [PATCH] feat(lib-instrument): add `inputSourceMap` option --- .../src/instrumenter.ts | 10 +++- .../istanbul-lib-instrument/src/visitor.ts | 9 ++- .../test/varia.test.ts | 57 +++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/packages/istanbul-lib-instrument/src/instrumenter.ts b/packages/istanbul-lib-instrument/src/instrumenter.ts index 4505876..9bdc2d6 100644 --- a/packages/istanbul-lib-instrument/src/instrumenter.ts +++ b/packages/istanbul-lib-instrument/src/instrumenter.ts @@ -39,9 +39,16 @@ export interface InstrumenterOptions { * combine the produced source map with the input source map (default `true`). * Set to `false` to get a source map of the instrumentation step only, e.g. for * bundler plugins whose source maps are combined by the bundler. - * The input source map is still stored in the coverage data. + * The input source map is still stored in the coverage data, see `inputSourceMap`. */ combineSourceMaps: boolean; + /** + * store the input source map in the coverage data (default `true`). + * The coverage data is embedded into the instrumented code, so the source map + * is a large part of it. When set to `false`, the source map has to be registered + * to the `MapStore` with `registerMap` before transforming the coverage. + */ + inputSourceMap: boolean; /** set to array of class method names to ignore for coverage */ ignoreClassMethods: string[]; /** enable ignore hints for lines (start, stop) (default `false`) */ @@ -115,6 +122,7 @@ class Instrumenter { ignoreClassMethods: opts.ignoreClassMethods, ignoreLines: opts.ignoreLines, inputSourceMap, + storeInputSourceMap: opts.inputSourceMap !== false, }); if (!output) { diff --git a/packages/istanbul-lib-instrument/src/visitor.ts b/packages/istanbul-lib-instrument/src/visitor.ts index 3c70569..7ae691e 100644 --- a/packages/istanbul-lib-instrument/src/visitor.ts +++ b/packages/istanbul-lib-instrument/src/visitor.ts @@ -47,6 +47,8 @@ export interface VisitorOptions { ignoreLines: boolean; /** the input source map, mapping the uninstrumented code back to the original code */ inputSourceMap?: InputSourceMap; + /** store the input source map in the coverage data (default `true`) */ + storeInputSourceMap?: boolean; } /** the result returned by {@link instrumentProgram} */ @@ -207,6 +209,7 @@ class VisitState { ignoreClassMethods: string[] = [], reportLogic = false, ignoreLines = false, + storeInputSourceMap = true, ) { this.code = code; this.s = s; @@ -216,8 +219,11 @@ class VisitState { this.cov = new SourceCoverage(sourceFilePath); if (typeof inputSourceMap !== "undefined") { - this.cov.inputSourceMap(inputSourceMap); + if (storeInputSourceMap) { + this.cov.inputSourceMap(inputSourceMap); + } + // ignore hints are read from the sources, so the source map is needed even when not stored if (ignoreLines) { this.traceMap = new TraceMap(inputSourceMap); } @@ -957,6 +963,7 @@ function instrumentProgram( visitorOpts.ignoreClassMethods, visitorOpts.reportLogic, visitorOpts.ignoreLines, + visitorOpts.storeInputSourceMap, ); // the rewire plugin (and potentially other middleware) diff --git a/packages/istanbul-lib-instrument/test/varia.test.ts b/packages/istanbul-lib-instrument/test/varia.test.ts index 1c1c539..f5a2ac2 100644 --- a/packages/istanbul-lib-instrument/test/varia.test.ts +++ b/packages/istanbul-lib-instrument/test/varia.test.ts @@ -179,6 +179,63 @@ describe("varia", () => { }); }); + describe("inputSourceMap", () => { + const original = "// header\n// header\nconst value = 1;\nconsole.log(value);\n"; + const input = new MagicString(original); + input.remove(0, original.indexOf("const")); + const code = input.toString(); + const inputSourceMap = { + ...input.generateMap({ hires: true, source: "original.js", includeContent: true }), + version: 3 as const, + }; + + function instrument(store?: boolean) { + const instrumenter = new Instrumenter({ inputSourceMap: store }); + const generated = instrumenter.instrumentSync(code, "input.js", inputSourceMap); + return { generated, coverage: instrumenter.lastFileCoverage() }; + } + + it("stores the whole source map by default", () => { + const { generated, coverage } = instrument(); + + expect(coverage.inputSourceMap).toBe(inputSourceMap); + expect(generated).toContain("// header"); + }); + + it("stores the whole source map when enabled", () => { + const { coverage } = instrument(true); + + expect(coverage.inputSourceMap).toBe(inputSourceMap); + }); + + it("stores no source map when disabled", () => { + const { generated, coverage } = instrument(false); + + expect(coverage).not.toHaveProperty("inputSourceMap"); + expect(generated).not.toContain(inputSourceMap.mappings); + expect(generated).not.toContain("// header"); + }); + + it("uses the sources content of the source map for ignore hints when disabled", () => { + const source = + "/* istanbul ignore start */\nconst a = 1;\n/* istanbul ignore stop */\nconst b = 2;\n"; + const transformed = new MagicString(source); + transformed.remove(0, source.indexOf("const")); + const map = { + ...transformed.generateMap({ hires: true, source: "original.js", includeContent: true }), + version: 3 as const, + }; + + const instrumenter = new Instrumenter({ inputSourceMap: false, ignoreLines: true }); + instrumenter.instrumentSync(transformed.toString(), "input.js", map); + const coverage = instrumenter.lastFileCoverage(); + + expect(coverage).not.toHaveProperty("inputSourceMap"); + expect(Object.keys(coverage.statementMap)).toHaveLength(1); + expect(coverage.statementMap[0].start.line).toBe(3); + }); + }); + it("registers source map URLs seen in the original source", () => { let f = ""; let u = "";