Skip to content

Commit 703e9f9

Browse files
committed
[Refactor] no-duplicates, no-unused-modules: use flatMap instead of map + filter
1 parent 70f24f1 commit 703e9f9

File tree

6 files changed

+25
-26
lines changed

6 files changed

+25
-26
lines changed

src/rules/no-default-export.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ module.exports = {
2727
},
2828

2929
ExportNamedDeclaration(node) {
30-
node.specifiers.filter((specifier) => (specifier.exported.name || specifier.exported.value) === 'default').forEach((specifier) => {
31-
const { loc } = context.getSourceCode().getFirstTokens(node)[1] || {};
32-
if (specifier.type === 'ExportDefaultSpecifier') {
33-
context.report({ node, message: preferNamed, loc });
34-
} else if (specifier.type === 'ExportSpecifier') {
35-
context.report({ node, message: noAliasDefault(specifier), loc });
36-
}
37-
});
30+
node.specifiers
31+
.filter((specifier) => (specifier.exported.name || specifier.exported.value) === 'default')
32+
.forEach((specifier) => {
33+
const { loc } = context.getSourceCode().getFirstTokens(node)[1] || {};
34+
if (specifier.type === 'ExportDefaultSpecifier') {
35+
context.report({ node, message: preferNamed, loc });
36+
} else if (specifier.type === 'ExportSpecifier') {
37+
context.report({ node, message: noAliasDefault(specifier), loc });
38+
}
39+
});
3840
},
3941
};
4042
},

src/rules/no-duplicates.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import resolve from 'eslint-module-utils/resolve';
2-
import docsUrl from '../docsUrl';
32
import semver from 'semver';
3+
import flatMap from 'array.prototype.flatmap';
4+
5+
import docsUrl from '../docsUrl';
46

57
let typescriptPkg;
68
try {
@@ -51,7 +53,7 @@ function getFix(first, rest, sourceCode, context) {
5153
}
5254

5355
const defaultImportNames = new Set(
54-
[first, ...rest].map(getDefaultImportName).filter(Boolean),
56+
flatMap([].concat(first, rest || []), (x) => getDefaultImportName(x) || []),
5557
);
5658

5759
// Bail if there are multiple different default import names – it's up to the
@@ -62,10 +64,7 @@ function getFix(first, rest, sourceCode, context) {
6264

6365
// Leave it to the user to handle comments. Also skip `import * as ns from
6466
// './foo'` imports, since they cannot be merged into another import.
65-
const restWithoutComments = rest.filter((node) => !(
66-
hasProblematicComments(node, sourceCode)
67-
|| hasNamespace(node)
68-
));
67+
const restWithoutComments = rest.filter((node) => !hasProblematicComments(node, sourceCode) && !hasNamespace(node));
6968

7069
const specifiers = restWithoutComments
7170
.map((node) => {

src/rules/no-restricted-paths.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,11 @@ module.exports = {
7777
const restrictedPaths = options.zones || [];
7878
const basePath = options.basePath || process.cwd();
7979
const currentFilename = context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename();
80-
const matchingZones = restrictedPaths.filter((zone) => [].concat(zone.target)
81-
.map((target) => path.resolve(basePath, target))
82-
.some((targetPath) => isMatchingTargetPath(currentFilename, targetPath)));
80+
const matchingZones = restrictedPaths.filter(
81+
(zone) => [].concat(zone.target)
82+
.map((target) => path.resolve(basePath, target))
83+
.some((targetPath) => isMatchingTargetPath(currentFilename, targetPath)),
84+
);
8385

8486
function isMatchingTargetPath(filename, targetPath) {
8587
if (isGlob(targetPath)) {
@@ -231,8 +233,7 @@ module.exports = {
231233
reportInvalidExceptions(validatorsWithInvalidExceptions, node);
232234

233235
const applicableValidatorsForImportPathExcludingExceptions = applicableValidatorsForImportPath
234-
.filter((validator) => validator.hasValidExceptions)
235-
.filter((validator) => !validator.isPathException(absoluteImportPath));
236+
.filter((validator) => validator.hasValidExceptions && !validator.isPathException(absoluteImportPath));
236237
reportImportsInRestrictedZone(applicableValidatorsForImportPathExcludingExceptions, node, importPath, zone.message);
237238
});
238239
}

src/rules/no-unused-modules.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ const resolveFiles = (src, ignoreExports, context) => {
177177
// prepare list of source files, don't consider files from node_modules
178178

179179
return new Set(
180-
srcFileList.filter(({ filename }) => !isNodeModule(filename)).map(({ filename }) => filename),
180+
flatMap(srcFileList, ({ filename }) => isNodeModule(filename) ? [] : filename),
181181
);
182182
};
183183

@@ -359,9 +359,7 @@ const fileIsInPkg = (file) => {
359359
};
360360

361361
const checkPkgFieldObject = (pkgField) => {
362-
const pkgFieldFiles = values(pkgField)
363-
.filter((value) => typeof value !== 'boolean')
364-
.map((value) => join(basePath, value));
362+
const pkgFieldFiles = flatMap(values(pkgField), (value) => typeof value === 'boolean' ? [] : join(basePath, value));
365363

366364
if (includes(pkgFieldFiles, file)) {
367365
return true;

tests/src/package.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ describe('package', function () {
2727
expect(err).not.to.exist;
2828

2929
files.filter(isJSFile).forEach(function (f) {
30-
expect(module.rules).to.have
31-
.property(path.basename(f, '.js'));
30+
expect(module.rules).to.have.property(path.basename(f, '.js'));
3231
});
3332

3433
done();

tests/src/rules/order.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2736,7 +2736,7 @@ ruleTester.run('order', rule, {
27362736
}],
27372737
}),
27382738
],
2739-
].filter((t) => !!t),
2739+
].filter(Boolean),
27402740
});
27412741

27422742
context('TypeScript', function () {

0 commit comments

Comments
 (0)