8000 Docs: fix no-invalid-regexp docs regarding ecmaVersion (#13991) · eslint/eslint@672deb0 · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit 672deb0

Browse files
authored
Docs: fix no-invalid-regexp docs regarding ecmaVersion (#13991)
1 parent 179a910 commit 672deb0

File tree

2 files changed

+116
-32
lines changed

2 files changed

+116
-32
lines changed

docs/rules/no-invalid-regexp.md

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,9 @@ new RegExp
3030
this.RegExp('[')
3131
```
3232

33-
## Environments
33+
Please note that this rule validates regular expressions per the latest ECMAScript specification, regardless of your parser settings.
3434

35-
ECMAScript 6 adds the following flag arguments to the `RegExp` constructor:
36-
37-
* `"u"` ([unicode](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-get-regexp.prototype.unicode))
38-
* `"y"` ([sticky](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-get-regexp.prototype.sticky))
39-
40-
You can enable these to be recognized as valid by setting the ECMAScript version to 6 in your [ESLint configuration](../user-guide/configuring).
41-
42-
If you want to allow additional constructor flags for any reason, you can specify them using an `allowConstructorFlags` option in `.eslintrc`. These flags will then be ignored by the rule regardless of the `ecmaVersion` setting.
35+
If you want to allow additional constructor flags for any reason, you can specify them using the `allowConstructorFlags` option. These flags will then be ignored by the rule.
4336

4437
## Options
4538

@@ -49,14 +42,14 @@ This rule has an object option for exceptions:
4942

5043
### allowConstructorFlags
5144

52-
Examples of **correct** code for this rule with the `{ "allowConstructorFlags": ["u", "y"] }` option:
45+
Examples of **correct** code for this rule with the `{ "allowConstructorFlags": ["a", "z"] }` option:
5346

5447
```js
55-
/*eslint no-invalid-regexp: ["error", { "allowConstructorFlags": ["u", "y"] }]*/
48+
/*eslint no-invalid-regexp: ["error", { "allowConstructorFlags": ["a", "z"] }]*/
5649

57-
new RegExp('.', 'y')
50+
new RegExp('.', 'a')
5851

59-
new RegExp('.', 'yu')
52+
new RegExp('.', 'az')
6053
```
6154

6255
## Further Reading

tests/lib/rules/no-invalid-regexp.js

Lines changed: 110 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,68 @@ ruleTester.run("no-invalid-regexp", rule, {
2424
"new RegExp('.', 'im')",
2525
"global.RegExp('\\\\')",
2626
"new RegExp('.', y)",
27-
{ code: "new RegExp('.', 'y')", options: [{ allowConstructorFlags: ["y"] }] },
28-
{ code: "new RegExp('.', 'u')", options: [{ allowConstructorFlags: ["U"] }] },
29-
{ code: "new RegExp('.', 'yu')", options: [{ allowConstructorFlags: ["y", "u"] }] },
30-
{ code: "new RegExp('/', 'yu')", options: [{ allowConstructorFlags: ["y", "u"] }] },
31-
{ code: "new RegExp('\\/', 'yu')", options: [{ allowConstructorFlags: ["y", "u"] }] },
32-
{ code: "new RegExp('.', 'y')", parserOptions: { ecmaVersion: 6 } },
33-
{ code: "new RegExp('.', 'u')", parserOptions: { ecmaVersion: 6 } },
34-
{ code: "new RegExp('.', 'yu')", parserOptions: { ecmaVersion: 6 } },
35-
{ code: "new RegExp('/', 'yu')", parserOptions: { ecmaVersion: 6 } },
36-
{ code: "new RegExp('\\/', 'yu')", parserOptions: { ecmaVersion: 6 } },
37-
{ code: "new RegExp('\\\\u{65}', 'u')", parserOptions: { ecmaVersion: 2015 } },
38-
{ code: "new RegExp('[\\\\u{0}-\\\\u{1F}]', 'u')", parserOptions: { ecmaVersion: 2015 } },
39-
{ code: "new RegExp('.', 's')", parserOptions: { ecmaVersion: 2018 } },
40-
{ code: "new RegExp('(?<=a)b')", parserOptions: { ecmaVersion: 2018 } },
41-
{ code: "new RegExp('(?<!a)b')", parserOptions: { ecmaVersion: 2018 } },
42-
{ code: "new RegExp('(?<a>b)\\k<a>')", parserOptions: { ecmaVersion: 2018 } },
43-
{ code: "new RegExp('(?<a>b)\\k<a>', 'u')", parserOptions: { ecmaVersion: 2018 } },
44-
{ code: "new RegExp('\\\\p{Letter}', 'u')", parserOptions: { ecmaVersion: 2018 } },
27+
"new RegExp('.', 'y')",
28+
"new RegExp('.', 'u')",
29+
"new RegExp('.', 'yu')",
30+
"new RegExp('/', 'yu')",
31+
"new RegExp('\\/', 'yu')",
32+
"new RegExp('\\\\u{65}', 'u')",
33+
"new RegExp('\\\\u{65}*', 'u')",
34+
"new RegExp('[\\\\u{0}-\\\\u{1F}]', 'u')",
35+
"new RegExp('.', 's')",
36+
"new RegExp('(?<=a)b')",
37+
"new RegExp('(?<!a)b')",
38+
"new RegExp('(?<a>b)\\k<a>')",
39+
"new RegExp('(?<a>b)\\k<a>', 'u')",
40+
"new RegExp('\\\\p{Letter}', 'u')",
4541

4642
// ES2020
4743
"new RegExp('(?<\\\\ud835\\\\udc9c>.)', 'g')",
4844
"new RegExp('(?<\\\\u{1d49c}>.)', 'g')",
4945
"new RegExp('(?<𝒜>.)', 'g');",
50-
"new RegExp('\\\\p{Script=Nandinagari}', 'u');"
46+
"new RegExp('\\\\p{Script=Nandinagari}', 'u');",
47+
48+
// allowConstructorFlags
49+
{
50+
code: "new RegExp('.', 'g')",
51+
options: [{ allowConstructorFlags: [] }]
52+
},
53+
{
54+
code: "new RegExp('.', 'g')",
55+
options: [{ allowConstructorFlags: ["a"] }]
56+
},
57+
{
58+
code: "new RegExp('.', 'a')",
59+
options: [{ allowConstructorFlags: ["a"] }]
60+
},
61+
{
62+
code: "new RegExp('.', 'ag')",
63+
options: [{ allowConstructorFlags: ["a"] }]
64+
},
65+
{
66+
code: "new RegExp('.', 'ga')",
67+
options: [{ allowConstructorFlags: ["a"] }]
68+
},
69+
{
70+
code: "new RegExp('.', 'a')",
71+
options: [{ allowConstructorFlags: ["a", "z"] }]
72+
},
73+
{
74+
code: "new RegExp('.', 'z')",
75+
options: [{ allowConstructorFlags: ["a", "z"] }]
76+
},
77+
{
78+
code: "new RegExp('.', 'az')",
79+
options: [{ allowConstructorFlags: ["a", "z"] }]
80+
},
81+
{
82+
code: "new RegExp('.', 'za')",
83+
options: [{ allowConstructorFlags: ["a", "z"] }]
84+
},
85+
{
86+
code: "new RegExp('.', 'agz')",
87+
options: [{ allowConstructorFlags: ["a", "z"] }]
88+
}
5189
],
5290
invalid: [
5391
{
@@ -66,6 +104,42 @@ ruleTester.run("no-invalid-regexp", rule, {
66104
type: "CallExpression"
67105
}]
68106
},
107+
{
108+
code: "RegExp('.', 'a');",
109+
options: [{}],
110+
errors: [{
111+
messageId: "regexMessage",
112+
data: { message: "Invalid flags supplied to RegExp constructor 'a'" },
113+
type: "CallExpression"
114+
}]
115+
},
116+
{
117+
code: "new RegExp('.', 'a');",
118+
options: [{ allowConstructorFlags: [] }],
119+
errors: [{
120+
messageId: "regexMessage",
121+
data: { message: "Invalid flags supplied to RegExp constructor 'a'" },
122+
type: "NewExpression"
123+
}]
124+
},
125+
{
126+
code: "new RegExp('.', 'z');",
127+
options: [{ allowConstructorFlags: ["a"] }],
128+
errors: [{
129+
messageId: "regexMessage",
130+
data: { message: "Invalid flags supplied to RegExp constructor 'z'" },
131+
type: "NewExpression"
132+
}]
133+
},
134+
{
135+
code: "new RegExp('.', 'az');",
136+
options: [{ allowConstructorFlags: ["z"] }],
137+
errors: [{
138+
messageId: "regexMessage",
139+
data: { message: "Invalid flags supplied to RegExp constructor 'a'" },
140+
type: "NewExpression"
141+
}]
142+
},
69143
{
70144
code: "new RegExp(')');",
71145
errors: [{
@@ -74,6 +148,23 @@ ruleTester.run("no-invalid-regexp", rule, {
74148
type: "NewExpression"
75149
}]
76150
},
151+
{
152+
code: String.raw`new RegExp('\\a', 'u');`,
153+
errors: [{
154+
messageId: "regexMessage",
155+
data: { message: "Invalid regular expression: /\\a/u: Invalid escape" },
156+
type: "NewExpression"
157+
}]
158+
},
159+
{
160+
code: String.raw`new RegExp('\\a', 'u');`,
161+
options: [{ allowConstructorFlags: ["u"] }],
162+
errors: [{
163+
messageId: "regexMessage",
164+
data: { message: "Invalid regular expression: /\\a/u: Invalid escape" },
165+
type: "NewExpression"
166+
}]
167+
},
77168

78169
// https://github.com/eslint/eslint/issues/10861
79170
{

0 commit comments

Comments
 (0)
0