Skip to content

Commit

Permalink
[Fix] no-extraneous-dependencies: avoid crashing when an intermedia…
Browse files Browse the repository at this point in the history
…te package.json lacks a name

Fixes import-js#2120.
  • Loading branch information
jeromeh authored and JCB-K committed Jun 18, 2021
1 parent 1104ec1 commit a422246
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/core/packagePath.js
Expand Up @@ -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;
}
7 changes: 7 additions & 0 deletions src/rules/no-extraneous-dependencies.js
Expand Up @@ -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) ||
Expand Down

0 comments on commit a422246

Please sign in to comment.