Skip to content

Commit

Permalink
[resolvers/webpack] [refactor] simplify loop
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored and ljharb committed Aug 26, 2024
1 parent bdff75d commit db8b95d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
5 changes: 4 additions & 1 deletion resolvers/webpack/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

## Unreleased

- [refactor] simplify loop ([#3029], thanks [@fregante])

## 0.13.8 - 2023-10-22
- [refactor] use `hasown` instead of `has`
- [deps] update `array.prototype.find`, `is-core-module`, `resolve`


## 0.13.7 - 2023-08-19
- [fix] use the `dirname` of the `configPath` as `basedir` ([#2859])

Expand Down Expand Up @@ -178,6 +179,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
### Added
- `interpret` configs (such as `.babel.js`). Thanks to [@gausie] for the initial PR ([#164], ages ago! 😅) and [@jquense] for tests ([#278]).

[#3029]: https://github.com/import-js/eslint-plugin-import/pull/3029
[#2287]: https://github.com/import-js/eslint-plugin-import/pull/2287
[#2023]: https://github.com/import-js/eslint-plugin-import/pull/2023
[#1967]: https://github.com/import-js/eslint-plugin-import/pull/1967
Expand Down Expand Up @@ -222,6 +224,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
[@benmvp]: https://github.com/benmvp
[@daltones]: https://github.com/daltones
[@echenley]: https://github.com/echenley
[@fregante]: https://github.com/fregante
[@gausie]: https://github.com/gausie
[@grahamb]: https://github.com/grahamb
[@graingert]: https://github.com/graingert
Expand Down
31 changes: 18 additions & 13 deletions resolvers/webpack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const findRoot = require('find-root');
const path = require('path');
const isEqual = require('lodash/isEqual');
const find = require('array.prototype.find');
const interpret = require('interpret');
const fs = require('fs');
const isCore = require('is-core-module');
Expand Down Expand Up @@ -293,17 +292,20 @@ const MAX_CACHE = 10;
const _cache = [];
function getResolveSync(configPath, webpackConfig, cwd) {
const cacheKey = { configPath, webpackConfig };
let cached = find(_cache, function (entry) { return isEqual(entry.key, cacheKey); });
if (!cached) {
cached = {
key: cacheKey,
value: createResolveSync(configPath, webpackConfig, cwd),
};
// put in front and pop last item
if (_cache.unshift(cached) > MAX_CACHE) {
_cache.pop();
for (let i = 0; i < _cache.length; i++) {
if (isEqual(_cache[i].key, cacheKey)) {
return _cache[i].value;
}
}

const cached = {
key: cacheKey,
value: createResolveSync(configPath, webpackConfig, cwd),
};
// put in front and pop last item
if (_cache.unshift(cached) > MAX_CACHE) {
_cache.pop();

Check warning on line 307 in resolvers/webpack/index.js

View check run for this annotation

Codecov / codecov/patch

resolvers/webpack/index.js#L307

Added line #L307 was not covered by tests
}
return cached.value;
}

Expand Down Expand Up @@ -409,9 +411,12 @@ exports.resolve = function (source, file, settings) {
if (typeof configIndex !== 'undefined' && webpackConfig.length > configIndex) {
webpackConfig = webpackConfig[configIndex];
} else {
webpackConfig = find(webpackConfig, function findFirstWithResolve(config) {
return !!config.resolve;
});
for (let i = 0; i < webpackConfig.length; i++) {
if (webpackConfig[i].resolve) {
webpackConfig = webpackConfig[i];
break;

Check warning on line 417 in resolvers/webpack/index.js

View check run for this annotation

Codecov / codecov/patch

resolvers/webpack/index.js#L414-L417

Added lines #L414 - L417 were not covered by tests
}
}
}
}

Expand Down
1 change: 0 additions & 1 deletion resolvers/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
},
"homepage": "https://github.com/import-js/eslint-plugin-import/tree/HEAD/resolvers/webpack",
"dependencies": {
"array.prototype.find": "^2.2.2",
"debug": "^3.2.7",
"enhanced-resolve": "^0.9.1",
"find-root": "^1.1.0",
Expand Down

0 comments on commit db8b95d

Please sign in to comment.