Skip to content

Commit

Permalink
[New] no-extraneous-dependencies: added includeInternal option to v…
Browse files Browse the repository at this point in the history
…alidate imports of internal dependencies

Fixes #1678
  • Loading branch information
bdwain committed Sep 3, 2022
1 parent d7c4f94 commit 881b431
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 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
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
6 changes: 5 additions & 1 deletion src/rules/no-extraneous-dependencies.js
Expand Up @@ -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;
}

Expand Down Expand Up @@ -245,6 +247,7 @@ module.exports = {
'peerDependencies': { 'type': ['boolean', 'array'] },
'bundledDependencies': { 'type': ['boolean', 'array'] },
'packageDir': { 'type': ['string', 'array'] },
'includeInternal': { 'type': ['boolean'] },
},
'additionalProperties': false,
},
Expand All @@ -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) => {
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

0 comments on commit 881b431

Please sign in to comment.