UNPKG

1.14 kBJavaScriptView Raw
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//------------------------------------------------------------------------------
7// Rule Definition
8//------------------------------------------------------------------------------
9
10module.exports = function(context) {
11
12 "use strict";
13
14 return {
15
16 "Literal": function(node) {
17 var token = context.getTokens(node)[0],
18 nodeType = token.type,
19 source,
20 grandparent,
21 ancestors;
22
23 if (nodeType === "RegularExpression") {
24 source = context.getTokens(node, 1, 1)[0].value;
25 ancestors = context.getAncestors();
26 grandparent = ancestors[ancestors.length - 1];
27
28 if (grandparent.type === "MemberExpression" &&
29 source[0] !== "(" && source[source.len - 1] !== ")") {
30 context.report(node, "Wrap the /regexp/ literal in parens to disambiguate the slash operator.");
31 }
32 }
33 }
34 };
35
36};