pve-eslint/eslint/tests/lib/rules/no-invalid-regexp.js
Thomas Lamprecht 456be15ea9 import eslint 7.18.0
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2021-01-19 16:04:55 +01:00

180 lines
5.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @fileoverview Validate strings passed to the RegExp constructor
* @author Michael Ficarra
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/no-invalid-regexp"),
{ RuleTester } = require("../../../lib/rule-tester");
const ruleTester = new RuleTester();
ruleTester.run("no-invalid-regexp", rule, {
valid: [
"RegExp('')",
"RegExp()",
"RegExp('.', 'g')",
"new RegExp('.')",
"new RegExp",
"new RegExp('.', 'im')",
"global.RegExp('\\\\')",
"new RegExp('.', y)",
"new RegExp('.', 'y')",
"new RegExp('.', 'u')",
"new RegExp('.', 'yu')",
"new RegExp('/', 'yu')",
"new RegExp('\\/', 'yu')",
"new RegExp('\\\\u{65}', 'u')",
"new RegExp('\\\\u{65}*', 'u')",
"new RegExp('[\\\\u{0}-\\\\u{1F}]', 'u')",
"new RegExp('.', 's')",
"new RegExp('(?<=a)b')",
"new RegExp('(?<!a)b')",
"new RegExp('(?<a>b)\\k<a>')",
"new RegExp('(?<a>b)\\k<a>', 'u')",
"new RegExp('\\\\p{Letter}', 'u')",
// ES2020
"new RegExp('(?<\\\\ud835\\\\udc9c>.)', 'g')",
"new RegExp('(?<\\\\u{1d49c}>.)', 'g')",
"new RegExp('(?<𝒜>.)', 'g');",
"new RegExp('\\\\p{Script=Nandinagari}', 'u');",
// allowConstructorFlags
{
code: "new RegExp('.', 'g')",
options: [{ allowConstructorFlags: [] }]
},
{
code: "new RegExp('.', 'g')",
options: [{ allowConstructorFlags: ["a"] }]
},
{
code: "new RegExp('.', 'a')",
options: [{ allowConstructorFlags: ["a"] }]
},
{
code: "new RegExp('.', 'ag')",
options: [{ allowConstructorFlags: ["a"] }]
},
{
code: "new RegExp('.', 'ga')",
options: [{ allowConstructorFlags: ["a"] }]
},
{
code: "new RegExp('.', 'a')",
options: [{ allowConstructorFlags: ["a", "z"] }]
},
{
code: "new RegExp('.', 'z')",
options: [{ allowConstructorFlags: ["a", "z"] }]
},
{
code: "new RegExp('.', 'az')",
options: [{ allowConstructorFlags: ["a", "z"] }]
},
{
code: "new RegExp('.', 'za')",
options: [{ allowConstructorFlags: ["a", "z"] }]
},
{
code: "new RegExp('.', 'agz')",
options: [{ allowConstructorFlags: ["a", "z"] }]
}
],
invalid: [
{
code: "RegExp('[');",
errors: [{
messageId: "regexMessage",
data: { message: "Invalid regular expression: /[/: Unterminated character class" },
type: "CallExpression"
}]
},
{
code: "RegExp('.', 'z');",
errors: [{
messageId: "regexMessage",
data: { message: "Invalid flags supplied to RegExp constructor 'z'" },
type: "CallExpression"
}]
},
{
code: "RegExp('.', 'a');",
options: [{}],
errors: [{
messageId: "regexMessage",
data: { message: "Invalid flags supplied to RegExp constructor 'a'" },
type: "CallExpression"
}]
},
{
code: "new RegExp('.', 'a');",
options: [{ allowConstructorFlags: [] }],
errors: [{
messageId: "regexMessage",
data: { message: "Invalid flags supplied to RegExp constructor 'a'" },
type: "NewExpression"
}]
},
{
code: "new RegExp('.', 'z');",
options: [{ allowConstructorFlags: ["a"] }],
errors: [{
messageId: "regexMessage",
data: { message: "Invalid flags supplied to RegExp constructor 'z'" },
type: "NewExpression"
}]
},
{
code: "new RegExp('.', 'az');",
options: [{ allowConstructorFlags: ["z"] }],
errors: [{
messageId: "regexMessage",
data: { message: "Invalid flags supplied to RegExp constructor 'a'" },
type: "NewExpression"
}]
},
{
code: "new RegExp(')');",
errors: [{
messageId: "regexMessage",
data: { message: "Invalid regular expression: /)/: Unmatched ')'" },
type: "NewExpression"
}]
},
{
code: String.raw`new RegExp('\\a', 'u');`,
errors: [{
messageId: "regexMessage",
data: { message: "Invalid regular expression: /\\a/u: Invalid escape" },
type: "NewExpression"
}]
},
{
code: String.raw`new RegExp('\\a', 'u');`,
options: [{ allowConstructorFlags: ["u"] }],
errors: [{
messageId: "regexMessage",
data: { message: "Invalid regular expression: /\\a/u: Invalid escape" },
type: "NewExpression"
}]
},
// https://github.com/eslint/eslint/issues/10861
{
code: String.raw`new RegExp('\\');`,
errors: [{
messageId: "regexMessage",
data: { message: "Invalid regular expression: /\\/: \\ at end of pattern" },
type: "NewExpression"
}]
}
]
});