Skip to content

Commit 5831fcd

Browse files
meowtecljharb
authored andcommitted
[Refactor] no-extraneous-dependencies improve performance using cache
Extracted from import-js#2374.
1 parent d7c4f94 commit 5831fcd

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
3535
- [Performance] `ExportMap`: add caching after parsing for an ambiguous module ([#2531], thanks [@stenin-nikita])
3636
- [Docs] [`no-useless-path-segments`]: fix paths ([#2424], thanks [@s-h-a-d-o-w])
3737
- [Tests] [`no-cycle`]: add passing test cases ([#2438], thanks [@georeith])
38+
- [Refactor] [`no-extraneous-dependencies`] improve performance using cache ([#2374], thanks [@meowtec])
3839

3940
## [2.26.0] - 2022-04-05
4041

@@ -1029,6 +1030,7 @@ for info on changes for earlier releases.
10291030
[#2387]: https://github.com/import-js/eslint-plugin-import/pull/2387
10301031
[#2381]: https://github.com/import-js/eslint-plugin-import/pull/2381
10311032
[#2378]: https://github.com/import-js/eslint-plugin-import/pull/2378
1033+
[#2374]: https://github.com/import-js/eslint-plugin-import/pull/2374
10321034
[#2371]: https://github.com/import-js/eslint-plugin-import/pull/2371
10331035
[#2367]: https://github.com/import-js/eslint-plugin-import/pull/2367
10341036
[#2332]: https://github.com/import-js/eslint-plugin-import/pull/2332
@@ -1663,6 +1665,7 @@ for info on changes for earlier releases.
16631665
[@Maxim-Mazurok]: https://github.com/Maxim-Mazurok
16641666
[@maxkomarychev]: https://github.com/maxkomarychev
16651667
[@maxmalov]: https://github.com/maxmalov
1668+
[@meowtec]: https://github.com/meowtec
16661669
[@mgwalker]: https://github.com/mgwalker
16671670
[@mhmadhamster]: https://github.com/MhMadHamster
16681671
[@MikeyBeLike]: https://github.com/MikeyBeLike

src/rules/no-extraneous-dependencies.js

+31-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'path';
22
import fs from 'fs';
3-
import readPkgUp from 'eslint-module-utils/readPkgUp';
3+
import pkgUp from 'eslint-module-utils/pkgUp';
44
import minimatch from 'minimatch';
55
import resolve from 'eslint-module-utils/resolve';
66
import moduleVisitor from 'eslint-module-utils/moduleVisitor';
@@ -18,6 +18,16 @@ function arrayOrKeys(arrayOrObject) {
1818
return Array.isArray(arrayOrObject) ? arrayOrObject : Object.keys(arrayOrObject);
1919
}
2020

21+
function readJSON(jsonPath, throwException) {
22+
try {
23+
return JSON.parse(fs.readFileSync(jsonPath, 'utf8'));
24+
} catch (err) {
25+
if (throwException) {
26+
throw err;
27+
}
28+
}
29+
}
30+
2131
function extractDepFields(pkg) {
2232
return {
2333
dependencies: pkg.dependencies || {},
@@ -30,6 +40,15 @@ function extractDepFields(pkg) {
3040
};
3141
}
3242

43+
function getPackageDepFields(packageJsonPath, throwAtRead) {
44+
if (!depFieldCache.has(packageJsonPath)) {
45+
const depFields = extractDepFields(readJSON(packageJsonPath, throwAtRead));
46+
depFieldCache.set(packageJsonPath, depFields);
47+
}
48+
49+
return depFieldCache.get(packageJsonPath);
50+
}
51+
3352
function getDependencies(context, packageDir) {
3453
let paths = [];
3554
try {
@@ -53,24 +72,21 @@ function getDependencies(context, packageDir) {
5372
// use rule config to find package.json
5473
paths.forEach(dir => {
5574
const packageJsonPath = path.join(dir, 'package.json');
56-
if (!depFieldCache.has(packageJsonPath)) {
57-
const depFields = extractDepFields(
58-
JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')),
59-
);
60-
depFieldCache.set(packageJsonPath, depFields);
61-
}
62-
const _packageContent = depFieldCache.get(packageJsonPath);
75+
const _packageContent = getPackageDepFields(packageJsonPath, true);
6376
Object.keys(packageContent).forEach(depsKey =>
6477
Object.assign(packageContent[depsKey], _packageContent[depsKey]),
6578
);
6679
});
6780
} else {
81+
const packageJsonPath = pkgUp({
82+
cwd: context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename(),
83+
normalize: false,
84+
});
85+
6886
// use closest package.json
6987
Object.assign(
7088
packageContent,
71-
extractDepFields(
72-
readPkgUp({ cwd: context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename(), normalize: false }).pkg,
73-
),
89+
getPackageDepFields(packageJsonPath, false),
7490
);
7591
}
7692

@@ -267,4 +283,8 @@ module.exports = {
267283
reportIfMissing(context, deps, depsOptions, node, source.value);
268284
}, { commonjs: true });
269285
},
286+
287+
'Program:exit'() {
288+
depFieldCache.clear();
289+
},
270290
};

0 commit comments

Comments
 (0)