From ceffff389336db34cf293700159cba0ddad360df Mon Sep 17 00:00:00 2001 From: Stephen Wade Date: Sat, 22 May 2021 16:04:40 -0400 Subject: [PATCH] Chore: Replace old syntax for Array flat/flatMap --- lib/linter/apply-disable-directives.js | 16 +--------------- lib/linter/linter.js | 3 +-- lib/linter/node-event-generator.js | 4 +--- lib/rules/max-lines.js | 16 +--------------- 4 files changed, 4 insertions(+), 35 deletions(-) diff --git a/lib/linter/apply-disable-directives.js b/lib/linter/apply-disable-directives.js index 0ba69ca9cc44..ad250a9e52c4 100644 --- a/lib/linter/apply-disable-directives.js +++ b/lib/linter/apply-disable-directives.js @@ -122,21 +122,7 @@ module.exports = ({ directives, problems, reportUnusedDisableDirectives = "off" .map(directive => Object.assign({}, directive, { unprocessedDirective: directive })) .sort(compareLocations); - /** - * Returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level. - * TODO(stephenwade): Replace this with array.flatMap when we drop support for Node v10 - * @param {any[]} array The array to process - * @param {Function} fn The function to use - * @returns {any[]} The result array - */ - function flatMap(array, fn) { - const mapped = array.map(fn); - const flattened = [].concat(...mapped); - - return flattened; - } - - const lineDirectives = flatMap(directives, directive => { + const lineDirectives = directives.flatMap(directive => { switch (directive.type) { case "disable": case "enable": diff --git a/lib/linter/linter.js b/lib/linter/linter.js index bdc6c1b1d016..86829787197b 100644 --- a/lib/linter/linter.js +++ b/lib/linter/linter.js @@ -1287,8 +1287,7 @@ class Linter { const text = ensureText(textOrSourceCode); const preprocess = options.preprocess || (rawText => [rawText]); - // TODO(stephenwade): Replace this with array.flat() when we drop support for Node v10 - const postprocess = options.postprocess || (array => [].concat(...array)); + const postprocess = options.postprocess || Array.prototype.flat.call; const filterCodeBlock = options.filterCodeBlock || (blockFilename => blockFilename.endsWith(".js")); diff --git a/lib/linter/node-event-generator.js b/lib/linter/node-event-generator.js index 8b619fdff83e..9b058247b3e6 100644 --- a/lib/linter/node-event-generator.js +++ b/lib/linter/node-event-generator.js @@ -37,9 +37,7 @@ const esquery = require("esquery"); * @returns {any[]} The union of the input arrays */ function union(...arrays) { - - // TODO(stephenwade): Replace this with arrays.flat() when we drop support for Node v10 - return [...new Set([].concat(...arrays))]; + return [...new Set(arrays.flat())]; } /** diff --git a/lib/rules/max-lines.js b/lib/rules/max-lines.js index 8bd5a1c95f40..0837dd1f0fde 100644 --- a/lib/rules/max-lines.js +++ b/lib/rules/max-lines.js @@ -137,20 +137,6 @@ module.exports = { return []; } - /** - * Returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level. - * TODO(stephenwade): Replace this with array.flatMap when we drop support for Node v10 - * @param {any[]} array The array to process - * @param {Function} fn The function to use - * @returns {any[]} The result array - */ - function flatMap(array, fn) { - const mapped = array.map(fn); - const flattened = [].concat(...mapped); - - return flattened; - } - return { "Program:exit"() { let lines = sourceCode.lines.map((text, i) => ({ @@ -173,7 +159,7 @@ module.exports = { if (skipComments) { const comments = sourceCode.getAllComments(); - const commentLines = flatMap(comments, comment => getLinesWithoutCode(comment)); + const commentLines = comments.flatMap(getLinesWithoutCode); lines = lines.filter( l => !commentLines.includes(l.lineNumber)