Skip to content

Commit

Permalink
[Refactor] no-unused-modules: use array.prototype.flatmap
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jul 4, 2023
1 parent 2c196b0 commit 3a5ad34
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 10 additions & 14 deletions src/rules/no-unused-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -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),
);
};

/**
Expand Down

0 comments on commit 3a5ad34

Please sign in to comment.