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: 9 additions & 1 deletion packages/istanbul-lib-instrument/src/instrumenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`) */
Expand Down Expand Up @@ -115,6 +122,7 @@ class Instrumenter {
ignoreClassMethods: opts.ignoreClassMethods,
ignoreLines: opts.ignoreLines,
inputSourceMap,
storeInputSourceMap: opts.inputSourceMap !== false,
});

if (!output) {
Expand Down
9 changes: 8 additions & 1 deletion packages/istanbul-lib-instrument/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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} */
Expand Down Expand Up @@ -207,6 +209,7 @@ class VisitState {
ignoreClassMethods: string[] = [],
reportLogic = false,
ignoreLines = false,
storeInputSourceMap = true,
) {
this.code = code;
this.s = s;
Expand All @@ -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);
}
Expand Down Expand Up @@ -957,6 +963,7 @@ function instrumentProgram(
visitorOpts.ignoreClassMethods,
visitorOpts.reportLogic,
visitorOpts.ignoreLines,
visitorOpts.storeInputSourceMap,
);

// the rewire plugin (and potentially other middleware)
Expand Down
57 changes: 57 additions & 0 deletions packages/istanbul-lib-instrument/test/varia.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down
Loading