Skip to content

Commit 7887af8

Browse files
authored
Add es-x/no-promise-try rule (#212)
1 parent 9c7f4b9 commit 7887af8

File tree

7 files changed

+100
-0
lines changed

7 files changed

+100
-0
lines changed

docs/rules/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ There is a config that enables the rules in this category: [`no-new-in-esnext`]
1313
| [es-x/no-dynamic-import-options](./no-dynamic-import-options.md) | disallow second parameter to `import()`. | |
1414
| [es-x/no-import-attributes](./no-import-attributes.md) | disallow Import Attributes. | |
1515
| [es-x/no-json-modules](./no-json-modules.md) | disallow JSON Modules. | |
16+
| [es-x/no-promise-try](./no-promise-try.md) | disallow `Promise.try` function. | |
1617
| [es-x/no-regexp-duplicate-named-capturing-groups](./no-regexp-duplicate-named-capturing-groups.md) | disallow RegExp duplicate named capturing groups. | |
1718
| [es-x/no-regexp-modifiers](./no-regexp-modifiers.md) | disallow RegExp Modifiers. | |
1819
| [es-x/no-set-prototype-difference](./no-set-prototype-difference.md) | disallow the `Set.prototype.difference` method. | |

docs/rules/no-promise-try.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-promise-try"
3+
description: "disallow `Promise.try` function"
4+
---
5+
6+
# es-x/no-promise-try
7+
> disallow `Promise.try` 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 [`Promise.try`](https://github.com/tc39/proposal-promise-try) 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-promise-try: error */
22+
const p = Promise.try(f)
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-promise-try.js)
30+
- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-promise-try.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
@@ -14,6 +14,7 @@ module.exports = {
1414
"es-x/no-dynamic-import-options": "error",
1515
"es-x/no-import-attributes": "error",
1616
"es-x/no-json-modules": "error",
17+
"es-x/no-promise-try": "error",
1718
"es-x/no-regexp-duplicate-named-capturing-groups": "error",
1819
"es-x/no-regexp-modifiers": "error",
1920
"es-x/no-set-prototype-difference": "error",

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports = {
1010
"es-x/no-dynamic-import-options": "error",
1111
"es-x/no-import-attributes": "error",
1212
"es-x/no-json-modules": "error",
13+
"es-x/no-promise-try": "error",
1314
"es-x/no-regexp-duplicate-named-capturing-groups": "error",
1415
"es-x/no-regexp-modifiers": "error",
1516
"es-x/no-set-prototype-difference": "error",

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ module.exports = {
295295
"no-promise-all-settled": require("./rules/no-promise-all-settled"),
296296
"no-promise-any": require("./rules/no-promise-any"),
297297
"no-promise-prototype-finally": require("./rules/no-promise-prototype-finally"),
298+
"no-promise-try": require("./rules/no-promise-try"),
298299
"no-promise-withresolvers": require("./rules/no-promise-withresolvers"),
299300
"no-property-shorthands": require("./rules/no-property-shorthands"),
300301
"no-proxy": require("./rules/no-proxy"),

lib/rules/no-promise-try.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @author Yosuke Ota
3+
* See LICENSE file in root directory for full license.
4+
*/
5+
"use strict"
6+
7+
const { READ, ReferenceTracker } = require("@eslint-community/eslint-utils")
8+
const { getSourceCode } = require("eslint-compat-utils")
9+
10+
module.exports = {
11+
meta: {
12+
docs: {
13+
description: "disallow `Promise.try` function",
14+
category: "ES2025",
15+
recommended: false,
16+
url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-promise-try.html",
17+
},
18+
fixable: null,
19+
messages: {
20+
forbidden: "ES2025 '{{name}}' is forbidden.",
21+
},
22+
schema: [],
23+
type: "problem",
24+
},
25+
create(context) {
26+
return {
27+
"Program:exit"(program) {
28+
const sourceCode = getSourceCode(context)
29+
const tracker = new ReferenceTracker(
30+
sourceCode.getScope(program),
31+
)
32+
for (const { node, path } of tracker.iterateGlobalReferences({
33+
Promise: {
34+
try: { [READ]: true },
35+
},
36+
})) {
37+
context.report({
38+
node,
39+
messageId: "forbidden",
40+
data: { name: path.join(".") },
41+
})
42+
}
43+
},
44+
}
45+
},
46+
}

tests/lib/rules/no-promise-try.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @author Yosuke Ota
3+
* See LICENSE file in root directory for full license.
4+
*/
5+
"use strict"
6+
7+
const RuleTester = require("../../tester")
8+
const rule = require("../../../lib/rules/no-promise-try.js")
9+
10+
new RuleTester().run("no-promise-try", rule, {
11+
valid: ["Promise.all"],
12+
invalid: [
13+
{
14+
code: "Promise.try",
15+
errors: ["ES2025 'Promise.try' is forbidden."],
16+
},
17+
],
18+
})

0 commit comments

Comments
 (0)