Blame view

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