diff --git a/CHANGELOG.md b/CHANGELOG.md index f54e6c07e..438efefc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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]) @@ -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 diff --git a/src/rules/no-restricted-paths.js b/src/rules/no-restricted-paths.js index a856a8bb4..e2b11957f 100644 --- a/src/rules/no-restricted-paths.js +++ b/src/rules/no-restricted-paths.js @@ -68,6 +68,19 @@ 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); @@ -75,19 +88,14 @@ module.exports = { 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);