Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ensure identity source map has the same number of fields as the combined source map #122

Merged
merged 2 commits into from
Jul 22, 2023
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
18 changes: 12 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,20 @@ export default function istanbulPlugin(opts: IstanbulPluginOptions = {}): Plugin
const filename = resolveFilename(id);

if (testExclude.shouldInstrument(filename)) {
// Create a source map to combine with the source map of previous plugins
instrumenter.instrumentSync(srcCode, filename, createIdentitySourceMap(filename, srcCode))
// Instrument code using the combined source map of previous plugins
const combinedSourceMap = sanitizeSourceMap(this.getCombinedSourcemap());
const code = instrumenter.instrumentSync(srcCode, filename, combinedSourceMap);

// Create an identity source map with the same number of fields as the combined source map
const identitySourceMap = sanitizeSourceMap(createIdentitySourceMap(filename, srcCode, {
file: combinedSourceMap.file,
sourceRoot: combinedSourceMap.sourceRoot
}));

// Create a result source map to combine with the source maps of previous plugins
instrumenter.instrumentSync(srcCode, filename, identitySourceMap);
const map = instrumenter.lastSourceMap();

// Instrument code using the source map of previous plugins
const sourceMap = sanitizeSourceMap(this.getCombinedSourcemap());
const code = instrumenter.instrumentSync(srcCode, filename, sourceMap);

// Required to cast to correct mapping value
return { code, map } as TransformResult;
}
Expand Down
7 changes: 4 additions & 3 deletions src/source-map.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as espree from 'espree'
import {SourceMapGenerator} from 'source-map'
import {SourceMapGenerator, StartOfSourceMap} from 'source-map'


export function createIdentitySourceMap(file: string, source: string) {
const gen = new SourceMapGenerator();
// Create a source map which always maps to the same line and column
export function createIdentitySourceMap(file: string, source: string, option: StartOfSourceMap) {
const gen = new SourceMapGenerator(option);
const tokens = espree.tokenize(source, { loc: true, ecmaVersion: 'latest' });

tokens.forEach((token: any) => {
Expand Down