Skip to content

Commit 3a5ad34

Browse files
committed
[Refactor] no-unused-modules: use array.prototype.flatmap
1 parent 2c196b0 commit 3a5ad34

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1919
- [Docs] [`extensions`]: reference node ESM behavior ([#2748], thanks [@xM8WVqaG])
2020
- [Refactor] [`exports-last`]: use `array.prototype.findlastindex` (thanks [@ljharb])
2121
- [Refactor] [`no-anonymous-default-export`]: use `object.fromentries` (thanks [@ljharb])
22+
- [Refactor] [`no-unused-modules`]: use `array.prototype.flatmap` (thanks [@ljharb])
2223

2324
## [2.27.5] - 2023-01-16
2425

src/rules/no-unused-modules.js

+10-14
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44
* @author René Fermann
55
*/
66

7-
import Exports, { recursivePatternCapture } from '../ExportMap';
87
import { getFileExtensions } from 'eslint-module-utils/ignore';
98
import resolve from 'eslint-module-utils/resolve';
109
import visit from 'eslint-module-utils/visit';
11-
import docsUrl from '../docsUrl';
1210
import { dirname, join } from 'path';
1311
import readPkgUp from 'eslint-module-utils/readPkgUp';
1412
import values from 'object.values';
1513
import includes from 'array-includes';
14+
import flatMap from 'array.prototype.flatmap';
15+
16+
import Exports, { recursivePatternCapture } from '../ExportMap';
17+
import docsUrl from '../docsUrl';
1618

1719
let FileEnumerator;
1820
let listFilesToProcess;
@@ -40,12 +42,7 @@ try {
4042
const { listFilesToProcess: originalListFilesToProcess } = require('eslint/lib/util/glob-util');
4143

4244
listFilesToProcess = function (src, extensions) {
43-
const patterns = src.reduce(
44-
(carry, pattern) => carry.concat(
45-
extensions.map((extension) => (/\*\*|\*\./).test(pattern) ? pattern : `${pattern}/**/*${extension}`),
46-
),
47-
src,
48-
);
45+
const patterns = src.concat(flatMap(src, (pattern) => extensions.map((extension) => (/\*\*|\*\./).test(pattern) ? pattern : `${pattern}/**/*${extension}`)));
4946

5047
return originalListFilesToProcess(patterns);
5148
};
@@ -171,18 +168,17 @@ const isNodeModule = (path) => (/\/(node_modules)\//).test(path);
171168
const resolveFiles = (src, ignoreExports, context) => {
172169
const extensions = Array.from(getFileExtensions(context.settings));
173170

174-
const srcFiles = new Set();
175171
const srcFileList = listFilesToProcess(src, extensions);
176172

177173
// prepare list of ignored files
178-
const ignoredFilesList = listFilesToProcess(ignoreExports, extensions);
174+
const ignoredFilesList = listFilesToProcess(ignoreExports, extensions);
179175
ignoredFilesList.forEach(({ filename }) => ignoredFiles.add(filename));
180176

181177
// prepare list of source files, don't consider files from node_modules
182-
srcFileList.filter(({ filename }) => !isNodeModule(filename)).forEach(({ filename }) => {
183-
srcFiles.add(filename);
184-
});
185-
return srcFiles;
178+
179+
return new Set(
180+
srcFileList.filter(({ filename }) => !isNodeModule(filename)).map(({ filename }) => filename),
181+
);
186182
};
187183

188184
/**

0 commit comments

Comments
 (0)