Skip to content

Commit

Permalink
Switch to TraceMap
Browse files Browse the repository at this point in the history
[trace-mapping](https://github.com/jridgewell/trace-mapping) is faster, smaller, and lighter than the `source-map` package, and doesn't require WASM, manual memory management, or async/await to use.

Fixes istanbuljs#185.
  • Loading branch information
jridgewell committed Apr 20, 2022
1 parent 1f357fa commit aeb6477
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const v8toIstanbul = require('v8-to-istanbul')
// the path to the original source-file is required, as its contents are
// used during the conversion algorithm.
const converter = v8toIstanbul('./path-to-instrumented-file.js')
await converter.load() // this is required due to the async source-map dependency.
await converter.load() // this is required due to async file reading.
// provide an array of coverage information in v8 format.
converter.applyCoverage([
{
Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { Profiler } from 'inspector'
import { CoverageMapData } from 'istanbul-lib-coverage'
import { RawSourceMap } from 'source-map'
import { SourceMapInput } from '@jridgewell/trace-mapping'

declare type Sources =
| {
Expand All @@ -11,7 +11,7 @@ declare type Sources =
| {
source: string
originalSource: string
sourceMap: { sourcemap: RawSourceMap }
sourceMap: { sourcemap: SourceMapInput }
}
declare class V8ToIstanbul {
load(): Promise<void>
Expand Down
16 changes: 8 additions & 8 deletions lib/source.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const CovLine = require('./line')
const { sliceRange } = require('./range')
const { GREATEST_LOWER_BOUND, LEAST_UPPER_BOUND } = require('source-map').SourceMapConsumer
const { originalPositionFor, generatedPositionFor, GREATEST_LOWER_BOUND, LEAST_UPPER_BOUND } = require('@jridgewell/trace-mapping')

module.exports = class CovSource {
constructor (sourceRaw, wrapperLength) {
Expand Down Expand Up @@ -102,7 +102,7 @@ module.exports = class CovSource {
}

if (start.line === end.line && start.column === end.column) {
end = sourceMap.originalPositionFor({
end = originalPositionFor(sourceMap, {
line: lines[lines.length - 1].line,
column: endCol - lines[lines.length - 1].startCol,
bias: LEAST_UPPER_BOUND
Expand Down Expand Up @@ -168,7 +168,7 @@ function originalEndPositionFor (sourceMap, line, column) {
// for mappings in the original-order sorted list, this will find the
// mapping that corresponds to the one immediately after the
// beforeEndMapping mapping.
const afterEndMapping = sourceMap.generatedPositionFor({
const afterEndMapping = generatedPositionFor(sourceMap, {
source: beforeEndMapping.source,
line: beforeEndMapping.line,
column: beforeEndMapping.column + 1,
Expand All @@ -181,7 +181,7 @@ function originalEndPositionFor (sourceMap, line, column) {
// If these don't match, it means that the call to
// 'generatedPositionFor' didn't find any other original mappings on
// the line we gave, so consider the binding to extend to infinity.
sourceMap.originalPositionFor(afterEndMapping).line !==
originalPositionFor(sourceMap, afterEndMapping).line !==
beforeEndMapping.line
) {
return {
Expand All @@ -192,17 +192,17 @@ function originalEndPositionFor (sourceMap, line, column) {
}

// Convert the end mapping into the real original position.
return sourceMap.originalPositionFor(afterEndMapping)
return originalPositionFor(sourceMap, afterEndMapping)
}

function originalPositionTryBoth (sourceMap, line, column) {
let original = sourceMap.originalPositionFor({
let original = originalPositionFor(sourceMap, {
line,
column,
bias: GREATEST_LOWER_BOUND
})
if (original.line === null) {
original = sourceMap.originalPositionFor({
original = originalPositionFor(sourceMap, {
line,
column,
bias: LEAST_UPPER_BOUND
Expand All @@ -220,7 +220,7 @@ function originalPositionTryBoth (sourceMap, line, column) {
// } else { ... }
// ^ ^
// l5 l3
const min = sourceMap.originalPositionFor({
const min = originalPositionFor(sourceMap, {
line,
column: 0,
bias: GREATEST_LOWER_BOUND
Expand Down
11 changes: 4 additions & 7 deletions lib/v8-to-istanbul.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ try {
} catch (_err) {
// most likely we're on an older version of Node.js.
}
const { SourceMapConsumer } = require('source-map')
const { TraceMap } = require('@jridgewell/trace-mapping')
const isOlderNode10 = /^v10\.(([0-9]\.)|(1[0-5]\.))/u.test(process.version)
const isNode8 = /^v8\./.test(process.version)

Expand Down Expand Up @@ -53,7 +53,7 @@ module.exports = class V8ToIstanbul {

if (this.rawSourceMap) {
if (this.rawSourceMap.sourcemap.sources.length > 1) {
this.sourceMap = await new SourceMapConsumer(this.rawSourceMap.sourcemap)
this.sourceMap = new TraceMap(this.rawSourceMap.sourcemap)
if (!this.sourceMap.sourcesContent) {
this.sourceMap.sourcesContent = await this.sourcesContentFromSources()
}
Expand All @@ -62,7 +62,7 @@ module.exports = class V8ToIstanbul {
} else {
const candidatePath = this.rawSourceMap.sourcemap.sources.length >= 1 ? this.rawSourceMap.sourcemap.sources[0] : this.rawSourceMap.sourcemap.file
this.path = this._resolveSource(this.rawSourceMap, candidatePath || this.path)
this.sourceMap = await new SourceMapConsumer(this.rawSourceMap.sourcemap)
this.sourceMap = new TraceMap(this.rawSourceMap.sourcemap)

let originalRawSource
if (this.sources.sourceMap && this.sources.sourceMap.sourcemap && this.sources.sourceMap.sourcemap.sourcesContent && this.sources.sourceMap.sourcemap.sourcesContent.length === 1) {
Expand Down Expand Up @@ -102,10 +102,7 @@ module.exports = class V8ToIstanbul {
}

destroy () {
if (this.sourceMap) {
this.sourceMap.destroy()
this.sourceMap = undefined
}
this.sourceMap = undefined
}

_resolveSource (rawSourceMap, sourcePath) {
Expand Down
50 changes: 47 additions & 3 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@
"author": "Ben Coe <ben@npmjs.com>",
"license": "ISC",
"dependencies": {
"@jridgewell/trace-mapping": "0.3.6",
"@types/istanbul-lib-coverage": "^2.0.1",
"convert-source-map": "^1.6.0",
"source-map": "^0.7.3"
"convert-source-map": "^1.6.0"
},
"devDependencies": {
"@types/node": "^16.0.0",
"c8": "^7.2.1",
"semver": "^7.3.2",
"should": "13.2.3",
"source-map": "^0.7.3",
"standard": "^16.0.4",
"tap": "^15.1.6"
},
Expand Down
6 changes: 3 additions & 3 deletions test/source.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global describe, it */

const CovSource = require('../lib/source')
const { SourceMapConsumer } = require('source-map')
const { TraceMap } = require('@jridgewell/trace-mapping')

require('tap').mochaGlobals()
require('should')
Expand Down Expand Up @@ -45,7 +45,7 @@ describe('Source', () => {
new CovSource(sourceRaw, 0).should.ok()
})

it('range crossing two sourcemaps', async () => {
it('range crossing two sourcemaps', () => {
const sourceRaw = `\
(() => {
// hello.ts
Expand All @@ -59,7 +59,7 @@ describe('Source', () => {
//# sourceMappingURL=greet.js.map\
`
const source = new CovSource(sourceRaw, 0)
const sourceMap = await new SourceMapConsumer({
const sourceMap = new TraceMap({
version: 3,
sources: ['../hello.ts', '../greet.ts'],
sourcesContent: ['export function hello() {\r\n console.log("hello world")\r\n}', 'import {hello} from "./hello"\r\n\r\nhello()\r\n'],
Expand Down

0 comments on commit aeb6477

Please sign in to comment.