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

Add produceSourceMap option #393

Closed
wants to merge 5 commits into from
Closed
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ and a `text-lcov` coverage report.
nyc --reporter=lcov --reporter=text-lcov npm test
```

## Stack Traces

When `source-map` handling enabled, then the instrumented source files will
include inline source maps for the instrumenter transform. When combined with
[source-map-support](https://github.com/evanw/node-source-map-support),
stack traces for instrumented code should reflect the original lines.

This is enabled by default, but may be disabled by `nyc --source-map=false`.

## Support for custom require hooks (babel, webpack, etc.)

nyc supports custom require hooks like
Expand Down
28 changes: 23 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var rimraf = require('rimraf')
var onExit = require('signal-exit')
var resolveFrom = require('resolve-from')
var convertSourceMap = require('convert-source-map')
var mergeSourceMap = require('merge-source-map')
var md5hex = require('md5-hex')
var findCacheDir = require('find-cache-dir')
var js = require('default-require-extensions/js')
Expand Down Expand Up @@ -120,7 +121,9 @@ NYC.prototype.instrumenter = function () {
}

NYC.prototype._createInstrumenter = function () {
return this._instrumenterLib(this.cwd)
return this._instrumenterLib(this.cwd, {
produceSourceMap: this._sourceMap ? 'inline' : ''
})
}

NYC.prototype.addFile = function (filename) {
Expand Down Expand Up @@ -250,14 +253,30 @@ NYC.prototype._transformFactory = function (cacheDir) {

return function (code, metadata, hash) {
var filename = metadata.filename
var sourceMap

if (_this._sourceMap) {
sourceMap = convertSourceMap.fromSource(code) || convertSourceMap.fromMapFileSource(code, path.dirname(filename))

if (_this._sourceMap) _this._handleSourceMap(cacheDir, code, hash, filename)
_this._handleSourceMap(cacheDir, code, hash, filename, sourceMap)
}

try {
instrumented = instrumenter.instrumentSync(code, filename)

var lastSourceMap = instrumenter.lastSourceMap()
if (lastSourceMap) {
if (sourceMap) {
lastSourceMap = mergeSourceMap(
sourceMap.toObject(),
lastSourceMap)
}

instrumented += '\n' + convertSourceMap.fromObject(lastSourceMap).toComment()
}
} catch (e) {
// don't fail external tests due to instrumentation bugs.
console.warn('failed to instrument', filename, 'with error:', e.message)
console.warn('failed to instrument', filename, 'with error:', e.stack)
instrumented = code
}

Expand All @@ -269,8 +288,7 @@ NYC.prototype._transformFactory = function (cacheDir) {
}
}

NYC.prototype._handleSourceMap = function (cacheDir, code, hash, filename) {
var sourceMap = convertSourceMap.fromSource(code) || convertSourceMap.fromMapFileSource(code, path.dirname(filename))
NYC.prototype._handleSourceMap = function (cacheDir, code, hash, filename, sourceMap) {
if (sourceMap) {
if (hash) {
var mapPath = path.join(cacheDir, hash + '.map')
Expand Down
5 changes: 3 additions & 2 deletions lib/instrumenters/istanbul.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
function InstrumenterIstanbul (cwd) {
function InstrumenterIstanbul (cwd, options) {
Copy link
Member

Choose a reason for hiding this comment

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

We should 'use strict'; at the top of these files.

var istanbul = InstrumenterIstanbul.istanbul()

return istanbul.createInstrumenter({
autoWrap: true,
coverageVariable: '__coverage__',
embedSource: true,
noCompact: false,
preserveComments: true
preserveComments: true,
produceSourceMap: options.produceSourceMap
})
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"istanbul-lib-source-maps": "^1.0.1",
"istanbul-reports": "^1.0.0-alpha.8",
"md5-hex": "^1.2.0",
"merge-source-map": "^1.0.2",
"micromatch": "^2.3.11",
"mkdirp": "^0.5.0",
"pkg-up": "^1.0.0",
Expand All @@ -112,8 +113,8 @@
"requirejs": "^2.3.0",
"sanitize-filename": "^1.5.3",
"sinon": "^1.15.3",
"source-map-support": "^0.4.2",
"split-lines": "^1.0.0",
"source-map-support": "^0.4.6",
"standard": "^8.0.0",
"standard-version": "^2.4.0",
"tap": "^7.0.0",
Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/stack-trace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

function blah() {
throw new Error('Blarrh')
}

var stack;
try {
blah();
} catch(err) {
stack = err.stack;
}

module.exports = function() {
return stack;
}
22 changes: 21 additions & 1 deletion test/src/nyc-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global describe, it */

require('source-map-support').install()
require('source-map-support').install({hookRequire: true})
var _ = require('lodash')
var ap = require('any-path')
var configUtil = require('../../lib/config-util')
Expand Down Expand Up @@ -202,6 +202,26 @@ describe('nyc', function () {
})
})

describe('produce source map', function () {
it('handles stack traces', function () {
var nyc = new NYC(configUtil.loadConfig())
nyc.reset()
nyc.wrap()

var check = require('../fixtures/stack-trace')
check().should.match(/stack-trace.js:4:/)
})

it('does not handle stack traces when disabled', function () {
var nyc = new NYC(configUtil.loadConfig(['--source-map=false']))
nyc.reset()
nyc.wrap()

var check = require('../fixtures/stack-trace')
check().should.match(/stack-trace.js:1:/)
})
})

describe('compile handlers for custom extensions are assigned', function () {
it('assigns a function to custom extensions', function () {
var nyc = new NYC(configUtil.loadConfig([],
Expand Down