From ca709ede5ac0d633f5fec7a6e1e2c40a4db3c48e Mon Sep 17 00:00:00 2001 From: Bob Pace Date: Mon, 7 Jul 2014 11:48:47 -0600 Subject: added __with__ function according to proposed syntax in issue #29 --- lib/__set__.js | 19 ++++++++++++++++++- lib/rewire.js | 10 +++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/__set__.js b/lib/__set__.js index 0fa88ac..ad0e5b0 100644 --- a/lib/__set__.js +++ b/lib/__set__.js @@ -46,4 +46,21 @@ function __set__() { }; } -module.exports = __set__; +function __with__() { + var args = arguments; + return function(callback) { + if (typeof callback !== "function") { + throw new TypeError("__with__ expects a callback function") + } + + var undo = __set__.apply(null, args) + try { + callback(); + } + finally { + undo(); + } + } +} + +module.exports = {"__set__": __set__, "__with__": __with__} diff --git a/lib/rewire.js b/lib/rewire.js index 3daacef..61160fd 100644 --- a/lib/rewire.js +++ b/lib/rewire.js @@ -1,13 +1,16 @@ var Module = require("module"), fs = require("fs"), __get__ = require("./__get__.js"), - __set__ = require("./__set__.js"), + setModule = require ("./__set__.js"), + __set__ = setModule["__set__"], + __with__ = setModule["__with__"], getImportGlobalsSrc = require("./getImportGlobalsSrc.js"), detectStrictMode = require("./detectStrictMode.js"), moduleEnv = require("./moduleEnv.js"); var __get__Src = __get__.toString(), - __set__Src = __set__.toString(); + __set__Src = __set__.toString(), + __with_Src = __with__.toString(); /** * Does actual rewiring the module. For further documentation @see index.js @@ -44,6 +47,7 @@ function internalRewire(parentModulePath, targetPath) { appendix = "\n"; appendix += "module.exports.__set__ = " + __set__Src + "; "; appendix += "module.exports.__get__ = " + __get__Src + "; "; + appendix += "module.exports.__with__ = " + __with_Src + "; "; // Check if the module uses the strict mode. // If so we must ensure that "use strict"; stays at the beginning of the module. @@ -58,4 +62,4 @@ function internalRewire(parentModulePath, targetPath) { return targetModule.exports; } -module.exports = internalRewire; \ No newline at end of file +module.exports = internalRewire; -- cgit v1.2.3