Skip to content

Commit

Permalink
Add es-x/no-promise-try rule (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi authored Nov 1, 2024
1 parent 9c7f4b9 commit 7887af8
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. | |
Expand Down
32 changes: 32 additions & 0 deletions docs/rules/no-promise-try.md
Original file line number Diff line number Diff line change
@@ -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
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 @@ -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",
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 @@ -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",
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
46 changes: 46 additions & 0 deletions lib/rules/no-promise-try.js
Original file line number Diff line number Diff line change
@@ -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(".") },
})
}
},
}
},
}
18 changes: 18 additions & 0 deletions tests/lib/rules/no-promise-try.js
Original file line number Diff line number Diff line change
@@ -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."],
},
],
})

0 comments on commit 7887af8

Please sign in to comment.