From 15a5850138bffb64c1e2134ac9f2eacd2c04d219 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=AF=E7=84=B6?= Date: Wed, 20 Dec 2023 16:51:02 +0800 Subject: [PATCH] feat: Add builtins check for ES2021 to no-unsupported-features/es-builtins rule (#153) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit the change was borrowed from https://github.com/mysticatea/eslint-plugin-node/pull/284 credits goes to @ota-meshi. :) Signed-off-by: 唯然 --- .../no-unsupported-features/es-builtins.md | 7 + .../no-unsupported-features/es-builtins.js | 10 ++ .../no-unsupported-features/es-builtins.js | 136 ++++++++++++++++++ 3 files changed, 153 insertions(+) diff --git a/docs/rules/no-unsupported-features/es-builtins.md b/docs/rules/no-unsupported-features/es-builtins.md index a9647a4a..2434ab29 100644 --- a/docs/rules/no-unsupported-features/es-builtins.md +++ b/docs/rules/no-unsupported-features/es-builtins.md @@ -48,6 +48,13 @@ The `"ignores"` option accepts an array of the following strings.
+**ES2021:** + +- `"AggregateError"` +- `"Promise.any"` +- `"WeakRef"` +- `"FinalizationRegistry"` + **ES2020:** - `"BigInt"` diff --git a/lib/rules/no-unsupported-features/es-builtins.js b/lib/rules/no-unsupported-features/es-builtins.js index 5499e6c9..f0215e35 100644 --- a/lib/rules/no-unsupported-features/es-builtins.js +++ b/lib/rules/no-unsupported-features/es-builtins.js @@ -14,6 +14,9 @@ const getConfiguredNodeVersion = require("../../util/get-configured-node-version const trackMap = { globals: { + AggregateError: { + [READ]: { supported: "15.0.0" }, + }, Array: { from: { [READ]: { supported: "4.0.0" } }, of: { [READ]: { supported: "4.0.0" } }, @@ -21,6 +24,9 @@ const trackMap = { BigInt: { [READ]: { supported: "10.4.0" }, }, + FinalizationRegistry: { + [READ]: { supported: "14.6.0" }, + }, Map: { [READ]: { supported: "0.12.0" }, }, @@ -67,6 +73,7 @@ const trackMap = { Promise: { [READ]: { supported: "0.12.0" }, allSettled: { [READ]: { supported: "12.9.0" } }, + any: { [READ]: { supported: "15.0.0" } }, }, Proxy: { [READ]: { supported: "6.0.0" }, @@ -123,6 +130,9 @@ const trackMap = { WeakMap: { [READ]: { supported: "0.12.0" }, }, + WeakRef: { + [READ]: { supported: "14.6.0" }, + }, WeakSet: { [READ]: { supported: "0.12.0" }, }, diff --git a/tests/lib/rules/no-unsupported-features/es-builtins.js b/tests/lib/rules/no-unsupported-features/es-builtins.js index 6956ef4b..424b88bf 100644 --- a/tests/lib/rules/no-unsupported-features/es-builtins.js +++ b/tests/lib/rules/no-unsupported-features/es-builtins.js @@ -72,6 +72,31 @@ ruleTester.run( "no-unsupported-features/es-builtins", rule, concat([ + { + keyword: "AggregateError", + valid: [ + { + code: "if (error instanceof AggregateError) {}", + options: [{ version: "15.0.0" }], + }, + ], + invalid: [ + { + code: "if (error instanceof AggregateError) {}", + options: [{ version: "14.0.0" }], + errors: [ + { + messageId: "unsupported", + data: { + name: "AggregateError", + supported: "15.0.0", + version: "14.0.0", + }, + }, + ], + }, + ], + }, { keyword: "Array.from", valid: [ @@ -185,6 +210,31 @@ ruleTester.run( }, ], }, + { + keyword: "FinalizationRegistry", + valid: [ + { + code: "new FinalizationRegistry(() => {})", + options: [{ version: "14.6.0" }], + }, + ], + invalid: [ + { + code: "new FinalizationRegistry(() => {})", + options: [{ version: "14.5.0" }], + errors: [ + { + messageId: "unsupported", + data: { + name: "FinalizationRegistry", + supported: "14.6.0", + version: "14.5.0", + }, + }, + ], + }, + ], + }, { keyword: "Map", valid: [ @@ -1209,6 +1259,49 @@ ruleTester.run( }, ], }, + { + keyword: "Promise.any", + valid: [ + { + code: "(function(Promise) { Promise.any }(a))", + options: [{ version: "14.0.0" }], + }, + { + code: "Promise.any", + options: [{ version: "15.0.0" }], + }, + ], + invalid: [ + { + code: "Promise.any", + options: [{ version: "14.0.0" }], + errors: [ + { + messageId: "unsupported", + data: { + name: "Promise.any", + supported: "15.0.0", + version: "14.0.0", + }, + }, + ], + }, + { + code: "function wrap() { Promise.any }", + options: [{ version: "14.0.0" }], + errors: [ + { + messageId: "unsupported", + data: { + name: "Promise.any", + supported: "15.0.0", + version: "14.0.0", + }, + }, + ], + }, + ], + }, { keyword: "Proxy", valid: [ @@ -2006,6 +2099,49 @@ ruleTester.run( }, ], }, + { + keyword: "WeakRef", + valid: [ + { + code: "(function(WeakRef) { WeakRef }(a))", + options: [{ version: "14.5.0" }], + }, + { + code: "WeakRef", + options: [{ version: "14.6.0" }], + }, + ], + invalid: [ + { + code: "WeakRef", + options: [{ version: "14.5.0" }], + errors: [ + { + messageId: "unsupported", + data: { + name: "WeakRef", + supported: "14.6.0", + version: "14.5.0", + }, + }, + ], + }, + { + code: "function wrap() { WeakRef }", + options: [{ version: "14.5.0" }], + errors: [ + { + messageId: "unsupported", + data: { + name: "WeakRef", + supported: "14.6.0", + version: "14.5.0", + }, + }, + ], + }, + ], + }, { keyword: "WeakSet", valid: [