From a4222463062738e8f87fcf197fa169ac7e063c42 Mon Sep 17 00:00:00 2001 From: jeromeh Date: Fri, 18 Jun 2021 14:04:09 +0200 Subject: [PATCH] [Fix] `no-extraneous-dependencies`: avoid crashing when an intermediate package.json lacks a name Fixes #2120. --- src/core/packagePath.js | 8 ++++++-- src/rules/no-extraneous-dependencies.js | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) 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) ||