diff --git a/docs/rules/index.md b/docs/rules/index.md index a087ad9c..d018ee73 100644 --- a/docs/rules/index.md +++ b/docs/rules/index.md @@ -4,6 +4,14 @@ This plugin provides the following rules. - 🔧 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. +## ES2026 + +There is a config that enables the rules in this category: [`no-new-in-esnext`] + +| Rule ID | Description | | +|:--------|:------------|:--:| +| [es-x/no-array-fromasync](./no-array-fromasync.md) | disallow the `Array.fromAsync` method. | | + ## ES2025 There is a config that enables the rules in this category: [`no-new-in-esnext`] diff --git a/docs/rules/no-array-fromasync.md b/docs/rules/no-array-fromasync.md new file mode 100644 index 00000000..a502f841 --- /dev/null +++ b/docs/rules/no-array-fromasync.md @@ -0,0 +1,54 @@ +--- +title: "es-x/no-array-fromasync" +description: "disallow the `Array.fromAsync` method" +--- + +# es-x/no-array-fromasync +> disallow the `Array.fromAsync` method + +- ❗ ***This rule has not been released yet.*** +- ✅ The following configurations enable this rule: [no-new-in-esnext] + +This rule reports ES2026 [`Array.fromAsync` method](https://github.com/tc39/proposal-array-from-async) as errors. + +## 💡 Examples + +⛔ Examples of **incorrect** code for this rule: + + + +```js +/*eslint es-x/no-array-fromasync: error */ +const arr = await Array.fromAsync(genPromises(4)); +``` + + + +## 🔧 Options + +This rule has an option. + +```jsonc +{ + "rules": { + "es-x/no-array-fromasync": [ + "error", + { + "allowTestedProperty": false + } + ] + } +} +``` + +### allowTestedProperty: boolean + +Configure the allowTestedProperty mode for only this rule. +This is prior to the `settings['es-x'].allowTestedProperty` setting. + +## 📚 References + +- [Rule source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/lib/rules/no-array-fromasync.js) +- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-array-fromasync.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 e5cccf75..6f125c80 100644 --- a/lib/configs/flat/no-new-in-esnext.js +++ b/lib/configs/flat/no-new-in-esnext.js @@ -11,6 +11,7 @@ module.exports = { }, }, rules: { + "es-x/no-array-fromasync": "error", "es-x/no-dataview-prototype-getfloat16-setfloat16": "error", "es-x/no-dynamic-import-options": "error", "es-x/no-float16array": "error", diff --git a/lib/configs/no-new-in-esnext.js b/lib/configs/no-new-in-esnext.js index 9dd343d4..23a762d3 100644 --- a/lib/configs/no-new-in-esnext.js +++ b/lib/configs/no-new-in-esnext.js @@ -7,6 +7,7 @@ module.exports = { plugins: ["es-x"], rules: { + "es-x/no-array-fromasync": "error", "es-x/no-dataview-prototype-getfloat16-setfloat16": "error", "es-x/no-dynamic-import-options": "error", "es-x/no-float16array": "error", diff --git a/lib/index.js b/lib/index.js index 03cc4988..40a40538 100644 --- a/lib/index.js +++ b/lib/index.js @@ -147,6 +147,7 @@ module.exports = { "no-accessor-properties": require("./rules/no-accessor-properties"), "no-arbitrary-module-namespace-names": require("./rules/no-arbitrary-module-namespace-names"), "no-array-from": require("./rules/no-array-from"), + "no-array-fromasync": require("./rules/no-array-fromasync"), "no-array-isarray": require("./rules/no-array-isarray"), "no-array-of": require("./rules/no-array-of"), "no-array-prototype-at": require("./rules/no-array-prototype-at"), diff --git a/lib/rules/no-array-fromasync.js b/lib/rules/no-array-fromasync.js new file mode 100644 index 00000000..97d23e40 --- /dev/null +++ b/lib/rules/no-array-fromasync.js @@ -0,0 +1,35 @@ +"use strict" + +const { + defineStaticPropertiesHandler, +} = require("../util/define-static-properties-handler") + +module.exports = { + meta: { + docs: { + description: "disallow the `Array.fromAsync` method.", + category: "ES2026", + recommended: false, + url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-array-fromasync.html", + }, + fixable: null, + messages: { + forbidden: "ES2026 '{{name}}' method is forbidden.", + }, + schema: [ + { + type: "object", + properties: { + allowTestedProperty: { type: "boolean" }, + }, + additionalProperties: false, + }, + ], + type: "problem", + }, + create(context) { + return defineStaticPropertiesHandler(context, { + Array: { fromAsync: "function" }, + }) + }, +} diff --git a/lib/util/well-known-properties.js b/lib/util/well-known-properties.js index 0f5a3426..247bf2d5 100644 --- a/lib/util/well-known-properties.js +++ b/lib/util/well-known-properties.js @@ -459,6 +459,7 @@ const arrayProperties = new Set([ // https://tc39.es/ecma262/multipage/indexed-collections.html#sec-properties-of-the-array-constructor "from", + "fromAsync", "isArray", "of", "prototype", diff --git a/tests/lib/rules/no-array-fromasync.js b/tests/lib/rules/no-array-fromasync.js new file mode 100644 index 00000000..13483460 --- /dev/null +++ b/tests/lib/rules/no-array-fromasync.js @@ -0,0 +1,47 @@ +"use strict" + +const RuleTester = require("../../tester") +const rule = require("../../../lib/rules/no-array-fromasync") + +new RuleTester().run("no-array-fromasync", rule, { + valid: [ + "Array", + "Array.from", + "let Array = 0; Array.fromAsync", + { + code: "if (Array.fromAsync) { Array.fromAsync }", + options: [{ allowTestedProperty: true }], + }, + { + code: "if (Array.fromAsync) { Array.fromAsync }", + settings: { "es-x": { allowTestedProperty: true } }, + }, + { + code: "if (Array.fromAsync) { const {fromAsync} = Array }", + options: [{ allowTestedProperty: true }], + }, + ], + invalid: [ + { + code: "Array.fromAsync", + errors: ["ES2026 'Array.fromAsync' method is forbidden."], + }, + { + code: "const {fromAsync} = Array", + errors: ["ES2026 'Array.fromAsync' method is forbidden."], + }, + { + code: "const {a:{fromAsync} = Array} = {}", + errors: ["ES2026 'Array.fromAsync' method is forbidden."], + }, + { + code: "if (Array.fromAsync) { Array.fromAsync }", + errors: 2, + }, + { + code: "if (Array.fromAsync) { }", + options: [{ allowTestedProperty: true }], + errors: ["ES2026 'Array.fromAsync' method is forbidden."], + }, + ], +})