Skip to content

Commit 74f39d9

Browse files
bdwainljharb
authored andcommitted
[New] no-extraneous-dependencies: added includeInternal option to validate imports of internal dependencies
Fixes import-js#1678
1 parent 7f251b2 commit 74f39d9

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1212
- [`no-restricted-paths`]: support arrays for `from` and `target` options ([#2466], thanks [@AdriAt360])
1313
- [`no-anonymous-default-export`]: add `allowNew` option ([#2505], thanks [@DamienCassou])
1414
- [`order`]: Add `distinctGroup` option ([#2395], thanks [@hyperupcall])
15+
- [`no-extraneous-dependencies`]: Add `includeInternal` option ([#2541], thanks [@bdwain])
1516

1617
### Fixed
1718
- [`order`]: move nested imports closer to main import entry ([#2396], thanks [@pri1311])
@@ -1008,6 +1009,7 @@ for info on changes for earlier releases.
10081009

10091010
[`memo-parser`]: ./memo-parser/README.md
10101011

1012+
[#2541]: https://github.com/import-js/eslint-plugin-import/pull/2541
10111013
[#2531]: https://github.com/import-js/eslint-plugin-import/pull/2531
10121014
[#2511]: https://github.com/import-js/eslint-plugin-import/pull/2511
10131015
[#2506]: https://github.com/import-js/eslint-plugin-import/pull/2506

docs/rules/no-extraneous-dependencies.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# import/no-extraneous-dependencies: Forbid the use of extraneous packages
22

33
Forbid the import of external modules that are not declared in the `package.json`'s `dependencies`, `devDependencies`, `optionalDependencies`, `peerDependencies`, or `bundledDependencies`.
4-
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`.
4+
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`.
55

66
Modules have to be installed for this rule to work.
77

@@ -31,6 +31,12 @@ You can also use an array of globs instead of literal booleans:
3131

3232
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.
3333

34+
There is a boolean option called `includeInternal`, which enables the checking of internal modules, which are otherwise ignored by this rule.
35+
36+
```js
37+
"import/no-extraneous-dependencies": ["error", {"includeInternal": true}]
38+
```
39+
3440
Also there is one more option called `packageDir`, this option is to specify the path to the folder containing package.json.
3541

3642
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');
99105
/* eslint import/no-extraneous-dependencies: ["error", {"bundledDependencies": false}] */
100106
import foo from '"@generated/foo"';
101107
var foo = require('"@generated/foo"');
108+
109+
/* eslint import/no-extraneous-dependencies: ["error", {"includeInternal": true}] */
110+
import foo from './foo';
111+
var foo = require('./foo');
102112
```
103113

104114

src/rules/no-extraneous-dependencies.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,12 @@ function reportIfMissing(context, deps, depsOptions, node, name) {
183183
return;
184184
}
185185

186-
if (importType(name, context) !== 'external') {
186+
const typeOfImport = importType(name, context);
187+
188+
if (
189+
typeOfImport !== 'external'
190+
&& (typeOfImport !== 'internal' || !depsOptions.verifyInternalDeps)
191+
) {
187192
return;
188193
}
189194

@@ -261,6 +266,7 @@ module.exports = {
261266
'peerDependencies': { 'type': ['boolean', 'array'] },
262267
'bundledDependencies': { 'type': ['boolean', 'array'] },
263268
'packageDir': { 'type': ['string', 'array'] },
269+
'includeInternal': { 'type': ['boolean'] },
264270
},
265271
'additionalProperties': false,
266272
},
@@ -277,6 +283,7 @@ module.exports = {
277283
allowOptDeps: testConfig(options.optionalDependencies, filename) !== false,
278284
allowPeerDeps: testConfig(options.peerDependencies, filename) !== false,
279285
allowBundledDeps: testConfig(options.bundledDependencies, filename) !== false,
286+
verifyInternalDeps: !!options.includeInternal,
280287
};
281288

282289
return moduleVisitor((source, node) => {

tests/src/rules/no-extraneous-dependencies.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,18 @@ ruleTester.run('no-extraneous-dependencies', rule, {
392392
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`,
393393
}],
394394
}),
395+
396+
test({
397+
code: 'import "not-a-dependency"',
398+
settings: {
399+
'import/resolver': { node: { paths: [ path.join(__dirname, '../../files') ] } },
400+
'import/internal-regex': '^not-a-dependency.*',
401+
},
402+
options: [{ includeInternal: true }],
403+
errors: [{
404+
message: '\'not-a-dependency\' should be listed in the project\'s dependencies. Run \'npm i -S not-a-dependency\' to add it',
405+
}],
406+
}),
395407
],
396408
});
397409

0 commit comments

Comments
 (0)