Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] no-extraneous-dependencies: allow incorrect paths in packageDir #3012

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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