e2cc7d225322eaba37656b4ae5df029da2d3e5cd
[platform/framework/web/crosswalk-tizen.git] /
1 /**
2  * @fileoverview Rule to flag when regex literals are not wrapped in parens
3  * @author Matt DuVall <http://www.mattduvall.com>
4  */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Rule Definition
10 //------------------------------------------------------------------------------
11
12 module.exports = function(context) {
13
14     return {
15
16         "Literal": function(node) {
17             var token = context.getFirstToken(node),
18                 nodeType = token.type,
19                 source,
20                 grandparent,
21                 ancestors;
22
23             if (nodeType === "RegularExpression") {
24                 source = context.getTokenBefore(node);
25                 ancestors = context.getAncestors();
26                 grandparent = ancestors[ancestors.length - 1];
27
28                 if (grandparent.type === "MemberExpression" && grandparent.object === node &&
29                     (!source || source.value !== "(")) {
30                     context.report(node, "Wrap the regexp literal in parens to disambiguate the slash.");
31                 }
32             }
33         }
34     };
35
36 };