Skip to content

Commit 9cecb8a

Browse files
authored
docs: document \c control letter escapes in no-control-regex (#21286)
* fix: no-control-regex false negative for \c control letter escapes The rule detected \x, \u, and raw control characters in regex patterns but missed \cA-\cZ (and \ca-\cz) control letter escapes, which also produce control characters in the U+0000-U+001F range. Fixes #21281 * docs: document \c control letter escapes in no-control-regex Clarify that \cA-\cZ (and \ca-\cz) control letter escapes are intentionally allowed by this rule, since they explicitly signal intent to match a control character, unlike the hex/unicode/raw forms the rule disallows. Add tests covering this behavior. Fixes #21281
1 parent c6cc6c5 commit 9cecb8a

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

‎docs/src/rules/no-control-regex.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ The following elements of regular expression patterns are considered possible er
2121
* Unicode code point escapes from `\u{0}` to `\u{1F}`.
2222
* Unescaped raw characters from U+0000 to U+001F.
2323

24-
Control escapes such as `\t` and `\n` are allowed by this rule.
24+
Control escapes such as `\t` and `\n` are allowed by this rule. Control letter escapes such as `\cA`-`\cZ` (and `\ca`-`\cz`) are also allowed, since writing a control character this way makes the intent to match a control character explicit, unlike the hexadecimal, Unicode, and raw forms above, which can obscure that intent.
2525

2626
Examples of **incorrect** code for this rule:
2727

@@ -56,6 +56,7 @@ const pattern5 = /\n/;
5656
const pattern6 = new RegExp("\x20");
5757
const pattern7 = new RegExp("\\t");
5858
const pattern8 = new RegExp("\\n");
59+
const pattern9 = /\cJ/; // control letter escape for U+000A
5960
```
6061

6162
:::

‎tests/lib/rules/no-control-regex.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ ruleTester.run("no-control-regex", rule, {
4646
code: String.raw`/[\u{20}--B]/v`,
4747
languageOptions: { ecmaVersion: 2024 },
4848
},
49+
String.raw`var regex = /\c1/`,
50+
String.raw`var regex = /\c$/`,
51+
String.raw`var regex = new RegExp('\\c1')`,
52+
String.raw`var regex = /\cA/`,
53+
String.raw`var regex = /\cz/`,
54+
String.raw`var regex = new RegExp('\\cA')`,
55+
{ code: String.raw`/\cA/u`, languageOptions: { ecmaVersion: 2015 } },
56+
{
57+
code: String.raw`/\cA/v`,
58+
languageOptions: { ecmaVersion: 2024 },
59+
},
4960
],
5061
invalid: [
5162
{
@@ -237,5 +248,14 @@ ruleTester.run("no-control-regex", rule, {
237248
},
238249
],
239250
},
251+
{
252+
code: String.raw`var regex = /\x1f\cA/`,
253+
errors: [
254+
{
255+
messageId: "unexpected",
256+
data: { controlChars: "\\x1f" },
257+
},
258+
],
259+
},
240260
],
241261
});

0 commit comments

Comments
 (0)