aboutsummaryrefslogtreecommitdiff
path: root/node_modules/istanbul-lib-hook
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/istanbul-lib-hook')
-rw-r--r--node_modules/istanbul-lib-hook/CHANGELOG.md13
-rw-r--r--node_modules/istanbul-lib-hook/lib/hook.js42
-rw-r--r--node_modules/istanbul-lib-hook/package.json2
3 files changed, 54 insertions, 3 deletions
diff --git a/node_modules/istanbul-lib-hook/CHANGELOG.md b/node_modules/istanbul-lib-hook/CHANGELOG.md
index 801bc7039..6083a4886 100644
--- a/node_modules/istanbul-lib-hook/CHANGELOG.md
+++ b/node_modules/istanbul-lib-hook/CHANGELOG.md
@@ -1,7 +1,18 @@
# Change Log
All notable changes to this project will be documented in this file.
-See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
+See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
+
+<a name="1.1.0"></a>
+# [1.1.0](https://github.com/istanbuljs/istanbuljs/compare/istanbul-lib-hook@1.0.7...istanbul-lib-hook@1.1.0) (2017-10-21)
+
+
+### Features
+
+* hook vm.runInContext ([#90](https://github.com/istanbuljs/istanbuljs/issues/90)) ([9659936](https://github.com/istanbuljs/istanbuljs/commit/9659936))
+
+
+
<a name="1.0.7"></a>
## [1.0.7](https://github.com/istanbuljs/istanbuljs/compare/istanbul-lib-hook@1.0.6...istanbul-lib-hook@1.0.7) (2017-05-27)
diff --git a/node_modules/istanbul-lib-hook/lib/hook.js b/node_modules/istanbul-lib-hook/lib/hook.js
index ff1119d8e..df3162fa2 100644
--- a/node_modules/istanbul-lib-hook/lib/hook.js
+++ b/node_modules/istanbul-lib-hook/lib/hook.js
@@ -6,7 +6,8 @@ var path = require('path'),
vm = require('vm'),
appendTransform = require('append-transform'),
originalCreateScript = vm.createScript,
- originalRunInThisContext = vm.runInThisContext;
+ originalRunInThisContext = vm.runInThisContext,
+ originalRunInContext = vm.runInContext;
function transformFn(matcher, transformer, verbose) {
@@ -150,6 +151,43 @@ function unhookRunInThisContext() {
vm.runInThisContext = originalRunInThisContext;
}
/**
+ * hooks `vm.runInContext` to return transformed code.
+ * @method hookRunInContext
+ * @static
+ * @param matcher {Function(filePath)} a function that is called with the filename passed to `vm.createScript`
+ * Should return a truthy value when transformations need to be applied to the code, a falsy value otherwise
+ * @param transformer {Function(code, filePath)} a function called with the original code and the filename passed to
+ * `vm.createScript`. Should return the transformed code.
+ * @param opts {Object} [opts={}] options
+ * @param {Boolean} [options.verbose] write a line to standard error every time the transformer is called
+ */
+function hookRunInContext(matcher, transformer, opts) {
+ opts = opts || {};
+ var fn = transformFn(matcher, transformer, opts.verbose);
+ vm.runInContext = function (code, context, file) {
+ var ret = fn(code, file);
+ var coverageVariable = opts.coverageVariable || '__coverage__';
+ // Refer coverage variable in context to global coverage variable.
+ // So that coverage data will be written in global coverage variable for unit tests run in vm.runInContext.
+ // If all unit tests are run in vm.runInContext, no global coverage variable will be generated.
+ // Thus initialize a global coverage variable here.
+ if (!global[coverageVariable]) {
+ global[coverageVariable] = {};
+ }
+ context[coverageVariable] = global[coverageVariable];
+ return originalRunInContext(ret.code, context, file);
+ };
+
+}
+/**
+ * unhooks vm.runInContext, restoring it to its original state.
+ * @method unhookRunInContext
+ * @static
+ */
+function unhookRunInContext() {
+ vm.runInContext = originalRunInContext;
+}
+/**
* istanbul-lib-hook provides mechanisms to transform code in the scope of `require`,
* `vm.createScript`, `vm.runInThisContext` etc.
*
@@ -177,6 +215,8 @@ module.exports = {
unhookCreateScript: unhookCreateScript,
hookRunInThisContext : hookRunInThisContext,
unhookRunInThisContext : unhookRunInThisContext,
+ hookRunInContext : hookRunInContext,
+ unhookRunInContext : unhookRunInContext,
unloadRequireCache: unloadRequireCache
};
diff --git a/node_modules/istanbul-lib-hook/package.json b/node_modules/istanbul-lib-hook/package.json
index 426989f15..811343d74 100644
--- a/node_modules/istanbul-lib-hook/package.json
+++ b/node_modules/istanbul-lib-hook/package.json
@@ -1,6 +1,6 @@
{
"name": "istanbul-lib-hook",
- "version": "1.0.7",
+ "version": "1.1.0",
"description": "Hooks for require, vm and script used in istanbul",
"author": "Krishnan Anantheswaran <kananthmail-github@yahoo.com>",
"main": "index.js",