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!: Fix source map #113

Merged
merged 4 commits into from
Jun 18, 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
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' {
iFaxity marked this conversation as resolved.
Show resolved Hide resolved
// 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;
}