Skip to content

Commit

Permalink
[Defect 2120][no-extraneous-dependencies] add ESM intermediate packag…
Browse files Browse the repository at this point in the history
…e.json support
  • Loading branch information
jeromeh committed Jun 14, 2021
1 parent 4079482 commit 9552589
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 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;
}
2 changes: 1 addition & 1 deletion src/rules/no-extraneous-dependencies.js
Expand Up @@ -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('/');
Expand Down
1 change: 1 addition & 0 deletions tests/files/package.json
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion tests/files/webpack.config.js
Expand Up @@ -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
}
},
}
9 changes: 9 additions & 0 deletions tests/src/rules/no-extraneous-dependencies.js
Expand Up @@ -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({
Expand Down

0 comments on commit 9552589

Please sign in to comment.