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

Commit1434938

Browse files
author
Johannes
committed
- Improved strict mode detection
1 parent203b816 commit1434938

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

‎CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
##Changelog
22

3+
###v1.0.2
4+
- Improved strict mode detection
5+
36
###v1.0.1
47
- Fixed crash when a global module has been used in the browser
58

‎lib/detectStrictMode.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
* Returns true if the source code is intended to run in strict mode. Does not detect
33
* "use strict" if it occurs in a nested function.
44
*
5-
*@param {!String} src
5+
*@param {String} src
66
*@return {Boolean}
77
*/
88
functiondetectStrictMode(src){
9-
return(/^\s*"usestrict";/g).test(src);
9+
return(/^\s*(?:"usestrict"|'usestrict')[\t]*(?:[\r\n]|;)/g).test(src);
1010
}
1111

1212
module.exports=detectStrictMode;

‎package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name" :"rewire",
3-
"version" :"1.0.1",
3+
"version" :"1.0.2",
44
"description" :"Dependency injection for node.js applications",
55
"keywords" : [
66
"dependency",

‎test/detectStrictMode.test.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@ var expect = require("expect.js"),
22
detectStrictMode=require("../lib/detectStrictMode.js");
33

44
describe("detectStrictMode",function(){
5-
it("should detect \"use strict\"; at the beginning of a string and ignore all whitespace before",function(){
5+
it("should detectall valid uses of\"use strict\";",function(){
66
expect(detectStrictMode('"use strict";')).to.be(true);
7-
expect(detectStrictMode(' "use strict";')).to.be(true);
8-
expect(detectStrictMode(' \n "use strict";')).to.be(true);
7+
expect(detectStrictMode("'use strict';")).to.be(true);
8+
expect(detectStrictMode(' "use strict";')).to.be(true);
9+
expect(detectStrictMode('\n"use strict";')).to.be(true);
10+
expect(detectStrictMode('\r\n"use strict";')).to.be(true);
11+
expect(detectStrictMode('"use strict"\r\n')).to.be(true);
12+
expect(detectStrictMode('"use strict" ; test();')).to.be(true);
13+
});
14+
it("should not detect invalid uses of \"use strict\";",function(){
15+
expect(detectStrictMode('" use strict ";')).to.be(false);
16+
expect(detectStrictMode('"use strict".replace("use", "fail");')).to.be(false);
17+
expect(detectStrictMode('"use strict" .replace("use", "fail");')).to.be(false);
18+
expect(detectStrictMode(';"use strict";')).to.be(false);
919
});
1020
it("should not detect \"use strict\"; if it occurs in some nested function",function(){
1121
expect(detectStrictMode('function () {"use strict";}')).to.be(false);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp