Skip to content

Commit c3b9ab0

Browse files
committed
Add no-array-fromasync rule
1 parent f6aa5e0 commit c3b9ab0

File tree

9 files changed

+128
-2
lines changed

9 files changed

+128
-2
lines changed

docs/rules/index.md

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ This plugin provides the following rules.
44

55
- 🔧 mark means that the `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by the rule.
66

7+
## ES2026
8+
9+
There is a config that enables the rules in this category: [`no-new-in-esnext`]
10+
11+
| Rule ID | Description | |
12+
|:--------|:------------|:--:|
13+
| [es-x/no-array-fromasync](./no-array-fromasync.md) | disallow the `Array.fromAsync` method. | |
14+
715
## ES2025
816

917
There is a config that enables the rules in this category: [`no-new-in-esnext`]

docs/rules/no-array-fromasync.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: "es-x/no-array-fromasync"
3+
description: "disallow the `Array.fromAsync` method"
4+
---
5+
6+
# es-x/no-array-fromasync
7+
> disallow the `Array.fromAsync` method
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 ES2026 [`Array.fromAsync` method](https://github.com/tc39/proposal-array-from-async) 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-array-fromasync: error */
22+
const arr = await Array.fromAsync(genPromises(4));
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-array-fromasync.js)
30+
- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-array-fromasync.js)
31+
32+
[no-new-in-esnext]: ../configs/index.md#no-new-in-esnext

jsconfig.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"compilerOptions": {
33
"target": "ESNext",
4-
"moduleResolution": "NodeNext",
5-
"module": "NodeNext",
4+
"moduleResolution": "node",
5+
"module": "CommonJS",
66
"strict": true,
77
"strictNullChecks": true
88
},

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

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = {
1111
},
1212
},
1313
rules: {
14+
"es-x/no-array-fromasync": "error",
1415
"es-x/no-dataview-prototype-getfloat16-setfloat16": "error",
1516
"es-x/no-dynamic-import-options": "error",
1617
"es-x/no-float16array": "error",

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

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
module.exports = {
88
plugins: ["es-x"],
99
rules: {
10+
"es-x/no-array-fromasync": "error",
1011
"es-x/no-dataview-prototype-getfloat16-setfloat16": "error",
1112
"es-x/no-dynamic-import-options": "error",
1213
"es-x/no-float16array": "error",

lib/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ module.exports = {
147147
"no-accessor-properties": require("./rules/no-accessor-properties"),
148148
"no-arbitrary-module-namespace-names": require("./rules/no-arbitrary-module-namespace-names"),
149149
"no-array-from": require("./rules/no-array-from"),
150+
"no-array-fromasync": require("./rules/no-array-fromasync"),
150151
"no-array-isarray": require("./rules/no-array-isarray"),
151152
"no-array-of": require("./rules/no-array-of"),
152153
"no-array-prototype-at": require("./rules/no-array-prototype-at"),

lib/rules/no-array-fromasync.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"use strict"
2+
3+
const {
4+
defineStaticPropertiesHandler,
5+
} = require("../util/define-static-properties-handler")
6+
7+
module.exports = {
8+
meta: {
9+
docs: {
10+
description: "disallow the `Array.fromAsync` method.",
11+
category: "ES2026",
12+
recommended: false,
13+
url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-array-fromasync.html",
14+
},
15+
fixable: null,
16+
messages: {
17+
forbidden: "ES2026 '{{name}}' method is forbidden.",
18+
},
19+
schema: [
20+
{
21+
type: "object",
22+
properties: {
23+
allowTestedProperty: { type: "boolean" },
24+
},
25+
additionalProperties: false,
26+
},
27+
],
28+
type: "problem",
29+
},
30+
create(context) {
31+
return defineStaticPropertiesHandler(context, {
32+
Array: { fromAsync: "function" },
33+
})
34+
},
35+
}

lib/util/well-known-properties.js

+1
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ const arrayProperties = new Set([
459459

460460
// https://tc39.es/ecma262/multipage/indexed-collections.html#sec-properties-of-the-array-constructor
461461
"from",
462+
"fromAsync",
462463
"isArray",
463464
"of",
464465
"prototype",

tests/lib/rules/no-array-fromasync.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"use strict"
2+
3+
const RuleTester = require("../../tester")
4+
const rule = require("../../../lib/rules/no-array-fromasync")
5+
6+
new RuleTester().run("no-array-fromasync", rule, {
7+
valid: [
8+
"Array",
9+
"Array.from",
10+
"let Array = 0; Array.fromAsync",
11+
{
12+
code: "if (Array.fromAsync) { Array.fromAsync }",
13+
options: [{ allowTestedProperty: true }],
14+
},
15+
{
16+
code: "if (Array.fromAsync) { Array.fromAsync }",
17+
settings: { "es-x": { allowTestedProperty: true } },
18+
},
19+
{
20+
code: "if (Array.fromAsync) { const {fromAsync} = Array }",
21+
options: [{ allowTestedProperty: true }],
22+
},
23+
],
24+
invalid: [
25+
{
26+
code: "Array.fromAsync",
27+
errors: ["ES2026 'Array.fromAsync' method is forbidden."],
28+
},
29+
{
30+
code: "const {fromAsync} = Array",
31+
errors: ["ES2026 'Array.fromAsync' method is forbidden."],
32+
},
33+
{
34+
code: "const {a:{fromAsync} = Array} = {}",
35+
errors: ["ES2026 'Array.fromAsync' method is forbidden."],
36+
},
37+
{
38+
code: "if (Array.fromAsync) { Array.fromAsync }",
39+
errors: 2,
40+
},
41+
{
42+
code: "if (Array.fromAsync) { }",
43+
options: [{ allowTestedProperty: true }],
44+
errors: ["ES2026 'Array.fromAsync' method is forbidden."],
45+
},
46+
],
47+
})

0 commit comments

Comments
 (0)