From 1e57e0a4d7b08893ee07341246e1ad1756133e4e Mon Sep 17 00:00:00 2001 From: jeromeh Date: Mon, 14 Jun 2021 23:30:19 +0200 Subject: [PATCH] [Fix] `no-extraneous-dependencies`: add ESM intermediate package.json support Fixes #2120. --- CHANGELOG.md | 2 ++ src/core/packagePath.js | 8 ++++++-- src/rules/no-extraneous-dependencies.js | 2 +- tests/files/package.json | 1 + tests/files/webpack.config.js | 3 ++- tests/src/rules/no-extraneous-dependencies.js | 9 +++++++++ 6 files changed, 21 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b918675a3f..3452dd5413 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel ### Fixed - [`no-duplicates`]: ensure autofix avoids excessive newlines ([#2028], thanks [@ertrzyiks]) - [`extensions`]: avoid crashing on partially typed import/export statements ([#2118], thanks [@ljharb]) +- [`no-extraneous-dependencies`]: add ESM intermediate package.json support] ([#2121], thanks [@paztis]) ## [2.23.4] - 2021-05-29 @@ -804,6 +805,7 @@ for info on changes for earlier releases. [`memo-parser`]: ./memo-parser/README.md +[#2121]: https://github.com/benmosher/eslint-plugin-import/pull/2121 [#2099]: https://github.com/benmosher/eslint-plugin-import/pull/2099 [#2097]: https://github.com/benmosher/eslint-plugin-import/pull/2097 [#2090]: https://github.com/benmosher/eslint-plugin-import/pull/2090 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..76d65e2f58 100644 --- a/src/rules/no-extraneous-dependencies.js +++ b/src/rules/no-extraneous-dependencies.js @@ -130,7 +130,7 @@ function checkDependencyDeclaration(deps, packageName) { // in case of sub package.json inside a module // check the dependencies on all hierarchy const packageHierarchy = []; - const packageNameParts = packageName.split('/'); + const packageNameParts = packageName ? packageName.split('/') : []; packageNameParts.forEach((namePart, index) => { if (!namePart.startsWith('@')) { const ancestor = packageNameParts.slice(0, index + 1).join('/'); diff --git a/tests/files/package.json b/tests/files/package.json index 62bd3764a3..de1d802753 100644 --- a/tests/files/package.json +++ b/tests/files/package.json @@ -9,6 +9,7 @@ }, "dependencies": { "@org/package": "^1.0.0", + "esm-package": "^1.0.0", "jquery": "^3.1.0", "lodash.cond": "^4.3.0", "pkg-up": "^1.0.0", diff --git a/tests/files/webpack.config.js b/tests/files/webpack.config.js index 6a5dc0b88c..bbe81b359e 100644 --- a/tests/files/webpack.config.js +++ b/tests/files/webpack.config.js @@ -3,7 +3,8 @@ module.exports = { extensions: ['', '.js', '.jsx'], root: __dirname, alias: { - 'alias/chai$': 'chai' // alias for no-extraneous-dependencies tests + 'alias/chai$': 'chai', // alias for no-extraneous-dependencies tests + 'alias/esm-package': 'esm-package' // alias for no-extraneous-dependencies tests } }, } diff --git a/tests/src/rules/no-extraneous-dependencies.js b/tests/src/rules/no-extraneous-dependencies.js index 6bb84358ae..9d59ba948a 100644 --- a/tests/src/rules/no-extraneous-dependencies.js +++ b/tests/src/rules/no-extraneous-dependencies.js @@ -154,6 +154,15 @@ ruleTester.run('no-extraneous-dependencies', rule, { test({ code: 'import "rxjs/operators"', }), + + test({ + code: 'import "esm-package/esm-module";', + }), + + test({ + code: 'import "alias/esm-package/esm-module";', + settings: { 'import/resolver': 'webpack' }, + }), ], invalid: [ test({