UNPKG

782 BJavaScriptView Raw
1var multiLineComment = /^\s*\/\*.*?\*\//;
2var singleLineComment = /^\s*\/\/.*?[\r\n]/;
3var strictMode = /^\s*(?:"use strict"|'use strict')[ \t]*(?:[\r\n]|;)/;
4
5/**
6 * Returns true if the source code is intended to run in strict mode. Does not detect
7 * "use strict" if it occurs in a nested function.
8 *
9 * @param {String} src
10 * @return {Boolean}
11 */
12function detectStrictMode(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);
26}
27
28module.exports = detectStrictMode;