Skip to content

Commit fc361a9

Browse files
U812320ljharb
authored andcommitted
[Fix] no-extraneous-dependencies: allow wrong path
- If you pass only one path to a package.json file, then this path should be correct - If you pass multiple paths, there are some situations when those paths point to a wrong path, this happens typically in a nx monorepo with husky -- NX will run eslint in the projects folder, so we need to grab the root package.json -- Husky will run in the root folder, so one of the path given will be an incorrect path, but we do not want throw there, otherwise the rull will fail
1 parent 6554bd5 commit fc361a9

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1010
- [`dynamic-import-chunkname`]: add `allowEmpty` option to allow empty leading comments ([#2942], thanks [@JiangWeixian])
1111
- [`dynamic-import-chunkname`]: Allow empty chunk name when webpackMode: 'eager' is set; add suggestions to remove name in eager mode ([#3004], thanks [@amsardesai])
1212

13+
### Fixed
14+
- [`no-extraneous-dependencies`]: allow wrong path ([#3012], thanks [@chabb])
15+
1316
### Changed
1417
- [Docs] `no-extraneous-dependencies`: Make glob pattern description more explicit ([#2944], thanks [@mulztob])
1518
- [`no-unused-modules`]: add console message to help debug [#2866]
@@ -1116,6 +1119,7 @@ for info on changes for earlier releases.
11161119

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

1122+
[#3012]: https://github.com/import-js/eslint-plugin-import/pull/3012
11191123
[#3004]: https://github.com/import-js/eslint-plugin-import/pull/3004
11201124
[#2991]: https://github.com/import-js/eslint-plugin-import/pull/2991
11211125
[#2989]: https://github.com/import-js/eslint-plugin-import/pull/2989
@@ -1733,6 +1737,7 @@ for info on changes for earlier releases.
17331737
[@bradzacher]: https://github.com/bradzacher
17341738
[@brendo]: https://github.com/brendo
17351739
[@brettz9]: https://github.com/brettz9
1740+
[@chabb]: https://github.com/chabb
17361741
[@Chamion]: https://github.com/Chamion
17371742
[@charlessuh]: https://github.com/charlessuh
17381743
[@charpeni]: https://github.com/charpeni

src/rules/no-extraneous-dependencies.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ function extractDepFields(pkg) {
4242

4343
function getPackageDepFields(packageJsonPath, throwAtRead) {
4444
if (!depFieldCache.has(packageJsonPath)) {
45-
const depFields = extractDepFields(readJSON(packageJsonPath, throwAtRead));
46-
depFieldCache.set(packageJsonPath, depFields);
45+
const packageJson = readJSON(packageJsonPath, throwAtRead);
46+
if (packageJson) {
47+
const depFields = extractDepFields(packageJson);
48+
depFieldCache.set(packageJsonPath, depFields);
49+
}
4750
}
4851

4952
return depFieldCache.get(packageJsonPath);
@@ -72,10 +75,12 @@ function getDependencies(context, packageDir) {
7275
// use rule config to find package.json
7376
paths.forEach((dir) => {
7477
const packageJsonPath = path.join(dir, 'package.json');
75-
const _packageContent = getPackageDepFields(packageJsonPath, true);
76-
Object.keys(packageContent).forEach((depsKey) => {
77-
Object.assign(packageContent[depsKey], _packageContent[depsKey]);
78-
});
78+
const _packageContent = getPackageDepFields(packageJsonPath, paths.length === 1);
79+
if (_packageContent) {
80+
Object.keys(packageContent).forEach((depsKey) => {
81+
Object.assign(packageContent[depsKey], _packageContent[depsKey]);
82+
});
83+
}
7984
});
8085
} else {
8186
const packageJsonPath = pkgUp({

tests/src/rules/no-extraneous-dependencies.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const packageDirWithEmpty = path.join(__dirname, '../../files/empty');
2626
const packageDirBundleDeps = path.join(__dirname, '../../files/bundled-dependencies/as-array-bundle-deps');
2727
const packageDirBundledDepsAsObject = path.join(__dirname, '../../files/bundled-dependencies/as-object');
2828
const packageDirBundledDepsRaceCondition = path.join(__dirname, '../../files/bundled-dependencies/race-condition');
29+
const emptyPackageDir = path.join(__dirname, '../../files/empty-folder');
2930

3031
const {
3132
dependencies: deps,
@@ -104,6 +105,14 @@ ruleTester.run('no-extraneous-dependencies', rule, {
104105
code: 'import leftpad from "left-pad";',
105106
options: [{ packageDir: packageDirMonoRepoRoot }],
106107
}),
108+
test({
109+
code: 'import leftpad from "left-pad";',
110+
options: [{ packageDir: [emptyPackageDir, packageDirMonoRepoRoot] }],
111+
}),
112+
test({
113+
code: 'import leftpad from "left-pad";',
114+
options: [{ packageDir: [packageDirMonoRepoRoot, emptyPackageDir] }],
115+
}),
107116
test({
108117
code: 'import react from "react";',
109118
options: [{ packageDir: [packageDirMonoRepoRoot, packageDirMonoRepoWithNested] }],

0 commit comments

Comments
 (0)