Skip to content

Commit

Permalink
fix: don't instrument a file if it has already been instrumented (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe authored and JaKXz committed Apr 28, 2017
1 parent 9f2f18a commit 9c38e4e
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/istanbul-lib-instrument/src/visitor.js
Expand Up @@ -453,6 +453,14 @@ const coverageTemplate = template(`
return coverage[path] = coverageData;
})();
`);
// the rewire plugin (and potentially other babel middleware)
// may cause files to be instrumented twice, see:
// https://github.com/istanbuljs/babel-plugin-istanbul/issues/94
// we should only instrument code for coverage the first time
// it's run through istanbul-lib-instrument.
function alreadyInstrumented(path, visitState) {
return path.scope.hasBinding(visitState.varName);
}
/**
* programVisitor is a `babel` adaptor for instrumentation.
* It returns an object with two methods `enter` and `exit`.
Expand All @@ -478,9 +486,15 @@ function programVisitor(types, sourceFilePath = 'unknown.js', opts = {coverageVa
const visitState = new VisitState(types, sourceFilePath, opts.inputSourceMap);
return {
enter(path) {
if (alreadyInstrumented(path, visitState)) {
return;
}
path.traverse(codeVisitor, visitState);
},
exit(path) {
if (alreadyInstrumented(path, visitState)) {
return;
}
visitState.cov.freeze();
const coverageData = visitState.cov.toJSON();
coverageData[MAGIC_KEY] = MAGIC_VALUE;
Expand Down

0 comments on commit 9c38e4e

Please sign in to comment.