Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0c86073

Browse files
author
Johannes
committed
removed "use strict"; to avoid influencing not-strict modules
1 parentce972e2 commit0c86073

File tree

10 files changed

+43
-29
lines changed

10 files changed

+43
-29
lines changed

‎lib/__get__.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
"use strict";// run code in ES5 strict mode
2-
31
/**
42
* This function will be stringified and then injected into every rewired module.
53
* Then you can leak private variables by calling myModule.__get__("myPrivateVar");

‎lib/__set__.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
"use strict";// run code in ES5 strict mode
2-
31
/**
42
* This function will be stringified and then injected into every rewired module.
53
* Then you can set private variables by calling myModule.__set__("myPrivateVar", newValue);
@@ -17,7 +15,7 @@ module.exports = function __set__() {
1715
arguments.varValue=arguments[1];
1816
arguments.src="";
1917
arguments.checkExistsSrc=function(varName){
20-
return"if (typeof "+varName+" === 'undefined') { throw new ReferenceError('"+varName+" is notdefined');} ";
18+
return"if (typeof "+varName+" === 'undefined') { throw new ReferenceError('Cannot __set__():"+varName+" is notdeclared within the module.');} ";
2119
};
2220

2321
if(typeofarguments[0]==="object"){

‎lib/getImportGlobalsSrc.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
"use strict";// run code in ES5 strict mode
2-
31
/**
42
* Declares all globals with a var and assigns the global object. Thus you're able to
53
* override globals without changing the global object itself.

‎lib/index.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
"use strict";// run code in ES5 strict mode
2-
3-
varrewireModule=require("./rewire.js");
1+
varrewireModule;
42

53
/**
64
* This function is needed to determine the calling parent module.
75
* Thus rewire acts exactly the same like require() in the test module.
86
*
97
*@param {!String} request Path to the module that shall be rewired. Use it exactly like require().
10-
*@param {Object} mocks An object with mocks. Keys should be the exactly same like they're required in the target module. So if you write require("../../myModules/myModuleA.js") you need to pass {"../../myModules/myModuleA.js": myModuleAMock}.
11-
*@param {Object} injections If you pass an object, all keys of the object will be vars within the module. You can also eval a string. Please note: All scripts are injected at the end of the module. So if there is any code in your module that is executed during require(), your injected variables will be undefined at this point. For example: passing {console: {...}} will cause all calls of console.log() to throw an exception if they're executed during require().
12-
*@param {Array} leaks An array with variable names that should be exported. These variables are accessible via myModule.__
138
*@param {Boolean} cache Indicates whether the rewired module should be cached by node so subsequent calls of require() will return the rewired module. Subsequent calls of rewire() will always overwrite the cache.
149
*@return {*} the rewired module
1510
*/
@@ -23,6 +18,16 @@ function rewire(request, cache) {
2318
returnrewireModule(module.parent,request,cache);
2419
}
2520

26-
rewire.reset=rewireModule.reset;
21+
// Conditional require for different environments
22+
if(process.title==="browser"){
23+
module.exports=require("./browserify/browserifyRewire.js");
24+
}else{
25+
// Putting (require) within brackets is a hack to disable browserify require sniffing
26+
//@see https://github.com/substack/node-browserify/issues/132#issuecomment-5281470
27+
rewireModule=(require)("./rewire.js");
28+
29+
rewire.reset=rewireModule.reset;
30+
rewire.browserify=(require)("./browserify/browserifyMiddleware.js");
2731

28-
module.exports=rewire;
32+
module.exports=rewire;
33+
}

‎lib/rewire.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
"use strict";// run code in ES5 strict mode
2-
31
varModule=require("module"),
2+
fs=require("fs"),
43
__get__=require("./__get__.js"),
54
__set__=require("./__set__.js"),
65
getImportGlobalsSrc=require("./getImportGlobalsSrc.js"),
6+
detectStrictMode=require("./detectStrictMode.js"),
77

88
moduleWrapper0=Module.wrapper[0],// caching original wrapper
99
moduleWrapper1=Module.wrapper[1],// caching original wrapper
@@ -21,7 +21,8 @@ function rewire(parentModule, filename, cache) {
2121
vartestModule,
2222
nodeRequire,
2323
prepend,
24-
append;
24+
append,
25+
src;
2526

2627
/**
2728
* Proxies the first require call in order to draw back all changes.
@@ -63,7 +64,14 @@ function rewire(parentModule, filename, cache) {
6364
append="module.exports.__set__ = "+__set__.toString()+"; ";
6465
append+="module.exports.__get__ = "+__get__.toString()+"; ";
6566

66-
// Apply prepend and append
67+
// Check if the module uses the strict mode.
68+
// If so we must ensure that "use strict"; stays at the beginning of the module.
69+
src=fs.readFileSync(filename,"utf8");
70+
if(detectStrictMode(src)===true){
71+
prepend=' "use strict"; '+prepend;
72+
}
73+
74+
// Apply prepend and appends
6775
Module.wrapper[0]=moduleWrapper0+prepend;
6876
Module.wrapper[1]=append+moduleWrapper1;
6977

‎test/__get__.test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
"use strict";// run code in ES5 strict mode
2-
31
varexpect=require("expect.js"),
42
vm=require("vm"),
53
__get__=require("../lib/__get__.js"),

‎test/__set__.test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
"use strict";// run code in ES5 strict mode
2-
31
varexpect=require("expect.js"),
42
__set__=require("../lib/__set__.js"),
53
vm=require("vm"),

‎test/debug.test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
"use strict";// run code in ES5 strict mode
2-
31
varrewire=require("../lib/index.js");
42

53
// add breakpoints in testModules/debuggerModule.js and debug this file with your IDE to

‎test/getImportGlobalsSrc.test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
"use strict";// run code in ES5 strict mode
2-
31
varexpect=require("expect.js"),
42
vm=require("vm"),
53
getImportGlobalsSrc=require("../lib/getImportGlobalsSrc.js");

‎test/rewire.test.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
"use strict";// run code in ES5 strict mode
1+
// Don't run code in ES5 strict mode.
2+
// In case this module was in strict mode, all other modules called by this would also be strict.
3+
// But when testing if the strict mode is preserved, we must ensure that this module is NOT strict.
24

35
varpath=require("path"),
46
expect=require("expect.js"),
@@ -8,9 +10,15 @@ var testModules = {
810
A:path.resolve(__dirname,"./testModules/moduleA.js"),
911
B:path.resolve(__dirname,"./testModules/moduleB.js"),
1012
someOtherModule:path.resolve(__dirname,"./testModules/someOtherModule.js"),
11-
emptyModule:path.resolve(__dirname,"./testModules/emptyModule.js")
13+
emptyModule:path.resolve(__dirname,"./testModules/emptyModule.js"),
14+
strictModule:path.resolve(__dirname,"./testModules/strictModule.js")
1215
};
1316

17+
18+
functioncheckForTypeError(err){
19+
expect(err.constructor).to.be(TypeError);
20+
}
21+
1422
functioncleanRequireCache(){
1523
varmoduleName,
1624
modulePath;
@@ -140,6 +148,13 @@ describe("rewire", function () {
140148
it("subsequent calls of rewire should always return a new instance",function(){
141149
expect(rewire(testModules.A)).not.to.be(rewire(testModules.A));
142150
});
151+
it("should preserve the strict mode",function(){
152+
varstrictModule=rewire(testModules.strictModule);
153+
154+
expect(function(){
155+
strictModule.doSomethingUnstrict();
156+
}).to.throwException(checkForTypeError);
157+
});
143158
describe("#reset",function(){
144159
it("should remove all rewired modules from cache",function(){
145160
varrewiredModuleA=rewire(testModules.A),

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp