node/tools/eslint/lib/rules/no-invalid-regexp.js
Yosuke Furukawa f9dd34d301 tools: replace closure-linter with eslint
PR-URL: https://github.com/iojs/io.js/pull/1539
Fixes: https://github.com/iojs/io.js/issues/1253
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com>
Reviewed-By: Johan Bergström <bugs@bergstroem.nu>
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
2015-05-09 12:09:52 +09:00

52 lines
1.5 KiB
JavaScript

/**
* @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
};
};