Skip to content

Commit 1e57e0a

Browse files
jeromehljharb
authored andcommitted
[Fix] no-extraneous-dependencies: add ESM intermediate package.json support
Fixes #2120.
1 parent 4079482 commit 1e57e0a

6 files changed

Lines changed: 21 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
99
### Fixed
1010
- [`no-duplicates`]: ensure autofix avoids excessive newlines ([#2028], thanks [@ertrzyiks])
1111
- [`extensions`]: avoid crashing on partially typed import/export statements ([#2118], thanks [@ljharb])
12+
- [`no-extraneous-dependencies`]: add ESM intermediate package.json support] ([#2121], thanks [@paztis])
1213

1314
## [2.23.4] - 2021-05-29
1415

@@ -804,6 +805,7 @@ for info on changes for earlier releases.
804805

805806
[`memo-parser`]: ./memo-parser/README.md
806807

808+
[#2121]: https://github.com/benmosher/eslint-plugin-import/pull/2121
807809
[#2099]: https://github.com/benmosher/eslint-plugin-import/pull/2099
808810
[#2097]: https://github.com/benmosher/eslint-plugin-import/pull/2097
809811
[#2090]: https://github.com/benmosher/eslint-plugin-import/pull/2090

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/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/files/webpack.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module.exports = {
33
extensions: ['', '.js', '.jsx'],
44
root: __dirname,
55
alias: {
6-
'alias/chai$': 'chai' // alias for no-extraneous-dependencies tests
6+
'alias/chai$': 'chai', // alias for no-extraneous-dependencies tests
7+
'alias/esm-package': 'esm-package' // alias for no-extraneous-dependencies tests
78
}
89
},
910
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ 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+
}),
161+
162+
test({
163+
code: 'import "alias/esm-package/esm-module";',
164+
settings: { 'import/resolver': 'webpack' },
165+
}),
157166
],
158167
invalid: [
159168
test({

0 commit comments

Comments
 (0)