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:
authorJohannes <johannes.ewald@roomieplanet.de>2012-09-18 21:00:39 +0400
committerJohannes <johannes.ewald@roomieplanet.de>2012-09-18 21:03:33 +0400
commit1434938354af135fbbecc7b21969f134dc6fdbd2 (patch)
tree745d790927371ca8133da54181e8c9caacdd0559
parent203b81607a82eb360113efbe8f63d2ed454c5a48 (diff)
- Improved strict mode detection
-rw-r--r--CHANGELOG.md3
-rw-r--r--lib/detectStrictMode.js4
-rw-r--r--package.json2
-rw-r--r--test/detectStrictMode.test.js16
4 files changed, 19 insertions, 6 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4479e9f..2b7499e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,8 @@
##Changelog
+###v1.0.2
+- Improved strict mode detection
+
###v1.0.1
- Fixed crash when a global module has been used in the browser
diff --git a/lib/detectStrictMode.js b/lib/detectStrictMode.js
index 347c455..35f27fb 100644
--- a/lib/detectStrictMode.js
+++ b/lib/detectStrictMode.js
@@ -2,11 +2,11 @@
* Returns true if the source code is intended to run in strict mode. Does not detect
* "use strict" if it occurs in a nested function.
*
- * @param {!String} src
+ * @param {String} src
* @return {Boolean}
*/
function detectStrictMode(src) {
- return (/^\s*"use strict";/g).test(src);
+ return (/^\s*(?:"use strict"|'use strict')[ \t]*(?:[\r\n]|;)/g).test(src);
}
module.exports = detectStrictMode; \ No newline at end of file
diff --git a/package.json b/package.json
index 8dd8573..6811a66 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name" : "rewire",
- "version" : "1.0.1",
+ "version" : "1.0.2",
"description" : "Dependency injection for node.js applications",
"keywords" : [
"dependency",
diff --git a/test/detectStrictMode.test.js b/test/detectStrictMode.test.js
index 89ef936..5cde34e 100644
--- a/test/detectStrictMode.test.js
+++ b/test/detectStrictMode.test.js
@@ -2,10 +2,20 @@ 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 () {
+ it("should detect all valid uses of \"use strict\";", function () {
expect(detectStrictMode('"use strict";')).to.be(true);
- expect(detectStrictMode(' "use strict";')).to.be(true);
- expect(detectStrictMode(' \n "use strict";')).to.be(true);
+ expect(detectStrictMode("'use strict';")).to.be(true);
+ expect(detectStrictMode(' "use strict";')).to.be(true);
+ expect(detectStrictMode('\n"use strict";')).to.be(true);
+ expect(detectStrictMode('\r\n"use strict";')).to.be(true);
+ expect(detectStrictMode('"use strict"\r\n')).to.be(true);
+ expect(detectStrictMode('"use strict" ; test();')).to.be(true);
+ });
+ it("should not detect invalid uses of \"use strict\";", function () {
+ expect(detectStrictMode('" use strict ";')).to.be(false);
+ expect(detectStrictMode('"use strict".replace("use", "fail");')).to.be(false);
+ expect(detectStrictMode('"use strict" .replace("use", "fail");')).to.be(false);
+ expect(detectStrictMode(';"use strict";')).to.be(false);
});
it("should not detect \"use strict\"; if it occurs in some nested function", function () {
expect(detectStrictMode('function () {"use strict";}')).to.be(false);