Repository: ember-cli/eslint-plugin-ember
Pull request #1632 (2022-10-14 11:38:47)

Add `getEach` to `map` autofixer for `no-array-prototype-extensions` rule
## Summary
Added auto-fixer for `no-array-prototype-extensions` which replaces `getEach` with `map`.

## Description
Recently, the proposal to deprecate array prototype extensions has been approved and got merged. The plan is to add autoFixers to replace ember array prototype extensions with the native array methods. In this PR, an auto fixer has been added which will replace the ember array method `getEach` with the native array method `map`.

## Testing
Modified test case to check if the right output is generated after the fix.

bmish: @tgvrssanthosh can you fix the merge conflict?
bmish: @tgvrssanthosh amazing work so far, a few questions:
1. It looks like we just have `firstObject` and `lastObject` left (https://github.com/ember-cli/eslint-plugin-ember/issues/1556) when you plan to get to those? I could do a release once everything is in ideally.
2. Can you try testing the rule autofixer on a real codebase to make sure we don't have obvious bugs before a release?
bmish: I realized that I missed a lot of methods from MutableArray in my list in https://github.com/ember-cli/eslint-plugin-ember/issues/1556 so I have added those to the list.

Also started testing the autofixers on my codebase and opened #1634 for a tweak.
tgvrssanthosh: @bmish Resolved merge conflicts. Can you please take a look?

Patch:
diff --git a/lib/rules/no-array-prototype-extensions.js b/lib/rules/no-array-prototype-extensions.js
index 0d443062..1a82df06 100644
--- a/lib/rules/no-array-prototype-extensions.js
+++ b/lib/rules/no-array-prototype-extensions.js
@@ -286,7 +286,8 @@ function applyFix(callExpressionNode, fixer, context, options = {}) {
       }
       return fixes;
     }
-    case 'mapBy': {
+    case 'mapBy':
+    case 'getEach': {
       if (callArgs.length === 1) {
         const argText = sourceCode.getText(callArgs[0]);
         const fixes = [];


Test patch:
diff --git a/tests/lib/rules/no-array-prototype-extensions.js b/tests/lib/rules/no-array-prototype-extensions.js
index 00f700f3..b3fef633 100644
--- a/tests/lib/rules/no-array-prototype-extensions.js
+++ b/tests/lib/rules/no-array-prototype-extensions.js
@@ -461,10 +461,54 @@ import { get as g } from 'dummy';
       errors: [{ messageId: 'main', type: 'CallExpression' }],
     },
     {
+      // When unexpected number of arguments are passed, auto-fixer will not run
       code: 'something.getEach()',
       output: null,
       errors: [{ messageId: 'main', type: 'CallExpression' }],
     },
+    {
+      // When unexpected number of arguments are passed, auto-fixer will not run
+      code: 'something.getEach(1, 2)',
+      output: null,
+      errors: [{ messageId: 'main', type: 'CallExpression' }],
+    },
+    {
+      code: "something.getEach('abc')",
+      output: `import { get } from '@ember/object';
+something.map(item => get(item, 'abc'))`,
+      errors: [{ messageId: 'main', type: 'CallExpression' }],
+    },
+    {
+      code: 'something.getEach(def)',
+      output: `import { get } from '@ember/object';
+something.map(item => get(item, def))`,
+      errors: [{ messageId: 'main', type: 'CallExpression' }],
+    },
+    {
+      // When `get` method is already imported from `@ember/object` package
+      code: `import { get } from '@ember/object';
+      something.getEach('abc')`,
+      output: `import { get } from '@ember/object';
+      something.map(item => get(item, 'abc'))`,
+      errors: [{ messageId: 'main', type: 'CallExpression' }],
+    },
+    {
+      // When `get` method is already imported as alias from `@ember/object` package
+      code: `import { get as g } from '@ember/object';
+      something.getEach('abc')`,
+      output: `import { get as g } from '@ember/object';
+      something.map(item => g(item, 'abc'))`,
+      errors: [{ messageId: 'main', type: 'CallExpression' }],
+    },
+    {
+      // When `get` method is already imported from a other than `@ember/object`
+      code: `import { get as g } from 'dummy';
+      something.getEach('abc')`,
+      output: `import { get } from '@ember/object';
+import { get as g } from 'dummy';
+      something.map(item => get(item, 'abc'))`,
+      errors: [{ messageId: 'main', type: 'CallExpression' }],
+    },
     {
       // When unexpected number of arguments are passed, auto-fixer will not run
       code: 'something.invoke()',