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

feat: add a sources option allowing to bypass fs operations #36

Merged
merged 1 commit into from
Jun 23, 2019
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
12 changes: 11 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@

import { Profiler } from 'inspector'
import { CoverageMapData } from 'istanbul-lib-coverage'
import { RawSourceMap } from 'source-map'

declare type Sources =
| {
source: string
}
| {
source: string
originalSource: string
sourceMap: { sourcemap: RawSourceMap }
}
declare class V8ToIstanbul {
load(): Promise<void>
applyCoverage(blocks: ReadonlyArray<Profiler.FunctionCoverage>): void
toIstanbul(): CoverageMapData
}

declare function v8ToIstanbul(scriptPath: string, wrapperLength?: number): V8ToIstanbul
declare function v8ToIstanbul(scriptPath: string, wrapperLength?: number, sources?: Sources): V8ToIstanbul

export = v8ToIstanbul
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const V8ToIstanbul = require('./lib/v8-to-istanbul')

module.exports = function (path, wrapperLength) {
return new V8ToIstanbul(path, wrapperLength)
module.exports = function (path, wrapperLength, sources) {
return new V8ToIstanbul(path, wrapperLength, sources)
}
23 changes: 16 additions & 7 deletions lib/v8-to-istanbul.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ const isNode10 = !!process.version.match(/^v10/)
const cjsWrapperLength = isNode10 ? require('module').wrapper[0].length : 0

module.exports = class V8ToIstanbul {
constructor (scriptPath, wrapperLength) {
constructor (scriptPath, wrapperLength, sources) {
assert(typeof scriptPath === 'string', 'scriptPath must be a string')
this.path = parsePath(scriptPath)
this.wrapperLength = wrapperLength === undefined ? cjsWrapperLength : wrapperLength
this.sources = sources || {}
this.generatedLines = []
this.branches = []
this.functions = []
Expand All @@ -27,11 +28,13 @@ module.exports = class V8ToIstanbul {
this.sourceTranspiled = undefined
}
async load () {
const rawSource = readFileSync(this.path, 'utf8')
// if we find a source-map (either inline, or a .map file) we load
// both the transpiled and original source, both of which are used during
// the backflips we perform to remap absolute to relative positions.
const rawSourceMap = convertSourceMap.fromSource(rawSource) || convertSourceMap.fromMapFileSource(rawSource, dirname(this.path))
const rawSource = this.sources.source || readFileSync(this.path, 'utf8')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When this.sources.source is a zero-length string, it still tries to read an actual file. I guess you meant

this.sources.source === undefined ? readFileSync(this.path, 'utf8') : this.sources.source

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call!

FWIW, I'm still not happy with the branching logic I introduced in this PR, I'd prefer an explicit .loadFromFs and .loadFromMemory or something API. Haven't had time to dig further into this yet though

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Separate APIs really sound good to me.

const rawSourceMap = this.sources.sourceMap ||
// if we find a source-map (either inline, or a .map file) we load
// both the transpiled and original source, both of which are used during
// the backflips we perform to remap absolute to relative positions.
convertSourceMap.fromSource(rawSource) || convertSourceMap.fromMapFileSource(rawSource, dirname(this.path))

if (rawSourceMap) {
if (rawSourceMap.sourcemap.sources.length > 1) {
console.warn('v8-to-istanbul: source-mappings from one to many files not yet supported')
Expand All @@ -44,7 +47,13 @@ module.exports = class V8ToIstanbul {
}
this.sourceMap = await new SourceMapConsumer(rawSourceMap.sourcemap)

const originalRawSource = readFileSync(this.path, 'utf8')
let originalRawSource
if (this.sources.originalSource) {
originalRawSource = this.sources.originalSource
} else {
originalRawSource = readFileSync(this.path, 'utf8')
}

this.source = new CovSource(originalRawSource, this.wrapperLength)
this.sourceTranspiled = new CovSource(rawSource, this.wrapperLength)
}
Expand Down