Skip to content

Commit 1e99295

Browse files
authored
Add es-x/no-regexp-escape rule (#238)
1 parent 48a7464 commit 1e99295

File tree

9 files changed

+84
-0
lines changed

9 files changed

+84
-0
lines changed

docs/rules/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ There is a config that enables the rules in this category: [`no-new-in-esnext`]
2727
| [es-x/no-json-modules](./no-json-modules.md) | disallow JSON Modules. | |
2828
| [es-x/no-promise-try](./no-promise-try.md) | disallow `Promise.try` function. | |
2929
| [es-x/no-regexp-duplicate-named-capturing-groups](./no-regexp-duplicate-named-capturing-groups.md) | disallow RegExp duplicate named capturing groups. | |
30+
| [es-x/no-regexp-escape](./no-regexp-escape.md) | disallow `RegExp.escape` function. | |
3031
| [es-x/no-regexp-modifiers](./no-regexp-modifiers.md) | disallow RegExp Modifiers. | |
3132
| [es-x/no-set-prototype-difference](./no-set-prototype-difference.md) | disallow the `Set.prototype.difference` method. | |
3233
| [es-x/no-set-prototype-intersection](./no-set-prototype-intersection.md) | disallow the `Set.prototype.intersection` method. | |

docs/rules/no-regexp-escape.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: "es-x/no-regexp-escape"
3+
description: "disallow `RegExp.escape` function"
4+
---
5+
6+
# es-x/no-regexp-escape
7+
> disallow `RegExp.escape` function
8+
9+
- ❗ <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
10+
- ✅ The following configurations enable this rule: [no-new-in-esnext]
11+
12+
This rule reports ES2025 [`RegExp.escape`](https://github.com/tc39/proposal-regex-escaping) as errors.
13+
14+
## 💡 Examples
15+
16+
⛔ Examples of **incorrect** code for this rule:
17+
18+
<eslint-playground type="bad">
19+
20+
```js
21+
/*eslint es-x/no-regexp-escape: error */
22+
RegExp.escape(s)
23+
```
24+
25+
</eslint-playground>
26+
27+
## 📚 References
28+
29+
- [Rule source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/lib/rules/no-regexp-escape.js)
30+
- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-regexp-escape.js)
31+
32+
[no-new-in-esnext]: ../configs/index.md#no-new-in-esnext

lib/configs/flat/no-new-in-esnext.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module.exports = {
2828
"es-x/no-json-modules": "error",
2929
"es-x/no-promise-try": "error",
3030
"es-x/no-regexp-duplicate-named-capturing-groups": "error",
31+
"es-x/no-regexp-escape": "error",
3132
"es-x/no-regexp-modifiers": "error",
3233
"es-x/no-set-prototype-difference": "error",
3334
"es-x/no-set-prototype-intersection": "error",

lib/configs/no-new-in-esnext.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module.exports = {
2424
"es-x/no-json-modules": "error",
2525
"es-x/no-promise-try": "error",
2626
"es-x/no-regexp-duplicate-named-capturing-groups": "error",
27+
"es-x/no-regexp-escape": "error",
2728
"es-x/no-regexp-modifiers": "error",
2829
"es-x/no-set-prototype-difference": "error",
2930
"es-x/no-set-prototype-intersection": "error",

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ module.exports = {
382382
"no-reflect": require("./rules/no-reflect"),
383383
"no-regexp-d-flag": require("./rules/no-regexp-d-flag"),
384384
"no-regexp-duplicate-named-capturing-groups": require("./rules/no-regexp-duplicate-named-capturing-groups"),
385+
"no-regexp-escape": require("./rules/no-regexp-escape"),
385386
"no-regexp-lookbehind-assertions": require("./rules/no-regexp-lookbehind-assertions"),
386387
"no-regexp-modifiers": require("./rules/no-regexp-modifiers"),
387388
"no-regexp-named-capture-groups": require("./rules/no-regexp-named-capture-groups"),

lib/rules/no-regexp-escape.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @author Yosuke Ota
3+
* See LICENSE file in root directory for full license.
4+
*/
5+
"use strict"
6+
7+
const {
8+
defineStaticPropertiesHandler,
9+
} = require("../util/define-static-properties-handler")
10+
11+
module.exports = {
12+
meta: {
13+
docs: {
14+
description: "disallow `RegExp.escape` function",
15+
category: "ES2025",
16+
recommended: false,
17+
url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-regexp-escape.html",
18+
},
19+
fixable: null,
20+
messages: {
21+
forbidden: "ES2025 '{{name}}' is forbidden.",
22+
},
23+
schema: [],
24+
type: "problem",
25+
},
26+
create(context) {
27+
return defineStaticPropertiesHandler(context, {
28+
RegExp: ["escape"],
29+
})
30+
},
31+
}

lib/util/type-checker/es-types.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ const WELLKNOWN_GLOBALS = {
185185
prototypeType: "RegExp",
186186
/** @type {Record<import('./types').RegExpProperty,TypeInfo|undefined>} */
187187
properties: {
188+
escape: { type: "Function", return: { type: "String" } },
189+
188190
"$&": { type: "String" },
189191
"$'": { type: "String" },
190192
"$`": { type: "String" },

lib/util/well-known-properties.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ const regexpProperties = new Set([
395395
...functionPrototypeProperties,
396396

397397
// https://tc39.es/ecma262/multipage/text-processing.html#sec-properties-of-the-regexp-constructor
398+
"escape",
398399
"prototype",
399400
// [ %Symbol.species% ]
400401

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict"
2+
3+
const RuleTester = require("../../tester")
4+
const rule = require("../../../lib/rules/no-regexp-escape.js")
5+
6+
new RuleTester().run("no-regexp-escape", rule, {
7+
valid: ["RegExp.$1"],
8+
invalid: [
9+
{
10+
code: "RegExp.escape",
11+
errors: ["ES2025 'RegExp.escape' is forbidden."],
12+
},
13+
],
14+
})

0 commit comments

Comments
 (0)