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

Accept source map input for the visitor #75

Merged
merged 2 commits into from
Dec 27, 2016
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
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