Repository: eslint-community/eslint-plugin-es-x
Pull request #106 (2023-11-30 13:56:31)

Add `es-x/no-object-map-groupby` rule
close #104


Patch:
diff --git a/docs/rules/index.md b/docs/rules/index.md
index 1fef76a..b0e74e6 100644
--- a/docs/rules/index.md
+++ b/docs/rules/index.md
@@ -11,6 +11,7 @@ There is a config that enables the rules in this category: `plugin:es-x/no-new-i
 | Rule ID | Description |    |
 |:--------|:------------|:--:|
 | [es-x/no-atomics-waitasync](./no-atomics-waitasync.md) | disallow the `Atomics.waitAsync` method. |  |
+| [es-x/no-object-map-groupby](./no-object-map-groupby.md) | disallow the `{Object,Map}.groupBy()` method (array grouping). |  |
 | [es-x/no-regexp-v-flag](./no-regexp-v-flag.md) | disallow RegExp `v` flag. |  |
 | [es-x/no-resizable-and-growable-arraybuffers](./no-resizable-and-growable-arraybuffers.md) | disallow resizable and growable ArrayBuffers. |  |
 | [es-x/no-string-prototype-iswellformed-towellformed](./no-string-prototype-iswellformed-towellformed.md) | disallow the `String.prototype.{isWellFormed,toWellFormed}` methods. |  |
diff --git a/docs/rules/no-object-map-groupby.md b/docs/rules/no-object-map-groupby.md
new file mode 100644
index 0000000..a1b10bc
--- /dev/null
+++ b/docs/rules/no-object-map-groupby.md
@@ -0,0 +1,36 @@
+---
+title: "es-x/no-object-map-groupby"
+description: "disallow the `{Object,Map}.groupBy()` method (array grouping)"
+---
+
+# es-x/no-object-map-groupby
+> disallow the `{Object,Map}.groupBy()` method (array grouping)
+
+- ❗ <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: `plugin:es-x/no-new-in-esnext`
+
+This rule reports ES2024 `Object.groupBy()` method and `Map.groupBy()` method (Array grouping) as errors.
+
+## 💡 Examples
+
+⛔ Examples of **incorrect** code for this rule:
+
+<eslint-playground type="bad">
+
+```js
+/*eslint es-x/no-object-map-groupby: error */
+Object.groupBy(array, (num, index) => {
+  return num % 2 === 0 ? 'even': 'odd';
+});
+
+Map.groupBy(array, (num, index) => {
+  return num % 2 === 0 ? even: odd;
+});
+```
+
+</eslint-playground>
+
+## 📚 References
+
+- [Rule source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/lib/rules/no-object-map-groupby.js)
+- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-object-map-groupby.js)
diff --git a/lib/configs/no-new-in-esnext.js b/lib/configs/no-new-in-esnext.js
index 41d2ee1..9e34257 100644
--- a/lib/configs/no-new-in-esnext.js
+++ b/lib/configs/no-new-in-esnext.js
@@ -8,6 +8,7 @@ module.exports = {
     plugins: ["es-x"],
     rules: {
         "es-x/no-atomics-waitasync": "error",
+        "es-x/no-object-map-groupby": "error",
         "es-x/no-regexp-v-flag": "error",
         "es-x/no-resizable-and-growable-arraybuffers": "error",
         "es-x/no-string-prototype-iswellformed-towellformed": "error",
diff --git a/lib/index.js b/lib/index.js
index f487755..e81dd3d 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -204,6 +204,7 @@ module.exports = {
         "no-object-isfrozen": require("./rules/no-object-isfrozen"),
         "no-object-issealed": require("./rules/no-object-issealed"),
         "no-object-keys": require("./rules/no-object-keys"),
+        "no-object-map-groupby": require("./rules/no-object-map-groupby"),
         "no-object-preventextensions": require("./rules/no-object-preventextensions"),
         "no-object-seal": require("./rules/no-object-seal"),
         "no-object-setprototypeof": require("./rules/no-object-setprototypeof"),
diff --git a/lib/rules/no-object-map-groupby.js b/lib/rules/no-object-map-groupby.js
new file mode 100644
index 0000000..b91f963
--- /dev/null
+++ b/lib/rules/no-object-map-groupby.js
@@ -0,0 +1,46 @@
+"use strict"
+
+const { ReferenceTracker, READ } = require("@eslint-community/eslint-utils")
+const { getSourceCode } = require("eslint-compat-utils")
+
+module.exports = {
+    meta: {
+        docs: {
+            description:
+                "disallow the `{Object,Map}.groupBy()` method (array grouping).",
+            category: "ES2024",
+            recommended: false,
+            url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-object-map-groupby.html",
+        },
+        fixable: null,
+        messages: {
+            forbidden: "ES2024 '{{name}}' method 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({
+                    Object: {
+                        groupBy: { [READ]: true },
+                    },
+                    Map: {
+                        groupBy: { [READ]: true },
+                    },
+                })) {
+                    context.report({
+                        node,
+                        messageId: "forbidden",
+                        data: { name: path.join(".") },
+                    })
+                }
+            },
+        }
+    },
+}


Test patch:
diff --git a/tests/lib/rules/no-object-map-groupby.js b/tests/lib/rules/no-object-map-groupby.js
new file mode 100644
index 0000000..e9d2377
--- /dev/null
+++ b/tests/lib/rules/no-object-map-groupby.js
@@ -0,0 +1,24 @@
+"use strict"
+
+const RuleTester = require("../../tester")
+const rule = require("../../../lib/rules/no-object-map-groupby.js")
+
+new RuleTester().run("no-object-map-groupby", rule, {
+    valid: [
+        "Object",
+        "Map",
+        "Object.assign",
+        "let Object = 0; Object.groupBy",
+        "let Map = 0; Map.groupBy",
+    ],
+    invalid: [
+        {
+            code: "Object.groupBy",
+            errors: ["ES2024 'Object.groupBy' method is forbidden."],
+        },
+        {
+            code: "Map.groupBy",
+            errors: ["ES2024 'Map.groupBy' method is forbidden."],
+        },
+    ],
+})