Skip to content

Commit 6da9913

Browse files
jeromehljharb
jeromeh
authored andcommitted
[Fix] no-extraneous-dependencies: avoid crashing when an intermediate package.json lacks a name
Fixes import-js#2120.
1 parent 4079482 commit 6da9913

File tree

8 files changed

+21
-3
lines changed

8 files changed

+21
-3
lines changed

src/core/packagePath.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export function getFilePackagePath(filePath) {
1313
}
1414

1515
export function getFilePackageName(filePath) {
16-
const { pkg } = readPkgUp.sync({ cwd: filePath, normalize: false });
17-
return pkg && pkg.name;
16+
const { pkg, path } = readPkgUp.sync({ cwd: filePath, normalize: false });
17+
if (pkg) {
18+
// recursion in case of intermediate esm package.json without name found
19+
return pkg.name || getFilePackageName(dirname(dirname(path)));
20+
}
21+
return null;
1822
}

src/rules/no-extraneous-dependencies.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ function checkDependencyDeclaration(deps, packageName) {
130130
// in case of sub package.json inside a module
131131
// check the dependencies on all hierarchy
132132
const packageHierarchy = [];
133-
const packageNameParts = packageName.split('/');
133+
const packageNameParts = packageName ? packageName.split('/') : [];
134134
packageNameParts.forEach((namePart, index) => {
135135
if (!namePart.startsWith('@')) {
136136
const ancestor = packageNameParts.slice(0, index + 1).join('/');

tests/files/node_modules/esm-package/esm-module/index.js

Whitespace-only changes.

tests/files/node_modules/esm-package/esm-module/package.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/files/node_modules/esm-package/index.js

Whitespace-only changes.

tests/files/node_modules/esm-package/package.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/files/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
},
1010
"dependencies": {
1111
"@org/package": "^1.0.0",
12+
"esm-package": "^1.0.0",
1213
"jquery": "^3.1.0",
1314
"lodash.cond": "^4.3.0",
1415
"pkg-up": "^1.0.0",

tests/src/rules/no-extraneous-dependencies.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ ruleTester.run('no-extraneous-dependencies', rule, {
154154
test({
155155
code: 'import "rxjs/operators"',
156156
}),
157+
158+
test({
159+
code: 'import "esm-package/esm-module";',
160+
}),
157161
],
158162
invalid: [
159163
test({

0 commit comments

Comments
 (0)