summaryrefslogtreecommitdiff
path: root/tools/eslint/lib/rules/no-invalid-regexp.js
diff options
context:
space:
mode:
Diffstat (limited to 'tools/eslint/lib/rules/no-invalid-regexp.js')
-rw-r--r--tools/eslint/lib/rules/no-invalid-regexp.js51
1 files changed, 51 insertions, 0 deletions
diff --git a/tools/eslint/lib/rules/no-invalid-regexp.js b/tools/eslint/lib/rules/no-invalid-regexp.js
new file mode 100644
index 0000000000..cea5372f15
--- /dev/null
+++ b/tools/eslint/lib/rules/no-invalid-regexp.js
@@ -0,0 +1,51 @@
+/**
+ * @fileoverview Validate strings passed to the RegExp constructor
+ * @author Michael Ficarra
+ * @copyright 2014 Michael Ficarra. All rights reserved.
+ */
+"use strict";
+
+//------------------------------------------------------------------------------
+// Requirements
+//------------------------------------------------------------------------------
+
+var espree = require("espree");
+
+//------------------------------------------------------------------------------
+// Rule Definition
+//------------------------------------------------------------------------------
+
+module.exports = function(context) {
+
+ function isString(node) {
+ return node && node.type === "Literal" && typeof node.value === "string";
+ }
+
+ function check(node) {
+ if (node.callee.type === "Identifier" && node.callee.name === "RegExp" && isString(node.arguments[0])) {
+ var flags = isString(node.arguments[1]) ? node.arguments[1].value : "";
+
+ try {
+ void new RegExp(node.arguments[0].value);
+ } catch(e) {
+ context.report(node, e.message);
+ }
+
+ if (flags) {
+
+ try {
+ espree.parse("/./" + flags, { ecmaFeatures: context.ecmaFeatures });
+ } catch (ex) {
+ context.report(node, "Invalid flags supplied to RegExp constructor '" + flags + "'");
+ }
+ }
+
+ }
+ }
+
+ return {
+ "CallExpression": check,
+ "NewExpression": check
+ };
+
+};