Repository: eslint-community/eslint-plugin-es-x
Pull request #212 (2024-10-31 16:05:58)

Add `es-x/no-promise-try` rule
related to https://github.com/eslint-community/eslint-plugin-es-x/issues/201


Patch:
diff --git a/docs/rules/index.md b/docs/rules/index.md
index fceeb4b..fdb38ac 100644
--- a/docs/rules/index.md
+++ b/docs/rules/index.md
@@ -13,6 +13,7 @@ There is a config that enables the rules in this category: [`no-new-in-esnext`]
 | [es-x/no-dynamic-import-options](./no-dynamic-import-options.md) | disallow second parameter to `import()`. |  |
 | [es-x/no-import-attributes](./no-import-attributes.md) | disallow Import Attributes. |  |
 | [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-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. |  |
diff --git a/docs/rules/no-promise-try.md b/docs/rules/no-promise-try.md
new file mode 100644
index 0000000..65c0444
--- /dev/null
+++ b/docs/rules/no-promise-try.md
@@ -0,0 +1,32 @@
+---
+title: "es-x/no-promise-try"
+description: "disallow `Promise.try` function"
+---
+
+# es-x/no-promise-try
+> disallow `Promise.try` 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 [`Promise.try`](https://github.com/tc39/proposal-promise-try) as errors.
+
+## 💡 Examples
+
+⛔ Examples of **incorrect** code for this rule:
+
+<eslint-playground type="bad">
+
+```js
+/*eslint es-x/no-promise-try: error */
+const p = Promise.try(f)
+```
+
+</eslint-playground>
+
+## 📚 References
+
+- [Rule source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/lib/rules/no-promise-try.js)
+- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-promise-try.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 f100a61..1aa5b33 100644
--- a/lib/configs/flat/no-new-in-esnext.js
+++ b/lib/configs/flat/no-new-in-esnext.js
@@ -14,6 +14,7 @@ module.exports = {
         "es-x/no-dynamic-import-options": "error",
         "es-x/no-import-attributes": "error",
         "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-modifiers": "error",
         "es-x/no-set-prototype-difference": "error",
diff --git a/lib/configs/no-new-in-esnext.js b/lib/configs/no-new-in-esnext.js
index d6eed7b..6faa674 100644
--- a/lib/configs/no-new-in-esnext.js
+++ b/lib/configs/no-new-in-esnext.js
@@ -10,6 +10,7 @@ module.exports = {
         "es-x/no-dynamic-import-options": "error",
         "es-x/no-import-attributes": "error",
         "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-modifiers": "error",
         "es-x/no-set-prototype-difference": "error",
diff --git a/lib/index.js b/lib/index.js
index 9874280..bc74d59 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -295,6 +295,7 @@ module.exports = {
         "no-promise-all-settled": require("./rules/no-promise-all-settled"),
         "no-promise-any": require("./rules/no-promise-any"),
         "no-promise-prototype-finally": require("./rules/no-promise-prototype-finally"),
+        "no-promise-try": require("./rules/no-promise-try"),
         "no-promise-withresolvers": require("./rules/no-promise-withresolvers"),
         "no-property-shorthands": require("./rules/no-property-shorthands"),
         "no-proxy": require("./rules/no-proxy"),
diff --git a/lib/rules/no-promise-try.js b/lib/rules/no-promise-try.js
new file mode 100644
index 0000000..890db3f
--- /dev/null
+++ b/lib/rules/no-promise-try.js
@@ -0,0 +1,46 @@
+/**
+ * @author Yosuke Ota
+ * See LICENSE file in root directory for full license.
+ */
+"use strict"
+
+const { READ, ReferenceTracker } = require("@eslint-community/eslint-utils")
+const { getSourceCode } = require("eslint-compat-utils")
+
+module.exports = {
+    meta: {
+        docs: {
+            description: "disallow `Promise.try` function",
+            category: "ES2025",
+            recommended: false,
+            url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-promise-try.html",
+        },
+        fixable: null,
+        messages: {
+            forbidden: "ES2025 '{{name}}' is forbidden.",
+        },
+        schema: [],
+        type: "problem",
+    },
+    create(context) {
+        return {
+            "Program:exit"(program) {
+                const sourceCode = getSourceCode(context)
+                const tracker = new ReferenceTracker(
+                    sourceCode.getScope(program),
+                )
+                for (const { node, path } of tracker.iterateGlobalReferences({
+                    Promise: {
+                        try: { [READ]: true },
+                    },
+                })) {
+                    context.report({
+                        node,
+                        messageId: "forbidden",
+                        data: { name: path.join(".") },
+                    })
+                }
+            },
+        }
+    },
+}


Test patch:
diff --git a/tests/lib/rules/no-promise-try.js b/tests/lib/rules/no-promise-try.js
new file mode 100644
index 0000000..2187e62
--- /dev/null
+++ b/tests/lib/rules/no-promise-try.js
@@ -0,0 +1,18 @@
+/**
+ * @author Yosuke Ota
+ * See LICENSE file in root directory for full license.
+ */
+"use strict"
+
+const RuleTester = require("../../tester")
+const rule = require("../../../lib/rules/no-promise-try.js")
+
+new RuleTester().run("no-promise-try", rule, {
+    valid: ["Promise.all"],
+    invalid: [
+        {
+            code: "Promise.try",
+            errors: ["ES2025 'Promise.try' is forbidden."],
+        },
+    ],
+})