diff options
Diffstat (limited to 'tools/eslint/lib/rules/wrap-regex.js')
-rw-r--r-- | tools/eslint/lib/rules/wrap-regex.js | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/tools/eslint/lib/rules/wrap-regex.js b/tools/eslint/lib/rules/wrap-regex.js new file mode 100644 index 0000000000..e2cc7d2253 --- /dev/null +++ b/tools/eslint/lib/rules/wrap-regex.js @@ -0,0 +1,36 @@ +/** + * @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."); + } + } + } + }; + +}; |