Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ There is a config that enables the rules in this category: [`no-new-in-esnext`]
| [es-x/no-json-modules](./no-json-modules.md) | disallow JSON Modules. | |
| [es-x/no-promise-try](./no-promise-try.md) | disallow `Promise.try` function. | |
| [es-x/no-regexp-duplicate-named-capturing-groups](./no-regexp-duplicate-named-capturing-groups.md) | disallow RegExp duplicate named capturing groups. | |
| [es-x/no-regexp-escape](./no-regexp-escape.md) | disallow `RegExp.escape` function. | |
| [es-x/no-regexp-modifiers](./no-regexp-modifiers.md) | disallow RegExp Modifiers. | |
| [es-x/no-set-prototype-difference](./no-set-prototype-difference.md) | disallow the `Set.prototype.difference` method. | |
| [es-x/no-set-prototype-intersection](./no-set-prototype-intersection.md) | disallow the `Set.prototype.intersection` method. | |
Expand Down
32 changes: 32 additions & 0 deletions docs/rules/no-regexp-escape.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
title: "es-x/no-regexp-escape"
description: "disallow `RegExp.escape` function"
---

# es-x/no-regexp-escape
> disallow `RegExp.escape` function

- ❗ <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
- ✅ The following configurations enable this rule: [no-new-in-esnext]

This rule reports ES2025 [`RegExp.escape`](https://github.com/tc39/proposal-regex-escaping) as errors.

## 💡 Examples

⛔ Examples of **incorrect** code for this rule:

<eslint-playground type="bad">

```js
/*eslint es-x/no-regexp-escape: error */
RegExp.escape(s)
```

</eslint-playground>

## 📚 References

- [Rule source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/lib/rules/no-regexp-escape.js)
- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-regexp-escape.js)

[no-new-in-esnext]: ../configs/index.md#no-new-in-esnext
1 change: 1 addition & 0 deletions lib/configs/flat/no-new-in-esnext.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = {
"es-x/no-json-modules": "error",
"es-x/no-promise-try": "error",
"es-x/no-regexp-duplicate-named-capturing-groups": "error",
"es-x/no-regexp-escape": "error",
"es-x/no-regexp-modifiers": "error",
"es-x/no-set-prototype-difference": "error",
"es-x/no-set-prototype-intersection": "error",
Expand Down
1 change: 1 addition & 0 deletions lib/configs/no-new-in-esnext.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = {
"es-x/no-json-modules": "error",
"es-x/no-promise-try": "error",
"es-x/no-regexp-duplicate-named-capturing-groups": "error",
"es-x/no-regexp-escape": "error",
"es-x/no-regexp-modifiers": "error",
"es-x/no-set-prototype-difference": "error",
"es-x/no-set-prototype-intersection": "error",
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ module.exports = {
"no-reflect": require("./rules/no-reflect"),
"no-regexp-d-flag": require("./rules/no-regexp-d-flag"),
"no-regexp-duplicate-named-capturing-groups": require("./rules/no-regexp-duplicate-named-capturing-groups"),
"no-regexp-escape": require("./rules/no-regexp-escape"),
"no-regexp-lookbehind-assertions": require("./rules/no-regexp-lookbehind-assertions"),
"no-regexp-modifiers": require("./rules/no-regexp-modifiers"),
"no-regexp-named-capture-groups": require("./rules/no-regexp-named-capture-groups"),
Expand Down
31 changes: 31 additions & 0 deletions lib/rules/no-regexp-escape.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @author Yosuke Ota
* See LICENSE file in root directory for full license.
*/
"use strict"

const {
defineStaticPropertiesHandler,
} = require("../util/define-static-properties-handler")

module.exports = {
meta: {
docs: {
description: "disallow `RegExp.escape` function",
category: "ES2025",
recommended: false,
url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-regexp-escape.html",
},
fixable: null,
messages: {
forbidden: "ES2025 '{{name}}' is forbidden.",
},
schema: [],
type: "problem",
},
create(context) {
return defineStaticPropertiesHandler(context, {
RegExp: ["escape"],
})
},
}
2 changes: 2 additions & 0 deletions lib/util/type-checker/es-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ const WELLKNOWN_GLOBALS = {
prototypeType: "RegExp",
/** @type {Record<import('./types').RegExpProperty,TypeInfo|undefined>} */
properties: {
escape: { type: "Function", return: { type: "String" } },

"$&": { type: "String" },
"$'": { type: "String" },
"$`": { type: "String" },
Expand Down
1 change: 1 addition & 0 deletions lib/util/well-known-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ const regexpProperties = new Set([
...functionPrototypeProperties,

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

Expand Down
14 changes: 14 additions & 0 deletions tests/lib/rules/no-regexp-escape.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict"

const RuleTester = require("../../tester")
const rule = require("../../../lib/rules/no-regexp-escape.js")

new RuleTester().run("no-regexp-escape", rule, {
valid: ["RegExp.$1"],
invalid: [
{
code: "RegExp.escape",
errors: ["ES2025 'RegExp.escape' is forbidden."],
},
],
})