Skip to content

Commit

Permalink
[Fix] no-restricted-paths: enhance performance for exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
malykhinvi authored and ljharb committed Apr 2, 2021
1 parent 359b6e5 commit 17a445d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -28,6 +28,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- [`no-named-default`]: ignore Flow import type and typeof ([#1983], thanks [@christianvuerings])
- [`no-extraneous-dependencies`]: Exclude flow `typeof` imports ([#1534], thanks [@devongovett])
- [`newline-after-import`]: respect decorator annotations ([#1985], thanks [@lilling])
- [`no-restricted-paths`]: enhance performance for zones with `except` paths ([#2022], thanks [@malykhinvi])

### Changed
- [Generic Import Callback] Make callback for all imports once in rules ([#1237], thanks [@ljqx])
Expand Down Expand Up @@ -763,6 +764,7 @@ for info on changes for earlier releases.

[`memo-parser`]: ./memo-parser/README.md

[#2022]: https://github.com/benmosher/eslint-plugin-import/pull/2022
[#2021]: https://github.com/benmosher/eslint-plugin-import/pull/2021
[#1997]: https://github.com/benmosher/eslint-plugin-import/pull/1997
[#1993]: https://github.com/benmosher/eslint-plugin-import/pull/1993
Expand Down
22 changes: 15 additions & 7 deletions src/rules/no-restricted-paths.js
Expand Up @@ -68,26 +68,34 @@ module.exports = {
});
}

const zoneExceptions = matchingZones.map((zone) => {
const exceptionPaths = zone.except || [];
const absoluteFrom = path.resolve(basePath, zone.from);
const absoluteExceptionPaths = exceptionPaths.map((exceptionPath) => path.resolve(absoluteFrom, exceptionPath));
const hasValidExceptionPaths = absoluteExceptionPaths
.every((absoluteExceptionPath) => isValidExceptionPath(absoluteFrom, absoluteExceptionPath));

return {
absoluteExceptionPaths,
hasValidExceptionPaths,
};
});

function checkForRestrictedImportPath(importPath, node) {
const absoluteImportPath = resolve(importPath, context);

if (!absoluteImportPath) {
return;
}

matchingZones.forEach((zone) => {
const exceptionPaths = zone.except || [];
matchingZones.forEach((zone, index) => {
const absoluteFrom = path.resolve(basePath, zone.from);

if (!containsPath(absoluteImportPath, absoluteFrom)) {
return;
}

const absoluteExceptionPaths = exceptionPaths.map((exceptionPath) =>
path.resolve(absoluteFrom, exceptionPath)
);
const hasValidExceptionPaths = absoluteExceptionPaths
.every((absoluteExceptionPath) => isValidExceptionPath(absoluteFrom, absoluteExceptionPath));
const { hasValidExceptionPaths, absoluteExceptionPaths } = zoneExceptions[index];

if (!hasValidExceptionPaths) {
reportInvalidExceptionPath(node);
Expand Down

0 comments on commit 17a445d

Please sign in to comment.