2024-08-09 12:04:48 +00:00
|
|
|
/**
|
|
|
|
* @fileoverview Validate strings passed to the RegExp constructor
|
|
|
|
* @author Michael Ficarra
|
|
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Requirements
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2024-08-21 06:34:30 +00:00
|
|
|
const RegExpValidator = require("@eslint-community/regexpp").RegExpValidator;
|
2024-08-09 12:04:48 +00:00
|
|
|
const validator = new RegExpValidator();
|
2024-08-21 06:34:30 +00:00
|
|
|
const validFlags = /[dgimsuvy]/gu;
|
2024-08-09 12:04:48 +00:00
|
|
|
const undefined1 = void 0;
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Rule Definition
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2024-08-21 06:34:30 +00:00
|
|
|
/** @type {import('../shared/types').Rule} */
|
2024-08-09 12:04:48 +00:00
|
|
|
module.exports = {
|
|
|
|
meta: {
|
|
|
|
type: "problem",
|
|
|
|
|
|
|
|
docs: {
|
2024-08-21 06:34:30 +00:00
|
|
|
description: "Disallow invalid regular expression strings in `RegExp` constructors",
|
2024-08-09 12:04:48 +00:00
|
|
|
recommended: true,
|
2024-08-21 06:34:30 +00:00
|
|
|
url: "https://eslint.org/docs/latest/rules/no-invalid-regexp"
|
2024-08-09 12:04:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
schema: [{
|
|
|
|
type: "object",
|
|
|
|
properties: {
|
|
|
|
allowConstructorFlags: {
|
|
|
|
type: "array",
|
|
|
|
items: {
|
|
|
|
type: "string"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
additionalProperties: false
|
|
|
|
}],
|
|
|
|
|
|
|
|
messages: {
|
|
|
|
regexMessage: "{{message}}."
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
create(context) {
|
|
|
|
|
|
|
|
const options = context.options[0];
|
|
|
|
let allowedFlags = null;
|
|
|
|
|
|
|
|
if (options && options.allowConstructorFlags) {
|
|
|
|
const temp = options.allowConstructorFlags.join("").replace(validFlags, "");
|
|
|
|
|
|
|
|
if (temp) {
|
|
|
|
allowedFlags = new RegExp(`[${temp}]`, "giu");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-21 06:34:30 +00:00
|
|
|
/**
|
|
|
|
* 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 }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-08-09 12:04:48 +00:00
|
|
|
/**
|
|
|
|
* Check if node is a string
|
|
|
|
* @param {ASTNode} node node to evaluate
|
|
|
|
* @returns {boolean} True if its a string
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
function isString(node) {
|
|
|
|
return node && node.type === "Literal" && typeof node.value === "string";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets flags of a regular expression created by the given `RegExp()` or `new RegExp()` call
|
|
|
|
* Examples:
|
|
|
|
* new RegExp(".") // => ""
|
|
|
|
* new RegExp(".", "gu") // => "gu"
|
|
|
|
* new RegExp(".", flags) // => null
|
|
|
|
* @param {ASTNode} node `CallExpression` or `NewExpression` node
|
|
|
|
* @returns {string|null} flags if they can be determined, `null` otherwise
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
function getFlags(node) {
|
|
|
|
if (node.arguments.length < 2) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isString(node.arguments[1])) {
|
|
|
|
return node.arguments[1].value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check syntax error in a given pattern.
|
|
|
|
* @param {string} pattern The RegExp pattern to validate.
|
2024-08-21 06:34:30 +00:00
|
|
|
* @param {Object} flags The RegExp flags to validate.
|
|
|
|
* @param {boolean} [flags.unicode] The Unicode flag.
|
|
|
|
* @param {boolean} [flags.unicodeSets] The UnicodeSets flag.
|
2024-08-09 12:04:48 +00:00
|
|
|
* @returns {string|null} The syntax error.
|
|
|
|
*/
|
2024-08-21 06:34:30 +00:00
|
|
|
function validateRegExpPattern(pattern, flags) {
|
2024-08-09 12:04:48 +00:00
|
|
|
try {
|
2024-08-21 06:34:30 +00:00
|
|
|
validator.validatePattern(pattern, undefined1, undefined1, flags);
|
2024-08-09 12:04:48 +00:00
|
|
|
return null;
|
|
|
|
} catch (err) {
|
|
|
|
return err.message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check syntax error in a given flags.
|
2024-08-21 06:34:30 +00:00
|
|
|
* @param {string|null} flags The RegExp flags to validate.
|
2024-08-09 12:04:48 +00:00
|
|
|
* @returns {string|null} The syntax error.
|
|
|
|
*/
|
|
|
|
function validateRegExpFlags(flags) {
|
2024-08-21 06:34:30 +00:00
|
|
|
if (!flags) {
|
|
|
|
return null;
|
|
|
|
}
|
2024-08-09 12:04:48 +00:00
|
|
|
try {
|
|
|
|
validator.validateFlags(flags);
|
|
|
|
} catch {
|
|
|
|
return `Invalid flags supplied to RegExp constructor '${flags}'`;
|
|
|
|
}
|
2024-08-21 06:34:30 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* `regexpp` checks the combination of `u` and `v` flags when parsing `Pattern` according to `ecma262`,
|
|
|
|
* but this rule may check only the flag when the pattern is unidentifiable, so check it here.
|
|
|
|
* https://tc39.es/ecma262/multipage/text-processing.html#sec-parsepattern
|
|
|
|
*/
|
|
|
|
if (flags.includes("u") && flags.includes("v")) {
|
|
|
|
return "Regex 'u' and 'v' flags cannot be used together";
|
|
|
|
}
|
|
|
|
return null;
|
2024-08-09 12:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
"CallExpression, NewExpression"(node) {
|
2024-08-21 06:34:30 +00:00
|
|
|
if (node.callee.type !== "Identifier" || node.callee.name !== "RegExp") {
|
2024-08-09 12:04:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-08-21 06:34:30 +00:00
|
|
|
|
2024-08-09 12:04:48 +00:00
|
|
|
let flags = getFlags(node);
|
|
|
|
|
|
|
|
if (flags && allowedFlags) {
|
|
|
|
flags = flags.replace(allowedFlags, "");
|
|
|
|
}
|
|
|
|
|
2024-08-21 06:34:30 +00:00
|
|
|
let message = validateRegExpFlags(flags);
|
|
|
|
|
|
|
|
if (message) {
|
|
|
|
report(node, message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isString(node.arguments[0])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const pattern = node.arguments[0].value;
|
|
|
|
|
|
|
|
message = (
|
2024-08-09 12:04:48 +00:00
|
|
|
|
2024-08-21 06:34:30 +00:00
|
|
|
// If flags are unknown, report the regex only if its pattern is invalid both with and without the "u" flag
|
|
|
|
flags === null
|
|
|
|
? (
|
|
|
|
validateRegExpPattern(pattern, { unicode: true, unicodeSets: false }) &&
|
|
|
|
validateRegExpPattern(pattern, { unicode: false, unicodeSets: true }) &&
|
|
|
|
validateRegExpPattern(pattern, { unicode: false, unicodeSets: false })
|
|
|
|
)
|
|
|
|
: validateRegExpPattern(pattern, { unicode: flags.includes("u"), unicodeSets: flags.includes("v") })
|
|
|
|
);
|
2024-08-09 12:04:48 +00:00
|
|
|
|
|
|
|
if (message) {
|
2024-08-21 06:34:30 +00:00
|
|
|
report(node, message);
|
2024-08-09 12:04:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|