From 74f39d901a7ce39888f9261d464590f7350bc40a Mon Sep 17 00:00:00 2001 From: bwain Date: Sat, 3 Sep 2022 01:54:08 -0500 Subject: [PATCH] [New] `no-extraneous-dependencies`: added includeInternal option to validate imports of internal dependencies Fixes #1678 --- CHANGELOG.md | 2 ++ docs/rules/no-extraneous-dependencies.md | 12 +++++++++++- src/rules/no-extraneous-dependencies.js | 9 ++++++++- tests/src/rules/no-extraneous-dependencies.js | 12 ++++++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a04c80a0..dd858a3af 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]) @@ -1008,6 +1009,7 @@ for info on changes for earlier releases. [`memo-parser`]: ./memo-parser/README.md +[#2541]: https://github.com/import-js/eslint-plugin-import/pull/2541 [#2531]: https://github.com/import-js/eslint-plugin-import/pull/2531 [#2511]: https://github.com/import-js/eslint-plugin-import/pull/2511 [#2506]: https://github.com/import-js/eslint-plugin-import/pull/2506 diff --git a/docs/rules/no-extraneous-dependencies.md b/docs/rules/no-extraneous-dependencies.md index cdc0a913f..4d9f035d2 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 4fc1a01d4..2e8b7fece 100644 --- a/src/rules/no-extraneous-dependencies.js +++ b/src/rules/no-extraneous-dependencies.js @@ -183,7 +183,12 @@ 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; } @@ -261,6 +266,7 @@ module.exports = { 'peerDependencies': { 'type': ['boolean', 'array'] }, 'bundledDependencies': { 'type': ['boolean', 'array'] }, 'packageDir': { 'type': ['string', 'array'] }, + 'includeInternal': { 'type': ['boolean'] }, }, 'additionalProperties': false, }, @@ -277,6 +283,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 d4e3886be..364921db3 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', + }], + }), ], });