diff --git a/CHANGELOG.md b/CHANGELOG.md index e349c4a0bf..1ffc3d0f0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange - [`no-restricted-paths`]: support arrays for `from` and `target` options ([#2466], thanks [@AdriAt360]) - [`no-anonymous-default-export`]: add `allowNew` option ([#2505], thanks [@DamienCassou]) - [`order`]: Add `distinctGroup` option ([#2395], thanks [@hyperupcall]) +- [`no-extraneous-dependencies`]: Add `includeInternal` option ([#2541], thanks [@bdwain]) ### Fixed - [`order`]: move nested imports closer to main import entry ([#2396], thanks [@pri1311]) diff --git a/docs/rules/no-extraneous-dependencies.md b/docs/rules/no-extraneous-dependencies.md index cdc0a913fe..4d9f035d29 100644 --- a/docs/rules/no-extraneous-dependencies.md +++ b/docs/rules/no-extraneous-dependencies.md @@ -1,7 +1,7 @@ # import/no-extraneous-dependencies: Forbid the use of extraneous packages Forbid the import of external modules that are not declared in the `package.json`'s `dependencies`, `devDependencies`, `optionalDependencies`, `peerDependencies`, or `bundledDependencies`. -The closest parent `package.json` will be used. If no `package.json` is found, the rule will not lint anything. This behavior can be changed with the rule option `packageDir`. +The closest parent `package.json` will be used. If no `package.json` is found, the rule will not lint anything. This behavior can be changed with the rule option `packageDir`. Normally ignores imports of modules marked internal, but this can be changed with the rule option `includeInternal`. Modules have to be installed for this rule to work. @@ -31,6 +31,12 @@ You can also use an array of globs instead of literal booleans: When using an array of globs, the setting will be set to `true` (no errors reported) if the name of the file being linted matches a single glob in the array, and `false` otherwise. +There is a boolean option called `includeInternal`, which enables the checking of internal modules, which are otherwise ignored by this rule. + +```js +"import/no-extraneous-dependencies": ["error", {"includeInternal": true}] +``` + Also there is one more option called `packageDir`, this option is to specify the path to the folder containing package.json. If provided as a relative path string, will be computed relative to the current working directory at linter execution time. If this is not ideal (does not work with some editor integrations), consider using `__dirname` to provide a path relative to your configuration. @@ -99,6 +105,10 @@ var isArray = require('lodash.isarray'); /* eslint import/no-extraneous-dependencies: ["error", {"bundledDependencies": false}] */ import foo from '"@generated/foo"'; var foo = require('"@generated/foo"'); + +/* eslint import/no-extraneous-dependencies: ["error", {"includeInternal": true}] */ +import foo from './foo'; +var foo = require('./foo'); ``` diff --git a/src/rules/no-extraneous-dependencies.js b/src/rules/no-extraneous-dependencies.js index b54ee28bb7..4bf146ed9e 100644 --- a/src/rules/no-extraneous-dependencies.js +++ b/src/rules/no-extraneous-dependencies.js @@ -167,7 +167,9 @@ function reportIfMissing(context, deps, depsOptions, node, name) { return; } - if (importType(name, context) !== 'external') { + const typeOfImport = importType(name, context); + + if (typeOfImport !== 'external' && (typeOfImport !== 'internal' || !depsOptions.verifyInternalDeps)) { return; } @@ -245,6 +247,7 @@ module.exports = { 'peerDependencies': { 'type': ['boolean', 'array'] }, 'bundledDependencies': { 'type': ['boolean', 'array'] }, 'packageDir': { 'type': ['string', 'array'] }, + 'includeInternal': { 'type': ['boolean'] }, }, 'additionalProperties': false, }, @@ -261,6 +264,7 @@ module.exports = { allowOptDeps: testConfig(options.optionalDependencies, filename) !== false, allowPeerDeps: testConfig(options.peerDependencies, filename) !== false, allowBundledDeps: testConfig(options.bundledDependencies, filename) !== false, + verifyInternalDeps: !!options.includeInternal, }; return moduleVisitor((source, node) => { diff --git a/tests/src/rules/no-extraneous-dependencies.js b/tests/src/rules/no-extraneous-dependencies.js index d4e3886bed..7f916ab9f4 100644 --- a/tests/src/rules/no-extraneous-dependencies.js +++ b/tests/src/rules/no-extraneous-dependencies.js @@ -392,6 +392,18 @@ ruleTester.run('no-extraneous-dependencies', rule, { message: `'esm-package-not-in-pkg-json' should be listed in the project's dependencies. Run 'npm i -S esm-package-not-in-pkg-json' to add it`, }], }), + + test({ + code: 'import "not-a-dependency"', + settings: { + 'import/resolver': { node: { paths: [ path.join(__dirname, '../../files') ] } }, + 'import/internal-regex': '^not-a-dependency.*', + }, + options: [{ includeInternal: true }], + errors: [{ + message: '\'not-a-dependency\' should be listed in the project\'s dependencies. Run \'npm i -S not-a-dependency\' to add it', + }], + }), ], });