Repository: ember-cli/eslint-plugin-ember (js)

Issue:
Tweak autofix for `sortBy` in `no-array-prototype-extensions` rule
Follow-up to https://github.com/ember-cli/eslint-plugin-ember/pull/1621/files.

before:

```js
"something.sortBy('abc')",
```

after (current):

```js
import { get } from '@ember/object';
import { compare } from '@ember/utils';

something.sort((a, b) => {
  for (const key of ['abc']) {
    const compareValue = compare(get(a, key), get(b, key));
    if (compareValue) {
      return compareValue;
    }
  }
  return 0;
});
```

after (desired):

```js
import { get } from '@ember/object';
import { compare } from '@ember/utils';

something.sort((a, b) => {
  const key = 'abc';
  const compareValue = compare(get(a, key), get(b, key));
  if (compareValue) {
    return compareValue;
  }
  return 0;
});

```

We can avoid the for-of loop if there is only one sort key.

CC: @tgvrssanthosh

Pull request:
Simpler autofix for `sortBy` with single arg for `no-array-prototype-extension` rule
Fixes #1634.

CC: @tgvrssanthosh

Patch:
diff --git a/lib/rules/no-array-prototype-extensions.js b/lib/rules/no-array-prototype-extensions.js
index 01bdff79..0d443062 100644
--- a/lib/rules/no-array-prototype-extensions.js
+++ b/lib/rules/no-array-prototype-extensions.js
@@ -423,12 +423,23 @@ function applyFix(callExpressionNode, fixer, context, options = {}) {
         // default to `compare` if the `compare` hasn't already been imported.
         const importedGetName = options.importedGetName ?? 'get';
 
-        const sortFn = `(a, b) => {
-          for (const key of [${callArgs.map((arg) => sourceCode.getText(arg)).join(', ')}]) {
-            const compareValue = ${importedCompareName}(${importedGetName}(a, key), ${importedGetName}(b, key));
+        const compareValueCheckText = `const compareValue = ${importedCompareName}(${importedGetName}(a, key), ${importedGetName}(b, key));
             if (compareValue) {
               return compareValue;
-            }
+            }`;
+        const argsText = callArgs.map((arg) => sourceCode.getText(arg)).join(', ');
+
+        // Loop through keys if more than one of them.
+        const sortFn =
+          callArgs.length === 1
+            ? `(a, b) => {
+            const key = ${argsText};
+            ${compareValueCheckText}
+            return 0;
+          }`
+            : `(a, b) => {
+          for (const key of [${argsText}]) {
+            ${compareValueCheckText}
           }
           return 0;
         }`;


Test patch:
diff --git a/tests/lib/rules/no-array-prototype-extensions.js b/tests/lib/rules/no-array-prototype-extensions.js
index c9eb648a..00f700f3 100644
--- a/tests/lib/rules/no-array-prototype-extensions.js
+++ b/tests/lib/rules/no-array-prototype-extensions.js
@@ -799,6 +799,21 @@ something.sort((a, b) => {
         })`,
       errors: [{ messageId: 'main', type: 'CallExpression' }],
     },
+    {
+      // Single argument.
+      code: 'something.sortBy(getKey())',
+      output: `import { get } from '@ember/object';
+import { compare } from '@ember/utils';
+something.sort((a, b) => {
+            const key = getKey();
+            const compareValue = compare(get(a, key), get(b, key));
+            if (compareValue) {
+              return compareValue;
+            }
+            return 0;
+          })`,
+      errors: [{ messageId: 'main', type: 'CallExpression' }],
+    },
     {
       // Arguments other than strings
       code: "something.sortBy('abc', def)",