// Requirements
//------------------------------------------------------------------------------
-const RegExpValidator = require("regexpp").RegExpValidator;
+const RegExpValidator = require("@eslint-community/regexpp").RegExpValidator;
const validator = new RegExpValidator();
const validFlags = /[dgimsuy]/gu;
const undefined1 = void 0;
docs: {
description: "Disallow invalid regular expression strings in `RegExp` constructors",
recommended: true,
- url: "https://eslint.org/docs/rules/no-invalid-regexp"
+ url: "https://eslint.org/docs/latest/rules/no-invalid-regexp"
},
schema: [{
}
}
+ /**
+ * Reports error with the provided message.
+ * @param {ASTNode} node The node holding the invalid RegExp
+ * @param {string} message The message to report.
+ * @returns {void}
+ */
+ function report(node, message) {
+ context.report({
+ node,
+ messageId: "regexMessage",
+ data: { message }
+ });
+ }
+
/**
* Check if node is a string
* @param {ASTNode} node node to evaluate
/**
* Check syntax error in a given flags.
- * @param {string} flags The RegExp flags to validate.
+ * @param {string|null} flags The RegExp flags to validate.
* @returns {string|null} The syntax error.
*/
function validateRegExpFlags(flags) {
+ if (!flags) {
+ return null;
+ }
try {
validator.validateFlags(flags);
return null;
return {
"CallExpression, NewExpression"(node) {
- if (node.callee.type !== "Identifier" || node.callee.name !== "RegExp" || !isString(node.arguments[0])) {
+ if (node.callee.type !== "Identifier" || node.callee.name !== "RegExp") {
return;
}
- const pattern = node.arguments[0].value;
+
let flags = getFlags(node);
if (flags && allowedFlags) {
flags = flags.replace(allowedFlags, "");
}
- const message =
- (
- flags && validateRegExpFlags(flags)
- ) ||
- (
+ let message = validateRegExpFlags(flags);
+
+ if (message) {
+ report(node, message);
+ return;
+ }
+
+ if (!isString(node.arguments[0])) {
+ return;
+ }
+
+ const pattern = node.arguments[0].value;
+
+ message = (
- // If flags are unknown, report the regex only if its pattern is invalid both with and without the "u" flag
- flags === null
- ? validateRegExpPattern(pattern, true) && validateRegExpPattern(pattern, false)
- : validateRegExpPattern(pattern, flags.includes("u"))
- );
+ // If flags are unknown, report the regex only if its pattern is invalid both with and without the "u" flag
+ flags === null
+ ? validateRegExpPattern(pattern, true) && validateRegExpPattern(pattern, false)
+ : validateRegExpPattern(pattern, flags.includes("u"))
+ );
if (message) {
- context.report({
- node,
- messageId: "regexMessage",
- data: { message }
- });
+ report(node, message);
}
}
};