From 3a5ad34ca69a5c3239fff56241eb7e353d87274c Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 3 Jul 2023 22:48:40 -0500 Subject: [PATCH] [Refactor] `no-unused-modules`: use `array.prototype.flatmap` --- CHANGELOG.md | 1 + src/rules/no-unused-modules.js | 24 ++++++++++-------------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fdc41210b..68d715678 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange - [Docs] [`extensions`]: reference node ESM behavior ([#2748], thanks [@xM8WVqaG]) - [Refactor] [`exports-last`]: use `array.prototype.findlastindex` (thanks [@ljharb]) - [Refactor] [`no-anonymous-default-export`]: use `object.fromentries` (thanks [@ljharb]) +- [Refactor] [`no-unused-modules`]: use `array.prototype.flatmap` (thanks [@ljharb]) ## [2.27.5] - 2023-01-16 diff --git a/src/rules/no-unused-modules.js b/src/rules/no-unused-modules.js index 4b09128a1..a2b6f4ea2 100644 --- a/src/rules/no-unused-modules.js +++ b/src/rules/no-unused-modules.js @@ -4,15 +4,17 @@ * @author René Fermann */ -import Exports, { recursivePatternCapture } from '../ExportMap'; import { getFileExtensions } from 'eslint-module-utils/ignore'; import resolve from 'eslint-module-utils/resolve'; import visit from 'eslint-module-utils/visit'; -import docsUrl from '../docsUrl'; import { dirname, join } from 'path'; import readPkgUp from 'eslint-module-utils/readPkgUp'; import values from 'object.values'; import includes from 'array-includes'; +import flatMap from 'array.prototype.flatmap'; + +import Exports, { recursivePatternCapture } from '../ExportMap'; +import docsUrl from '../docsUrl'; let FileEnumerator; let listFilesToProcess; @@ -40,12 +42,7 @@ try { const { listFilesToProcess: originalListFilesToProcess } = require('eslint/lib/util/glob-util'); listFilesToProcess = function (src, extensions) { - const patterns = src.reduce( - (carry, pattern) => carry.concat( - extensions.map((extension) => (/\*\*|\*\./).test(pattern) ? pattern : `${pattern}/**/*${extension}`), - ), - src, - ); + const patterns = src.concat(flatMap(src, (pattern) => extensions.map((extension) => (/\*\*|\*\./).test(pattern) ? pattern : `${pattern}/**/*${extension}`))); return originalListFilesToProcess(patterns); }; @@ -171,18 +168,17 @@ const isNodeModule = (path) => (/\/(node_modules)\//).test(path); const resolveFiles = (src, ignoreExports, context) => { const extensions = Array.from(getFileExtensions(context.settings)); - const srcFiles = new Set(); const srcFileList = listFilesToProcess(src, extensions); // prepare list of ignored files - const ignoredFilesList = listFilesToProcess(ignoreExports, extensions); + const ignoredFilesList = listFilesToProcess(ignoreExports, extensions); ignoredFilesList.forEach(({ filename }) => ignoredFiles.add(filename)); // prepare list of source files, don't consider files from node_modules - srcFileList.filter(({ filename }) => !isNodeModule(filename)).forEach(({ filename }) => { - srcFiles.add(filename); - }); - return srcFiles; + + return new Set( + srcFileList.filter(({ filename }) => !isNodeModule(filename)).map(({ filename }) => filename), + ); }; /**