a800acc87fefd1e94a31e8a164f4c921518855c6
[platform/framework/web/crosswalk-tizen.git] /
1 /**
2  * @fileoverview Rule to forbid control charactes from regular expressions.
3  * @author Nicholas C. Zakas
4  */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Rule Definition
10 //------------------------------------------------------------------------------
11
12 module.exports = function(context) {
13
14     function getRegExp(node) {
15
16         if (node.value instanceof RegExp) {
17             return node.value;
18         } else if (typeof node.value === "string") {
19
20             var parent = context.getAncestors().pop();
21             if ((parent.type === "NewExpression" || parent.type === "CallExpression") &&
22             parent.callee.type === "Identifier" && parent.callee.name === "RegExp") {
23
24                 // there could be an invalid regular expression string
25                 try {
26                     return new RegExp(node.value);
27                 } catch (ex) {
28                     return null;
29                 }
30
31             }
32         } else {
33             return null;
34         }
35
36     }
37
38
39
40     return {
41
42         "Literal": function(node) {
43
44             var computedValue,
45                 regex = getRegExp(node);
46
47             if (regex) {
48                 computedValue = regex.toString();
49                 if (/[\x00-\x1f]/.test(computedValue)) {
50                     context.report(node, "Unexpected control character in regular expression.");
51                 }
52             }
53         }
54     };
55
56 };