]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-invalid-regexp.js
ee19932896609934ccae25fe9a697889c860f53e
2 * @fileoverview Validate strings passed to the RegExp constructor
3 * @author Michael Ficarra
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const RegExpValidator
= require("regexpp").RegExpValidator
;
12 const validator
= new RegExpValidator();
13 const validFlags
= /[dgimsuy]/gu;
14 const undefined1
= void 0;
16 //------------------------------------------------------------------------------
18 //------------------------------------------------------------------------------
25 description
: "disallow invalid regular expression strings in `RegExp` constructors",
27 url
: "https://eslint.org/docs/rules/no-invalid-regexp"
33 allowConstructorFlags
: {
40 additionalProperties
: false
44 regexMessage
: "{{message}}."
50 const options
= context
.options
[0];
51 let allowedFlags
= null;
53 if (options
&& options
.allowConstructorFlags
) {
54 const temp
= options
.allowConstructorFlags
.join("").replace(validFlags
, "");
57 allowedFlags
= new RegExp(`[${temp}]`, "giu");
62 * Check if node is a string
63 * @param {ASTNode} node node to evaluate
64 * @returns {boolean} True if its a string
67 function isString(node
) {
68 return node
&& node
.type
=== "Literal" && typeof node
.value
=== "string";
72 * Gets flags of a regular expression created by the given `RegExp()` or `new RegExp()` call
74 * new RegExp(".") // => ""
75 * new RegExp(".", "gu") // => "gu"
76 * new RegExp(".", flags) // => null
77 * @param {ASTNode} node `CallExpression` or `NewExpression` node
78 * @returns {string|null} flags if they can be determined, `null` otherwise
81 function getFlags(node
) {
82 if (node
.arguments
.length
< 2) {
86 if (isString(node
.arguments
[1])) {
87 return node
.arguments
[1].value
;
94 * Check syntax error in a given pattern.
95 * @param {string} pattern The RegExp pattern to validate.
96 * @param {boolean} uFlag The Unicode flag.
97 * @returns {string|null} The syntax error.
99 function validateRegExpPattern(pattern
, uFlag
) {
101 validator
.validatePattern(pattern
, undefined1
, undefined1
, uFlag
);
109 * Check syntax error in a given flags.
110 * @param {string} flags The RegExp flags to validate.
111 * @returns {string|null} The syntax error.
113 function validateRegExpFlags(flags
) {
115 validator
.validateFlags(flags
);
118 return `Invalid flags supplied to RegExp constructor '${flags}'`;
123 "CallExpression, NewExpression"(node
) {
124 if (node
.callee
.type
!== "Identifier" || node
.callee
.name
!== "RegExp" || !isString(node
.arguments
[0])) {
127 const pattern
= node
.arguments
[0].value
;
128 let flags
= getFlags(node
);
130 if (flags
&& allowedFlags
) {
131 flags
= flags
.replace(allowedFlags
, "");
136 flags
&& validateRegExpFlags(flags
)
140 // If flags are unknown, report the regex only if its pattern is invalid both with and without the "u" flag
142 ? validateRegExpPattern(pattern
, true) && validateRegExpPattern(pattern
, false)
143 : validateRegExpPattern(pattern
, flags
.includes("u"))
149 messageId
: "regexMessage",