Skip to content

Commit

Permalink
[Refactor] no-duplicates, no-unused-modules: use flatMap instea…
Browse files Browse the repository at this point in the history
…d of `map` + `filter`
  • Loading branch information
ljharb committed Jul 28, 2023
1 parent 70f24f1 commit 703e9f9
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 26 deletions.
18 changes: 10 additions & 8 deletions src/rules/no-default-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ module.exports = {
},

ExportNamedDeclaration(node) {
node.specifiers.filter((specifier) => (specifier.exported.name || specifier.exported.value) === 'default').forEach((specifier) => {
const { loc } = context.getSourceCode().getFirstTokens(node)[1] || {};
if (specifier.type === 'ExportDefaultSpecifier') {
context.report({ node, message: preferNamed, loc });
} else if (specifier.type === 'ExportSpecifier') {
context.report({ node, message: noAliasDefault(specifier), loc });
}
});
node.specifiers
.filter((specifier) => (specifier.exported.name || specifier.exported.value) === 'default')
.forEach((specifier) => {
const { loc } = context.getSourceCode().getFirstTokens(node)[1] || {};
if (specifier.type === 'ExportDefaultSpecifier') {
context.report({ node, message: preferNamed, loc });
} else if (specifier.type === 'ExportSpecifier') {
context.report({ node, message: noAliasDefault(specifier), loc });
}
});
},
};
},
Expand Down
11 changes: 5 additions & 6 deletions src/rules/no-duplicates.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import resolve from 'eslint-module-utils/resolve';
import docsUrl from '../docsUrl';
import semver from 'semver';
import flatMap from 'array.prototype.flatmap';

import docsUrl from '../docsUrl';

let typescriptPkg;
try {
Expand Down Expand Up @@ -51,7 +53,7 @@ function getFix(first, rest, sourceCode, context) {
}

const defaultImportNames = new Set(
[first, ...rest].map(getDefaultImportName).filter(Boolean),
flatMap([].concat(first, rest || []), (x) => getDefaultImportName(x) || []),
);

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

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

const specifiers = restWithoutComments
.map((node) => {
Expand Down
11 changes: 6 additions & 5 deletions src/rules/no-restricted-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ module.exports = {
const restrictedPaths = options.zones || [];
const basePath = options.basePath || process.cwd();
const currentFilename = context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename();
const matchingZones = restrictedPaths.filter((zone) => [].concat(zone.target)
.map((target) => path.resolve(basePath, target))
.some((targetPath) => isMatchingTargetPath(currentFilename, targetPath)));
const matchingZones = restrictedPaths.filter(
(zone) => [].concat(zone.target)
.map((target) => path.resolve(basePath, target))
.some((targetPath) => isMatchingTargetPath(currentFilename, targetPath)),
);

function isMatchingTargetPath(filename, targetPath) {
if (isGlob(targetPath)) {
Expand Down Expand Up @@ -231,8 +233,7 @@ module.exports = {
reportInvalidExceptions(validatorsWithInvalidExceptions, node);

const applicableValidatorsForImportPathExcludingExceptions = applicableValidatorsForImportPath
.filter((validator) => validator.hasValidExceptions)
.filter((validator) => !validator.isPathException(absoluteImportPath));
.filter((validator) => validator.hasValidExceptions && !validator.isPathException(absoluteImportPath));
reportImportsInRestrictedZone(applicableValidatorsForImportPathExcludingExceptions, node, importPath, zone.message);
});
}
Expand Down
6 changes: 2 additions & 4 deletions src/rules/no-unused-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ const resolveFiles = (src, ignoreExports, context) => {
// prepare list of source files, don't consider files from node_modules

return new Set(
srcFileList.filter(({ filename }) => !isNodeModule(filename)).map(({ filename }) => filename),
flatMap(srcFileList, ({ filename }) => isNodeModule(filename) ? [] : filename),
);
};

Expand Down Expand Up @@ -359,9 +359,7 @@ const fileIsInPkg = (file) => {
};

const checkPkgFieldObject = (pkgField) => {
const pkgFieldFiles = values(pkgField)
.filter((value) => typeof value !== 'boolean')
.map((value) => join(basePath, value));
const pkgFieldFiles = flatMap(values(pkgField), (value) => typeof value === 'boolean' ? [] : join(basePath, value));

if (includes(pkgFieldFiles, file)) {
return true;
Expand Down
3 changes: 1 addition & 2 deletions tests/src/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ describe('package', function () {
expect(err).not.to.exist;

files.filter(isJSFile).forEach(function (f) {
expect(module.rules).to.have
.property(path.basename(f, '.js'));
expect(module.rules).to.have.property(path.basename(f, '.js'));
});

done();
Expand Down
2 changes: 1 addition & 1 deletion tests/src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -2736,7 +2736,7 @@ ruleTester.run('order', rule, {
}],
}),
],
].filter((t) => !!t),
].filter(Boolean),
});

context('TypeScript', function () {
Expand Down

0 comments on commit 703e9f9

Please sign in to comment.