Skip to content

Commit

Permalink
break: Fix source map (#113)
Browse files Browse the repository at this point in the history
* fix sourcemap line number

* fix comment

* use espree

* fix function name
  • Loading branch information
kota65535 authored and iFaxity committed Jul 22, 2023
1 parent be19850 commit 93601b6
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 4 deletions.
61 changes: 58 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
],
"dependencies": {
"@istanbuljs/load-nyc-config": "^1.1.0",
"espree": "^9.5.2",
"istanbul-lib-instrument": "^5.1.0",
"picocolors": "^1.0.0",
"test-exclude": "^6.0.0"
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createInstrumenter } from 'istanbul-lib-instrument';
import TestExclude from 'test-exclude';
import { loadNycConfig } from '@istanbuljs/load-nyc-config';
import picocolors from 'picocolors';
import {createIdentitySourceMap} from "./source-map";

const { yellow } = picocolors;

Expand Down Expand Up @@ -181,9 +182,13 @@ 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))
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);
const map = instrumenter.lastSourceMap();

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


export function createIdentitySourceMap(file: string, source: string) {
const gen = new SourceMapGenerator({ file });
const tokens = espree.tokenize(source, { loc: true, ecmaVersion: 'latest' });

tokens.forEach((token: any) => {
const loc = token.loc.start;
gen.addMapping({
source: file,
original: loc,
generated: loc
});
});

return JSON.parse(gen.toString())
}
38 changes: 38 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,41 @@ declare module 'test-exclude' {

export = TestExclude;
}

declare module 'espree' {
// https://github.com/eslint/espree#options
export interface Options {
comment?: boolean;
ecmaFeatures?: {
globalReturn?: boolean;
impliedStrict?: boolean;
jsx?: boolean;
};
ecmaVersion?:
| 3
| 5
| 6
| 7
| 8
| 9
| 10
| 11
| 12
| 2015
| 2016
| 2017
| 2018
| 2019
| 2020
| 2021
| 2022
| 2023
| 'latest';
loc?: boolean;
range?: boolean;
sourceType?: 'script' | 'module';
tokens?: boolean;
}
// https://github.com/eslint/espree#tokenize
export function tokenize(code: string, options?: Options): any;
}

0 comments on commit 93601b6

Please sign in to comment.