diff --git a/src/core/packagePath.js b/src/core/packagePath.js index e95b066668..315ec0918c 100644 --- a/src/core/packagePath.js +++ b/src/core/packagePath.js @@ -13,6 +13,10 @@ export function getFilePackagePath(filePath) { } export function getFilePackageName(filePath) { - const { pkg } = readPkgUp.sync({ cwd: filePath, normalize: false }); - return pkg && pkg.name; + const { pkg, path } = readPkgUp.sync({ cwd: filePath, normalize: false }); + if (pkg) { + // recursion in case of intermediate esm package.json without name found + return pkg.name || getFilePackageName(dirname(dirname(path))); + } + return null; } diff --git a/src/rules/no-extraneous-dependencies.js b/src/rules/no-extraneous-dependencies.js index 8a6af2f617..1c7b61c6f7 100644 --- a/src/rules/no-extraneous-dependencies.js +++ b/src/rules/no-extraneous-dependencies.js @@ -184,8 +184,15 @@ function reportIfMissing(context, deps, depsOptions, node, name) { // test the real name from the resolved package.json // if not aliased imports (alias/react for example), importPackageName can be misinterpreted const realPackageName = getModuleRealName(resolved); + + if(!realPackageName){ + throw new Error(`cant find real package name for import ${name}`); + } + const realPackageNameDeclaration = checkDependencyDeclaration(deps, realPackageName); + + if (realPackageNameDeclaration.isInDeps || (depsOptions.allowDevDeps && realPackageNameDeclaration.isInDevDeps) || (depsOptions.allowPeerDeps && realPackageNameDeclaration.isInPeerDeps) ||