diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/detectStrictMode.js | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/lib/detectStrictMode.js b/lib/detectStrictMode.js index 35f27fb..57dc14d 100644 --- a/lib/detectStrictMode.js +++ b/lib/detectStrictMode.js @@ -1,3 +1,7 @@ +var multiLineComment = /^\s*\/\*.*?\*\//;
+var singleLineComment = /^\s*\/\/.*?[\r\n]/;
+var strictMode = /^\s*(?:"use strict"|'use strict')[ \t]*(?:[\r\n]|;)/;
+
/**
* 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.
@@ -6,7 +10,19 @@ * @return {Boolean}
*/
function detectStrictMode(src) {
- return (/^\s*(?:"use strict"|'use strict')[ \t]*(?:[\r\n]|;)/g).test(src);
+ var singleLine;
+ var multiLine;
+
+ while ((singleLine = singleLineComment.test(src)) || (multiLine = multiLineComment.test(src))) {
+ if (singleLine) {
+ src = src.replace(singleLineComment, "");
+ }
+ if (multiLine) {
+ src = src.replace(multiLineComment, "");
+ }
+ }
+
+ return strictMode.test(src);
}
-module.exports = detectStrictMode;
\ No newline at end of file +module.exports = detectStrictMode;
|