Skip to content

Commit

Permalink
fix: Ensure identity source map has the same number of fields as the …
Browse files Browse the repository at this point in the history
…combined source map (#122)

* fix: source map should have the same number of optional fields
* fix
  • Loading branch information
kota65535 authored and iFaxity committed Jul 22, 2023
1 parent 10978b0 commit bc194fb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
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

0 comments on commit bc194fb

Please sign in to comment.