Repository: ember-cli/eslint-plugin-ember
Pull request #872 (2020-06-28 18:48:48)

Rename `flatten` util to `flat`
To match native JavaScript function name that this polyfills:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat

CC: @mongoose700 


Patch:
diff --git a/lib/rules/no-duplicate-dependent-keys.js b/lib/rules/no-duplicate-dependent-keys.js
index e5881ceb..1797c639 100644
--- a/lib/rules/no-duplicate-dependent-keys.js
+++ b/lib/rules/no-duplicate-dependent-keys.js
@@ -43,7 +43,7 @@ module.exports = {
                 return null;
               }
 
-              return javascriptUtils.flatten(
+              return javascriptUtils.flat(
                 duplicateNodes.map((duplicateNode) =>
                   fixerUtils.removeCommaSeparatedNode(duplicateNode, sourceCode, fixer)
                 )
diff --git a/lib/utils/javascript.js b/lib/utils/javascript.js
index 80dfffd0..1e250e9f 100644
--- a/lib/utils/javascript.js
+++ b/lib/utils/javascript.js
@@ -2,7 +2,7 @@
 
 module.exports = {
   duplicateArrays,
-  flatten,
+  flat,
   flatMap,
   removeWhitespace,
 };
@@ -24,13 +24,14 @@ function duplicateArrays(arr, times) {
 /**
  * Flattens an array.
  * WARNING: can only handle one level of nested arrays.
+ * TODO: switch to native JavaScript `flat` once Node 12 is the minimum version we need to support.
  * Input: [[1], [2]]
  * Output: [1, 2]
  *
  * @param  {Object[]} arr Array to flatten.
  * @return {Object[]}     Flattened array.
  */
-function flatten(arr) {
+function flat(arr) {
   if (!arr || arr.length === 0) {
     return arr;
   }
@@ -40,6 +41,7 @@ function flatten(arr) {
 
 /**
  * Builds an array by concatenating the results of a map.
+ * TODO: switch to native JavaScript `flatMap` once Node 12 is the minimum version we need to support.
  *
  * @template T, U
  * @param {Array<T>} array


Test patch:
diff --git a/tests/lib/rules/no-invalid-debug-function-arguments.js b/tests/lib/rules/no-invalid-debug-function-arguments.js
index e8bcda8c..5716f905 100644
--- a/tests/lib/rules/no-invalid-debug-function-arguments.js
+++ b/tests/lib/rules/no-invalid-debug-function-arguments.js
@@ -21,7 +21,7 @@ const VALID_USAGES_BASIC = [
   },
 ];
 
-const VALID_USAGES_FOR_EACH_DEBUG_FUNCTION = javascriptUtils.flatten(
+const VALID_USAGES_FOR_EACH_DEBUG_FUNCTION = javascriptUtils.flat(
   DEBUG_FUNCTIONS.map((debugFunction) => [
     {
       code: `import { ${debugFunction} } from '@ember/debug'; OtherClass.${debugFunction}(true, 'My string.');`,
@@ -78,7 +78,7 @@ const VALID_USAGES_FOR_EACH_DEBUG_FUNCTION = javascriptUtils.flatten(
 
 const VALID_USAGES = [...VALID_USAGES_BASIC, ...VALID_USAGES_FOR_EACH_DEBUG_FUNCTION];
 
-const INVALID_USAGES = javascriptUtils.flatten(
+const INVALID_USAGES = javascriptUtils.flat(
   DEBUG_FUNCTIONS.map((debugFunction) => [
     {
       code: `import { ${debugFunction} } from '@ember/debug'; ${debugFunction}(true, 'My description.');`,
diff --git a/tests/lib/utils/javascript-test.js b/tests/lib/utils/javascript-test.js
index b46cc92f..758f6f7f 100644
--- a/tests/lib/utils/javascript-test.js
+++ b/tests/lib/utils/javascript-test.js
@@ -17,20 +17,20 @@ describe('duplicateArrays', () => {
   });
 });
 
-describe('flatten', function () {
+describe('flat', function () {
   it('correctly flattens arrays', function () {
-    expect(javascriptUtils.flatten([])).toStrictEqual([]);
-    expect(javascriptUtils.flatten(null)).toBeNull();
-    expect(javascriptUtils.flatten([[1], [2]])).toStrictEqual([1, 2]);
+    expect(javascriptUtils.flat([])).toStrictEqual([]);
+    expect(javascriptUtils.flat(null)).toBeNull();
+    expect(javascriptUtils.flat([[1], [2]])).toStrictEqual([1, 2]);
     expect(
-      javascriptUtils.flatten([
+      javascriptUtils.flat([
         [1, 2],
         [3, 4],
       ])
     ).toStrictEqual([1, 2, 3, 4]);
-    expect(javascriptUtils.flatten([[1], [2, 3], [4, 5, 6]])).toStrictEqual([1, 2, 3, 4, 5, 6]); // different lengths forward
-    expect(javascriptUtils.flatten([[6, 5, 4], [3, 2], [1]])).toStrictEqual([6, 5, 4, 3, 2, 1]); // different lengths reverse
-    expect(javascriptUtils.flatten([[1], null, [2]])).toStrictEqual([1, null, 2]); // with null array
+    expect(javascriptUtils.flat([[1], [2, 3], [4, 5, 6]])).toStrictEqual([1, 2, 3, 4, 5, 6]); // different lengths forward
+    expect(javascriptUtils.flat([[6, 5, 4], [3, 2], [1]])).toStrictEqual([6, 5, 4, 3, 2, 1]); // different lengths reverse
+    expect(javascriptUtils.flat([[1], null, [2]])).toStrictEqual([1, null, 2]); // with null array
   });
 });
 
diff --git a/tests/rule-setup.js b/tests/rule-setup.js
index 145f99d8..91ebd6fa 100644
--- a/tests/rule-setup.js
+++ b/tests/rule-setup.js
@@ -5,7 +5,7 @@ const { join } = require('path');
 const rules = require('../lib').rules;
 const recommendedRules = require('../lib/recommended-rules.js');
 const octaneRules = require('../lib/octane-rules.js');
-const { flatten } = require('../lib/utils/javascript');
+const { flat } = require('../lib/utils/javascript');
 
 const RULE_NAMES = Object.keys(rules);
 const RECOMMENDED_RULE_NAMES = Object.keys(recommendedRules).map((name) =>
@@ -19,7 +19,7 @@ function getAllNamedOptions(jsonSchema) {
   }
 
   if (Array.isArray(jsonSchema)) {
-    return flatten(jsonSchema.map(getAllNamedOptions));
+    return flat(jsonSchema.map(getAllNamedOptions));
   }
 
   if (jsonSchema.items) {