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
diff options
context:
space:
mode:
-rw-r--r--lib/detectStrictMode.js5
-rw-r--r--test/detectStrictMode.test.js13
-rw-r--r--test/testModules/strictModule.js7
3 files changed, 25 insertions, 0 deletions
diff --git a/lib/detectStrictMode.js b/lib/detectStrictMode.js
new file mode 100644
index 0000000..d0107d0
--- /dev/null
+++ b/lib/detectStrictMode.js
@@ -0,0 +1,5 @@
+function detectStrictMode(src) {
+ return (/^\s*"use strict";/g).test(src);
+}
+
+module.exports = detectStrictMode; \ No newline at end of file
diff --git a/test/detectStrictMode.test.js b/test/detectStrictMode.test.js
new file mode 100644
index 0000000..a154471
--- /dev/null
+++ b/test/detectStrictMode.test.js
@@ -0,0 +1,13 @@
+var expect = require("expect.js"),
+ detectStrictMode = require("../lib/detectStrictMode.js");
+
+describe("detectStrictMode", function () {
+ it("should detect \"use strict\"; at the beginning of a string and ignore all whitespace before", function () {
+ expect(detectStrictMode('"use strict";')).to.be(true);
+ expect(detectStrictMode(' "use strict";')).to.be(true);
+ expect(detectStrictMode(' \n "use strict";')).to.be(true);
+ });
+ it("should not detect \"use strict\"; if it occurs in some nested function", function () {
+ expect(detectStrictMode('function () {"use strict";}')).to.be(false);
+ });
+}); \ No newline at end of file
diff --git a/test/testModules/strictModule.js b/test/testModules/strictModule.js
new file mode 100644
index 0000000..140dd2a
--- /dev/null
+++ b/test/testModules/strictModule.js
@@ -0,0 +1,7 @@
+"use strict"; // run code in ES5 strict mode
+
+function doSomethingUnstrict() {
+ var caller = arguments.callee.caller; // this should throw an error as a proof that strict mode is on
+}
+
+exports.doSomethingUnstrict = doSomethingUnstrict; \ No newline at end of file