diff --git a/src/index.ts b/src/index.ts index 7755244..7fcf88b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; } diff --git a/src/source-map.ts b/src/source-map.ts index 37c968c..9422143 100644 --- a/src/source-map.ts +++ b/src/source-map.ts @@ -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) => {