Skip to content

Commit

Permalink
feat: accept source map input for the visitor (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbay authored and bcoe committed Dec 27, 2016
1 parent 7bd4eb8 commit 437e90b
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,40 @@ If you don't provide options in your Babel config, the plugin will look for `exc

You can also use [istanbul's ignore hints](https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignoring-code-for-coverage-purposes) to specify specific lines of code to skip instrumenting.

## Source Maps

By default, this plugin will pick up inline source maps and attach them to the instrumented code such that code coverage can be remapped back to the original source, even for multi-step build processes. This can be memory intensive. Set `useInlineSourceMaps` to prevent this behavior.

```json
{
"env": {
"test": {
"plugins": [
["istanbul", {
"useInlineSourceMaps": false
}]
]
}
}
}
```

If you're instrumenting code programatically, you can pass a source map explicitly.
```js
import babelPluginIstanbul from 'babel-plugin-istanbul';

function instrument(sourceCode, sourceMap, fileName) {
return babel.transform(sourceCode, {
filename,
plugins: [
[babelPluginIstanbul, {
inputSourceMap: sourceMap
}]
]
})
}
```

## Credit where credit is due

The approach used in `babel-plugin-istanbul` was inspired by [Thai Pangsakulyanont](https://github.com/dtinth)'s original library [`babel-plugin-__coverage__`](https://github.com/dtinth/babel-plugin-__coverage__).
12 changes: 12 additions & 0 deletions fixtures/has-inline-source-map.js

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

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
"prepublish": "npm test && npm run release",
"version": "standard-version"
},
"standard": {
"ignore": [
"fixtures/has-inline-source-map.js"
]
},
"repository": {
"type": "git",
"url": "git+https://github.com/istanbuljs/babel-plugin-istanbul.git"
Expand Down
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ function makeVisitor ({types: t}) {
if (shouldSkip(realPath, this.opts)) {
return
}
this.__dv__ = programVisitor(t, realPath)
let { inputSourceMap } = this.opts
if (this.opts.useInlineSourceMaps !== false) {
inputSourceMap = inputSourceMap || this.file.opts.inputSourceMap
}
this.__dv__ = programVisitor(t, realPath, {
coverageVariable: '__coverage__',
inputSourceMap
})
this.__dv__.enter(path)
},
exit (path) {
Expand Down
37 changes: 37 additions & 0 deletions test/babel-plugin-istanbul.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,43 @@ describe('babel-plugin-istanbul', function () {
})
})

context('source maps', function () {
it('should use inline source map', function () {
var result = babel.transformFileSync('./fixtures/has-inline-source-map.js', {
plugins: [
[makeVisitor({types: babel.types}), {
include: ['fixtures/has-inline-source-map.js']
}]
]
})
result.code.should.match(/inputSourceMap/)
})

it('should not use inline source map if inputSourceMap is set to false', function () {
var result = babel.transformFileSync('./fixtures/has-inline-source-map.js', {
plugins: [
[makeVisitor({types: babel.types}), {
include: ['fixtures/has-inline-source-map.js'],
useInlineSourceMaps: false
}]
]
})
result.code.should.not.match(/inputSourceMap/)
})

it('should use provided source map', function () {
var result = babel.transformFileSync('./fixtures/has-inline-source-map.js', {
plugins: [
[makeVisitor({types: babel.types}), {
include: ['fixtures/has-inline-source-map.js'],
inputSourceMap: { asdfQwer: 'foo' }
}]
]
})
result.code.should.match(/inputSourceMap:\s*{\s*asdfQwer: "foo"\s*}/)
})
})

context('package.json "nyc" config', function () {
it('should instrument file if shouldSkip returns false', function () {
var result = babel.transformFileSync('./fixtures/should-cover.js', {
Expand Down

0 comments on commit 437e90b

Please sign in to comment.