diff options
-rw-r--r-- | CHANGELOG.md | 3 | ||||
-rw-r--r-- | lib/detectStrictMode.js | 4 | ||||
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | test/detectStrictMode.test.js | 16 |
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); |