Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/twbs/rewire.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBob Pace <bob.pace@gmail.com>2014-07-07 21:48:47 +0400
committerBob Pace <bob.pace@gmail.com>2014-07-07 21:48:47 +0400
commitca709ede5ac0d633f5fec7a6e1e2c40a4db3c48e (patch)
tree64492c7455ef2287c75b27563dca5fec1bc45e2d /lib
parent74dcfece99ff75e36a4ea544885f7aab888a82cf (diff)
added __with__ function according to proposed syntax in issue #29
Diffstat (limited to 'lib')
-rw-r--r--lib/__set__.js19
-rw-r--r--lib/rewire.js10
2 files changed, 25 insertions, 4 deletions
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;