/** * hooks `vm.runInThisContext` to return transformed code. * @method hookRunInThisContext * @static * @param matcher {Function(filePath)} a function that is called with the filename passed to `vm.runInThisContext` * Should return a truthy value when transformations need to be applied to the code, a falsy value otherwise * @param transformer {Function(code, options)} a function called with the original code and the filename passed to * `vm.runInThisContext`. Should return the transformed code. * @param opts {Object} [opts={}] options * @param {Boolean} [opts.verbose] write a line to standard error every time the transformer is called */ function hookRunInThisContext(matcher, transformer, opts) { opts = opts || {}; const fn = transformFn(matcher, transformer, opts.verbose); vm.runInThisContext = function(code, options) { const ret = fn(code, options); return originalRunInThisContext(ret.code, options); }; }