UNPKG

1.05 kBJavaScriptView Raw
1/**
2 * @fileoverview Validate strings passed to the RegExp constructor
3 * @author Michael Ficarra
4 */
5"use strict";
6
7//------------------------------------------------------------------------------
8// Rule Definition
9//------------------------------------------------------------------------------
10
11module.exports = function(context) {
12
13 function isString(node) {
14 return node && node.type === "Literal" && typeof node.value === "string";
15 }
16
17 function check(node) {
18 if(node.callee.type === "Identifier" && node.callee.name === "RegExp" && isString(node.arguments[0])) {
19 try {
20 if(isString(node.arguments[1])) {
21 void new RegExp(node.arguments[0].value, node.arguments[1].value);
22 } else {
23 void new RegExp(node.arguments[0].value);
24 }
25 } catch(e) {
26 context.report(node, e.message);
27 }
28 }
29 }
30
31 return {
32 "CallExpression": check,
33 "NewExpression": check
34 };
35
36};