Skip to content

Commit

Permalink
[Fix] no-extraneous-dependencies: allow wrong path
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
U812320 authored and ljharb committed Jun 3, 2024
1 parent 6554bd5 commit fc361a9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
- [`dynamic-import-chunkname`]: add `allowEmpty` option to allow empty leading comments ([#2942], thanks [@JiangWeixian])
- [`dynamic-import-chunkname`]: Allow empty chunk name when webpackMode: 'eager' is set; add suggestions to remove name in eager mode ([#3004], thanks [@amsardesai])

### Fixed
- [`no-extraneous-dependencies`]: allow wrong path ([#3012], thanks [@chabb])

### Changed
- [Docs] `no-extraneous-dependencies`: Make glob pattern description more explicit ([#2944], thanks [@mulztob])
- [`no-unused-modules`]: add console message to help debug [#2866]
Expand Down Expand Up @@ -1116,6 +1119,7 @@ for info on changes for earlier releases.

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

[#3012]: https://github.com/import-js/eslint-plugin-import/pull/3012
[#3004]: https://github.com/import-js/eslint-plugin-import/pull/3004
[#2991]: https://github.com/import-js/eslint-plugin-import/pull/2991
[#2989]: https://github.com/import-js/eslint-plugin-import/pull/2989
Expand Down Expand Up @@ -1733,6 +1737,7 @@ for info on changes for earlier releases.
[@bradzacher]: https://github.com/bradzacher
[@brendo]: https://github.com/brendo
[@brettz9]: https://github.com/brettz9
[@chabb]: https://github.com/chabb
[@Chamion]: https://github.com/Chamion
[@charlessuh]: https://github.com/charlessuh
[@charpeni]: https://github.com/charpeni
Expand Down
17 changes: 11 additions & 6 deletions src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ function extractDepFields(pkg) {

function getPackageDepFields(packageJsonPath, throwAtRead) {
if (!depFieldCache.has(packageJsonPath)) {
const depFields = extractDepFields(readJSON(packageJsonPath, throwAtRead));
depFieldCache.set(packageJsonPath, depFields);
const packageJson = readJSON(packageJsonPath, throwAtRead);
if (packageJson) {
const depFields = extractDepFields(packageJson);
depFieldCache.set(packageJsonPath, depFields);
}
}

return depFieldCache.get(packageJsonPath);
Expand Down Expand Up @@ -72,10 +75,12 @@ function getDependencies(context, packageDir) {
// use rule config to find package.json
paths.forEach((dir) => {
const packageJsonPath = path.join(dir, 'package.json');
const _packageContent = getPackageDepFields(packageJsonPath, true);
Object.keys(packageContent).forEach((depsKey) => {
Object.assign(packageContent[depsKey], _packageContent[depsKey]);
});
const _packageContent = getPackageDepFields(packageJsonPath, paths.length === 1);
if (_packageContent) {
Object.keys(packageContent).forEach((depsKey) => {
Object.assign(packageContent[depsKey], _packageContent[depsKey]);
});
}
});
} else {
const packageJsonPath = pkgUp({
Expand Down
9 changes: 9 additions & 0 deletions tests/src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const packageDirWithEmpty = path.join(__dirname, '../../files/empty');
const packageDirBundleDeps = path.join(__dirname, '../../files/bundled-dependencies/as-array-bundle-deps');
const packageDirBundledDepsAsObject = path.join(__dirname, '../../files/bundled-dependencies/as-object');
const packageDirBundledDepsRaceCondition = path.join(__dirname, '../../files/bundled-dependencies/race-condition');
const emptyPackageDir = path.join(__dirname, '../../files/empty-folder');

const {
dependencies: deps,
Expand Down Expand Up @@ -104,6 +105,14 @@ ruleTester.run('no-extraneous-dependencies', rule, {
code: 'import leftpad from "left-pad";',
options: [{ packageDir: packageDirMonoRepoRoot }],
}),
test({
code: 'import leftpad from "left-pad";',
options: [{ packageDir: [emptyPackageDir, packageDirMonoRepoRoot] }],
}),
test({
code: 'import leftpad from "left-pad";',
options: [{ packageDir: [packageDirMonoRepoRoot, emptyPackageDir] }],
}),
test({
code: 'import react from "react";',
options: [{ packageDir: [packageDirMonoRepoRoot, packageDirMonoRepoWithNested] }],
Expand Down

0 comments on commit fc361a9

Please sign in to comment.