8000 added detectStrictMode-module · ShMcK/rewire-coderoad@ce972e2 · GitHub
[go: up one dir, main page]

Skip to content

Commit ce972e2

Browse files
author
Johannes
committed
added detectStrictMode-module
1 parent 7d90695 commit ce972e2

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

lib/detectStrictMode.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function detectStrictMode(src) {
2+
return (/^\s*"use strict";/g).test(src);
3+
}
4+
5+
module.exports = detectStrictMode;

test/detectStrictMode.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var expect = require("expect.js"),
2+
detectStrictMode = require("../lib/detectStrictMode.js");
3+
4+
describe("detectStrictMode", function () {
5+
it("should detect \"use strict\"; at the beginning of a string and ignore all whitespace before", function () {
6+
expect(detectStrictMode('"use strict";')).to.be(true);
7+
expect(detectStrictMode(' "use strict";')).to.be(true);
8+
expect(detectStrictMode(' \n "use strict";')).to.be(true);
9+
});
10+
it("should not detect \"use strict\"; if it occurs in some nested function", function () {
11+
expect(detectStrictMode('function () {"use strict";}')).to.be(false);
12+
});
13+
});

test/testModules/strictModule.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict"; // run code in ES5 strict mode
2+
3+
function doSomethingUnstrict() {
4+
var caller = arguments.callee.caller; // this should throw an error as a proof that strict mode is on
5+
}
6+
7+
exports.doSomethingUnstrict = doSomethingUnstrict;

0 commit comments

Comments
 (0)
0