]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-invalid-regexp.js
c09e36fd017697d71903d81e8eb167ab109a2b73
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({ ecmaVersion
: 2018 });
13 const validFlags
= /[gimuys]/gu;
14 const undefined1
= void 0;
16 //------------------------------------------------------------------------------
18 //------------------------------------------------------------------------------
25 description
: "disallow invalid regular expression strings in `RegExp` constructors",
26 category
: "Possible Errors",
28 url
: "https://eslint.org/docs/rules/no-invalid-regexp"
34 allowConstructorFlags
: {
41 additionalProperties
: false
45 regexMessage
: "{{message}}."
51 const options
= context
.options
[0];
52 let allowedFlags
= null;
54 if (options
&& options
.allowConstructorFlags
) {
55 const temp
= options
.allowConstructorFlags
.join("").replace(validFlags
, "");
58 allowedFlags
= new RegExp(`[${temp}]`, "giu");
63 * Check if node is a string
64 * @param {ASTNode} node node to evaluate
65 * @returns {boolean} True if its a string
68 function isString(node
) {
69 return node
&& node
.type
=== "Literal" && typeof node
.value
=== "string";
73 * Check syntax error in a given pattern.
74 * @param {string} pattern The RegExp pattern to validate.
75 * @param {boolean} uFlag The Unicode flag.
76 * @returns {string|null} The syntax error.
78 function validateRegExpPattern(pattern
, uFlag
) {
80 validator
.validatePattern(pattern
, undefined1
, undefined1
, uFlag
);
88 * Check syntax error in a given flags.
89 * @param {string} flags The RegExp flags to validate.
90 * @returns {string|null} The syntax error.
92 function validateRegExpFlags(flags
) {
94 validator
.validateFlags(flags
);
97 return `Invalid flags supplied to RegExp constructor '${flags}'`;
102 "CallExpression, NewExpression"(node
) {
103 if (node
.callee
.type
!== "Identifier" || node
.callee
.name
!== "RegExp" || !isString(node
.arguments
[0])) {
106 const pattern
= node
.arguments
[0].value
;
107 let flags
= isString(node
.arguments
[1]) ? node
.arguments
[1].value
: "";
110 flags
= flags
.replace(allowedFlags
, "");
113 // If flags are unknown, check both are errored or not.
114 const message
= validateRegExpFlags(flags
) || (
116 ? validateRegExpPattern(pattern
, flags
.indexOf("u") !== -1)
117 : validateRegExpPattern(pattern
, true) && validateRegExpPattern(pattern
, false)
123 messageId
: "regexMessage",