diff --git a/docs/rules/no-extraneous-dependencies.md b/docs/rules/no-extraneous-dependencies.md index 70c08809c..5813d5de2 100644 --- a/docs/rules/no-extraneous-dependencies.md +++ b/docs/rules/no-extraneous-dependencies.md @@ -54,6 +54,12 @@ folder layouts: "import/no-extraneous-dependencies": ["error", {"packageDir": ['./some-dir/', './root-pkg']}] ``` +There is `ignoreImports` options, it can be used to ignore some files or directories. + +```js +"import/no-extraneous-dependencies": ["error", {"ignoreImports": ['./some-root-dir/', '__mocks__']}] +``` + ## Rule Details Given the following `package.json`: diff --git a/src/rules/no-extraneous-dependencies.js b/src/rules/no-extraneous-dependencies.js index 65c396e67..58b51b31d 100644 --- a/src/rules/no-extraneous-dependencies.js +++ b/src/rules/no-extraneous-dependencies.js @@ -200,6 +200,7 @@ function reportIfMissing(context, deps, depsOptions, node, name) { if ( declarationStatus.isInDeps || + depsOptions.allowIgnoreImports || (depsOptions.allowDevDeps && declarationStatus.isInDevDeps) || (depsOptions.allowPeerDeps && declarationStatus.isInPeerDeps) || (depsOptions.allowOptDeps && declarationStatus.isInOptDeps) || @@ -216,6 +217,7 @@ function reportIfMissing(context, deps, depsOptions, node, name) { if ( declarationStatus.isInDeps || + depsOptions.allowIgnoreImports || (depsOptions.allowDevDeps && declarationStatus.isInDevDeps) || (depsOptions.allowPeerDeps && declarationStatus.isInPeerDeps) || (depsOptions.allowOptDeps && declarationStatus.isInOptDeps) || @@ -250,6 +252,16 @@ function testConfig(config, filename) { )); } +function testIgnoreImports(config, filename) { + if (!config) { + return false + } + return config.some(c => ( + filename.includes(value.substr(0, 2) === './' ? value.replace('.', process.cwd()) : value) + )) +} + + module.exports = { meta: { type: 'problem', @@ -268,6 +280,7 @@ module.exports = { 'packageDir': { 'type': ['string', 'array'] }, 'includeInternal': { 'type': ['boolean'] }, 'includeTypes': { 'type': ['boolean'] }, + 'ignoreImports': { 'type': ['array'] }, }, 'additionalProperties': false, }, @@ -280,6 +293,7 @@ module.exports = { const deps = getDependencies(context, options.packageDir) || extractDepFields({}); const depsOptions = { + allowIgnoreImports: testIgnoreImports(options.ignoreImports, filename) !== false, allowDevDeps: testConfig(options.devDependencies, filename) !== false, allowOptDeps: testConfig(options.optionalDependencies, filename) !== false, allowPeerDeps: testConfig(options.peerDependencies, filename) !== false,