Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New] no-extraneous-dependencies: added includeInternal option to validate imports of internal dependencies #2541

Merged
merged 1 commit into from Sep 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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])
Expand Down Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion 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.

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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');
```


Expand Down
9 changes: 8 additions & 1 deletion src/rules/no-extraneous-dependencies.js
Expand Up @@ -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;
}

Expand Down Expand Up @@ -261,6 +266,7 @@ module.exports = {
'peerDependencies': { 'type': ['boolean', 'array'] },
'bundledDependencies': { 'type': ['boolean', 'array'] },
'packageDir': { 'type': ['string', 'array'] },
'includeInternal': { 'type': ['boolean'] },
},
'additionalProperties': false,
},
Expand All @@ -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) => {
Expand Down
12 changes: 12 additions & 0 deletions tests/src/rules/no-extraneous-dependencies.js
Expand Up @@ -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',
}],
}),
],
});

Expand Down