Content-Length: 331649 | pFad | https://github.com/eslint/eslint/commit/183b459b72be5c1e359985b1584f73421dfb1484

A7 feat: add error message for duplicate flags in `no-invalid-regexp` (#… · eslint/eslint@183b459 · GitHub
Skip to content

Commit

Permalink
feat: add error message for duplicate flags in no-invalid-regexp (#…
Browse files Browse the repository at this point in the history
…18837)

* feat: add error message to duplicate flags

* update error message
  • Loading branch information
Tanujkanti4441 authored Sep 3, 2024
1 parent f6fdef9 commit 183b459
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
21 changes: 13 additions & 8 deletions lib/rules/no-invalid-regexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,16 @@ module.exports = {
* Check syntax error in a given flags.
* @param {string|null} flags The RegExp flags to validate.
* @param {string|null} flagsToCheck The RegExp invalid flags.
* @param {string} allFlags all valid and allowed flags.
* @returns {string|null} The syntax error.
*/
function validateRegExpFlags(flags, flagsToCheck) {
const flagsToReport = [];
function validateRegExpFlags(flags, flagsToCheck, allFlags) {
const duplicateFlags = [];

if (typeof flags === "string") {
for (const flag of flags) {
if (flagsToCheck.includes(flag)) {
flagsToReport.push(flag);
if (typeof flagsToCheck === "string") {
for (const flag of flagsToCheck) {
if (allFlags.includes(flag)) {
duplicateFlags.push(flag);
}
}
}
Expand All @@ -148,11 +149,15 @@ module.exports = {
return "Regex 'u' and 'v' flags cannot be used together";
}

if (duplicateFlags.length > 0) {
return `Duplicate flags ('${duplicateFlags.join("")}') supplied to RegExp constructor`;
}

if (!flagsToCheck) {
return null;
}

return `Invalid flags supplied to RegExp constructor '${flagsToReport.join("")}'`;
return `Invalid flags supplied to RegExp constructor '${flagsToCheck}'`;
}

return {
Expand All @@ -171,7 +176,7 @@ module.exports = {
});
}

let message = validateRegExpFlags(flags, flagsToCheck);
let message = validateRegExpFlags(flags, flagsToCheck, allFlags);

if (message) {
report(node, message);
Expand Down
21 changes: 15 additions & 6 deletions tests/lib/rules/no-invalid-regexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ ruleTester.run("no-invalid-regexp", rule, {
options: [{ allowConstructorFlags: ["a"] }],
errors: [{
messageId: "regexMessage",
data: { message: "Invalid flags supplied to RegExp constructor 'aa'" },
data: { message: "Duplicate flags ('a') supplied to RegExp constructor" },
type: "NewExpression"
}]
},
Expand All @@ -227,7 +227,7 @@ ruleTester.run("no-invalid-regexp", rule, {
options: [{ allowConstructorFlags: ["a", "a"] }],
errors: [{
messageId: "regexMessage",
data: { message: "Invalid flags supplied to RegExp constructor 'aa'" },
data: { message: "Duplicate flags ('a') supplied to RegExp constructor" },
type: "NewExpression"
}]
},
Expand All @@ -245,7 +245,7 @@ ruleTester.run("no-invalid-regexp", rule, {
options: [{ allowConstructorFlags: ["a", "z"] }],
errors: [{
messageId: "regexMessage",
data: { message: "Invalid flags supplied to RegExp constructor 'aa'" },
data: { message: "Duplicate flags ('a') supplied to RegExp constructor" },
type: "NewExpression"
}]
},
Expand All @@ -254,7 +254,7 @@ ruleTester.run("no-invalid-regexp", rule, {
options: [{ allowConstructorFlags: ["a", "z"] }],
errors: [{
messageId: "regexMessage",
data: { message: "Invalid flags supplied to RegExp constructor 'zz'" },
data: { message: "Duplicate flags ('z') supplied to RegExp constructor" },
type: "NewExpression"
}]
},
Expand All @@ -263,7 +263,7 @@ ruleTester.run("no-invalid-regexp", rule, {
options: [{ allowConstructorFlags: ["a"] }],
errors: [{
messageId: "regexMessage",
data: { message: "Invalid flags supplied to RegExp constructor 'aa'" },
data: { message: "Duplicate flags ('a') supplied to RegExp constructor" },
type: "NewExpression"
}]
},
Expand All @@ -272,7 +272,16 @@ ruleTester.run("no-invalid-regexp", rule, {
options: [{ allowConstructorFlags: ["u"] }],
errors: [{
messageId: "regexMessage",
data: { message: "Invalid flags supplied to RegExp constructor 'uu'" },
data: { message: "Duplicate flags ('u') supplied to RegExp constructor" },
type: "NewExpression"
}]
},
{
code: "new RegExp('.', 'ouo');",
options: [{ allowConstructorFlags: ["u"] }],
errors: [{
messageId: "regexMessage",
data: { message: "Invalid flags supplied to RegExp constructor 'oo'" },
type: "NewExpression"
}]
},
Expand Down

0 comments on commit 183b459

Please sign in to comment.








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/eslint/eslint/commit/183b459b72be5c1e359985b1584f73421dfb1484

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy