aboutsummaryrefslogtreecommitdiff
path: root/node_modules/istanbul-lib-instrument/dist/visitor.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/istanbul-lib-instrument/dist/visitor.js')
-rw-r--r--node_modules/istanbul-lib-instrument/dist/visitor.js14
1 files changed, 14 insertions, 0 deletions
diff --git a/node_modules/istanbul-lib-instrument/dist/visitor.js b/node_modules/istanbul-lib-instrument/dist/visitor.js
index 518b1e31e..beae5d91b 100644
--- a/node_modules/istanbul-lib-instrument/dist/visitor.js
+++ b/node_modules/istanbul-lib-instrument/dist/visitor.js
@@ -482,6 +482,14 @@ var codeVisitor = {
};
// the template to insert at the top of the program.
var coverageTemplate = (0, _babelTemplate2.default)('\n var COVERAGE_VAR = (function () {\n var path = PATH,\n hash = HASH,\n global = (new Function(\'return this\'))(),\n gcv = GLOBAL_COVERAGE_VAR,\n coverageData = INITIAL,\n coverage = global[gcv] || (global[gcv] = {});\n if (coverage[path] && coverage[path].hash === hash) {\n return coverage[path];\n }\n coverageData.hash = hash;\n return coverage[path] = coverageData;\n })();\n');
+// 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`.
@@ -510,9 +518,15 @@ function programVisitor(types) {
var visitState = new VisitState(types, sourceFilePath, opts.inputSourceMap);
return {
enter: function enter(path) {
+ if (alreadyInstrumented(path, visitState)) {
+ return;
+ }
path.traverse(codeVisitor, visitState);
},
exit: function exit(path) {
+ if (alreadyInstrumented(path, visitState)) {
+ return;
+ }
visitState.cov.freeze();
var coverageData = visitState.cov.toJSON();
coverageData[_constants.MAGIC_KEY] = _constants.MAGIC_VALUE;