Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bbbd05c

Browse files
committed
Fix issue where the strict mode was not detected when a comment was before "strict mode";
Fixes jhnns#54
1 parent 7cd73d4 commit bbbd05c

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

lib/detectStrictMode.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
var multiLineComment = /^\s*\/\*.*?\*\//;
2+
var singleLineComment = /^\s*\/\/.*?[\r\n]/;
3+
var strictMode = /^\s*(?:"use strict"|'use strict')[ \t]*(?:[\r\n]|;)/;
4+
15
/**
26
* Returns true if the source code is intended to run in strict mode. Does not detect
37
* "use strict" if it occurs in a nested function.
@@ -6,7 +10,19 @@
610
* @return {Boolean}
711
*/
812
function detectStrictMode(src) {
9-
return (/^\s*(?:"use strict"|'use strict')[ \t]*(?:[\r\n]|;)/g).test(src);
13+
var singleLine;
14+
var multiLine;
15+
16+
while ((singleLine = singleLineComment.test(src)) || (multiLine = multiLineComment.test(src))) {
17+
if (singleLine) {
18+
src = src.replace(singleLineComment, "");
19+
}
20+
if (multiLine) {
21+
src = src.replace(multiLineComment, "");
22+
}
23+
}
24+
25+
return strictMode.test(src);
1026
}
1127

12-
module.exports = detectStrictMode;
28+
module.exports = detectStrictMode;

test/detectStrictMode.test.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var expect = require("expect.js"),
22
detectStrictMode = require("../lib/detectStrictMode.js");
33

44
describe("detectStrictMode", function () {
5+
56
it("should detect all valid uses of \"use strict\";", function () {
67
expect(detectStrictMode('"use strict";')).to.be(true);
78
expect(detectStrictMode("'use strict';")).to.be(true);
@@ -11,13 +12,22 @@ describe("detectStrictMode", function () {
1112
expect(detectStrictMode('"use strict"\r\n')).to.be(true);
1213
expect(detectStrictMode('"use strict" ; test();')).to.be(true);
1314
});
15+
16+
it("should be allowed to place comments before \"use strict\";", function () {
17+
expect(detectStrictMode('// some comment\n"use strict";')).to.be(true);
18+
expect(detectStrictMode('/* yo! */"use strict"; /* another comment */')).to.be(true);
19+
expect(detectStrictMode(' // yes yo\r\n\r\n\r\n /*oh yoh*/\r\n//oh snap!\r /* yoh! */"use strict";')).to.be(true);
20+
});
21+
1422
it("should not detect invalid uses of \"use strict\";", function () {
1523
expect(detectStrictMode('" use strict ";')).to.be(false);
1624
expect(detectStrictMode('"use strict".replace("use", "fail");')).to.be(false);
1725
expect(detectStrictMode('"use strict" .replace("use", "fail");')).to.be(false);
1826
expect(detectStrictMode(';"use strict";')).to.be(false);
1927
});
28+
2029
it("should not detect \"use strict\"; if it occurs in some nested function", function () {
2130
expect(detectStrictMode('function () {"use strict";}')).to.be(false);
2231
});
23-
});
32+
33+
});

0 commit comments

Comments
 (0)