diff --git a/docs/rules/index.md b/docs/rules/index.md index 59a69421..4316b782 100644 --- a/docs/rules/index.md +++ b/docs/rules/index.md @@ -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. | | diff --git a/docs/rules/no-regexp-escape.md b/docs/rules/no-regexp-escape.md new file mode 100644 index 00000000..674be9d3 --- /dev/null +++ b/docs/rules/no-regexp-escape.md @@ -0,0 +1,32 @@ +--- +title: "es-x/no-regexp-escape" +description: "disallow `RegExp.escape` function" +--- + +# es-x/no-regexp-escape +> disallow `RegExp.escape` function + +- ❗ ***This rule has not been released yet.*** +- ✅ 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: + + + +```js +/*eslint es-x/no-regexp-escape: error */ +RegExp.escape(s) +``` + + + +## 📚 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 diff --git a/lib/configs/flat/no-new-in-esnext.js b/lib/configs/flat/no-new-in-esnext.js index 1defda7d..45c0559d 100644 --- a/lib/configs/flat/no-new-in-esnext.js +++ b/lib/configs/flat/no-new-in-esnext.js @@ -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", diff --git a/lib/configs/no-new-in-esnext.js b/lib/configs/no-new-in-esnext.js index 069a1c55..4974777c 100644 --- a/lib/configs/no-new-in-esnext.js +++ b/lib/configs/no-new-in-esnext.js @@ -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", diff --git a/lib/index.js b/lib/index.js index f49eb1ae..840ae41d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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"), diff --git a/lib/rules/no-regexp-escape.js b/lib/rules/no-regexp-escape.js new file mode 100644 index 00000000..6b50cdb1 --- /dev/null +++ b/lib/rules/no-regexp-escape.js @@ -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"], + }) + }, +} diff --git a/lib/util/type-checker/es-types.js b/lib/util/type-checker/es-types.js index 98cc05ee..e5f831a9 100644 --- a/lib/util/type-checker/es-types.js +++ b/lib/util/type-checker/es-types.js @@ -185,6 +185,8 @@ const WELLKNOWN_GLOBALS = { prototypeType: "RegExp", /** @type {Record} */ properties: { + escape: { type: "Function", return: { type: "String" } }, + "$&": { type: "String" }, "$'": { type: "String" }, "$`": { type: "String" }, diff --git a/lib/util/well-known-properties.js b/lib/util/well-known-properties.js index 806d1752..c24bcc14 100644 --- a/lib/util/well-known-properties.js +++ b/lib/util/well-known-properties.js @@ -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% ] diff --git a/tests/lib/rules/no-regexp-escape.js b/tests/lib/rules/no-regexp-escape.js new file mode 100644 index 00000000..7d0d2efb --- /dev/null +++ b/tests/lib/rules/no-regexp-escape.js @@ -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."], + }, + ], +})