Skip to content

Commit d051190

Browse files
authored
break: Fix source map (#113)
* fix sourcemap line number * fix comment * use espree * fix function name
1 parent 2f6683c commit d051190

File tree

5 files changed

+122
-4
lines changed

5 files changed

+122
-4
lines changed

package-lock.json

+58-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
],
3939
"dependencies": {
4040
"@istanbuljs/load-nyc-config": "^1.1.0",
41+
"espree": "^9.5.2",
4142
"istanbul-lib-instrument": "^5.1.0",
4243
"picocolors": "^1.0.0",
4344
"test-exclude": "^6.0.0"

src/index.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { createInstrumenter } from 'istanbul-lib-instrument';
44
import TestExclude from 'test-exclude';
55
import { loadNycConfig } from '@istanbuljs/load-nyc-config';
66
import picocolors from 'picocolors';
7+
import {createIdentitySourceMap} from "./source-map";
78

89
const { yellow } = picocolors;
910

@@ -181,9 +182,13 @@ export default function istanbulPlugin(opts: IstanbulPluginOptions = {}): Plugin
181182
const filename = resolveFilename(id);
182183

183184
if (testExclude.shouldInstrument(filename)) {
185+
// Create a source map to combine with the source map of previous plugins
186+
instrumenter.instrumentSync(srcCode, filename, createIdentitySourceMap(filename, srcCode))
187+
const map = instrumenter.lastSourceMap();
188+
189+
// Instrument code using the source map of previous plugins
184190
const sourceMap = sanitizeSourceMap(this.getCombinedSourcemap());
185191
const code = instrumenter.instrumentSync(srcCode, filename, sourceMap);
186-
const map = instrumenter.lastSourceMap();
187192

188193
// Required to cast to correct mapping value
189194
return { code, map } as TransformResult;

src/source-map.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as espree from 'espree'
2+
import {SourceMapGenerator} from 'source-map'
3+
4+
5+
export function createIdentitySourceMap(file: string, source: string) {
6+
const gen = new SourceMapGenerator({ file });
7+
const tokens = espree.tokenize(source, { loc: true, ecmaVersion: 'latest' });
8+
9+
tokens.forEach((token: any) => {
10+
const loc = token.loc.start;
11+
gen.addMapping({
12+
source: file,
13+
original: loc,
14+
generated: loc
15+
});
16+
});
17+
18+
return JSON.parse(gen.toString())
19+
}

src/types.d.ts

+38
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,41 @@ declare module 'test-exclude' {
5151

5252
export = TestExclude;
5353
}
54+
55+
declare module 'espree' {
56+
// https://github.com/eslint/espree#options
57+
export interface Options {
58+
comment?: boolean;
59+
ecmaFeatures?: {
60+
globalReturn?: boolean;
61+
impliedStrict?: boolean;
62+
jsx?: boolean;
63+
};
64+
ecmaVersion?:
65+
| 3
66+
| 5
67+
| 6
68+
| 7
69+
| 8
70+
| 9
71+
| 10
72+
| 11
73+
| 12
74+
| 2015
75+
| 2016
76+
| 2017
77+
| 2018
78+
| 2019
79+
| 2020
80+
| 2021
81+
| 2022
82+
| 2023
83+
| 'latest';
84+
loc?: boolean;
85+
range?: boolean;
86+
sourceType?: 'script' | 'module';
87+
tokens?: boolean;
88+
}
89+
// https://github.com/eslint/espree#tokenize
90+
export function tokenize(code: string, options?: Options): any;
91+
}

0 commit comments

Comments
 (0)