Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ad7056d

Browse files
committed
rewire methods as non-enumerable
1 parent 3e06f87 commit ad7056d

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

lib/rewire.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var Module = require("module"),
99

1010
var __get__Src = __get__.toString(),
1111
__set__Src = __set__.toString(),
12-
__with_Src = __with__.toString();
12+
__with__Src = __with__.toString();
1313

1414
/**
1515
* Does actual rewiring the module. For further documentation @see index.js
@@ -18,7 +18,13 @@ function internalRewire(parentModulePath, targetPath) {
1818
var targetModule,
1919
prelude,
2020
appendix,
21-
src;
21+
srcs;
22+
23+
srcs = {
24+
"__get__": __get__Src,
25+
"__set__": __set__Src,
26+
"__with__": __with__Src
27+
};
2228

2329
// Checking params
2430
if (typeof targetPath !== "string") {
@@ -42,11 +48,20 @@ function internalRewire(parentModulePath, targetPath) {
4248
// We prepend a list of all globals declared with var so they can be overridden (without changing original globals)
4349
prelude = getImportGlobalsSrc();
4450

51+
4552
// We append our special setter and getter.
4653
appendix = "\n";
47-
appendix += "module.exports.__set__ = " + __set__Src + "; ";
48-
appendix += "module.exports.__get__ = " + __get__Src + "; ";
49-
appendix += "module.exports.__with__ = " + __with_Src + "; ";
54+
55+
Object.keys(srcs).forEach( function (key) {
56+
var str = [
57+
"Object.defineProperty(module.exports, '",
58+
key,
59+
"', {enumerable: false, value: ",
60+
srcs[key],
61+
"}); "
62+
].join("");
63+
appendix += str;
64+
});
5065

5166
// Check if the module uses the strict mode.
5267
// If so we must ensure that "use strict"; stays at the beginning of the module.

test/testModules/sharedTestCases.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
6666
expect(rewire("./moduleB.js").__with__.toString()).to.be(__with__Src);
6767
});
6868

69+
it("should provide __set__ as a non-enumerable property", function () {
70+
expect(Object.keys(rewire("./moduleA.js")).indexOf("__set__")).to.be(-1)
71+
});
72+
73+
it("should provide __get__ as a non-enumerable property", function () {
74+
expect(Object.keys(rewire("./moduleA.js")).indexOf("__get__")).to.be(-1)
75+
});
76+
77+
it("should provide __with__ as a non-enumerable property", function () {
78+
expect(Object.keys(rewire("./moduleA.js")).indexOf("__with__")).to.be(-1)
79+
});
80+
6981
it("should not influence other modules", function () {
7082
rewire("./moduleA.js");
7183

0 commit comments

Comments
 (0)