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 committed Jun 3, 2024
1 parent 6554bd5 commit f0c9fd2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
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 f0c9fd2

Please sign in to comment.