Skip to content

Commit b772ad0

Browse files
rfermannljharb
authored andcommitted
[New] max-dependencies: add option ignoreTypeImports
1 parent bba59c4 commit b772ad0

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1010
- [`no-dynamic-require`]: add option `esmodule` ([#1223], thanks [@vikr01])
1111
- [`named`]: add `commonjs` option ([#1222], thanks [@vikr01])
1212
- [`no-namespace`]: Add `ignore` option ([#2112], thanks [@aberezkin])
13+
- [`max-dependencies`]: add option `ignoreTypeImports` ([#1847], thanks [@rfermann])
1314

1415
### Fixed
1516
- [`no-duplicates`]: ensure autofix avoids excessive newlines ([#2028], thanks [@ertrzyiks])
@@ -853,6 +854,7 @@ for info on changes for earlier releases.
853854
[#1878]: https://github.com/benmosher/eslint-plugin-import/pull/1878
854855
[#1860]: https://github.com/benmosher/eslint-plugin-import/pull/1860
855856
[#1848]: https://github.com/benmosher/eslint-plugin-import/pull/1848
857+
[#1847]: https://github.com/benmosher/eslint-plugin-import/pull/1847
856858
[#1846]: https://github.com/benmosher/eslint-plugin-import/pull/1846
857859
[#1836]: https://github.com/benmosher/eslint-plugin-import/pull/1836
858860
[#1835]: https://github.com/benmosher/eslint-plugin-import/pull/1835

src/rules/max-dependencies.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ import moduleVisitor from 'eslint-module-utils/moduleVisitor';
22
import docsUrl from '../docsUrl';
33

44
const DEFAULT_MAX = 10;
5+
const DEFAULT_IGNORE_TYPE_IMPORTS = false;
6+
const TYPE_IMPORT = 'type';
57

68
const countDependencies = (dependencies, lastNode, context) => {
79
const { max } = context.options[0] || { max: DEFAULT_MAX };
810

911
if (dependencies.size > max) {
10-
context.report(
11-
lastNode,
12-
`Maximum number of dependencies (${max}) exceeded.`
13-
);
12+
context.report(lastNode, `Maximum number of dependencies (${max}) exceeded.`);
1413
}
1514
};
1615

@@ -26,22 +25,29 @@ module.exports = {
2625
'type': 'object',
2726
'properties': {
2827
'max': { 'type': 'number' },
28+
'ignoreTypeImports': { 'type': 'boolean' },
2929
},
3030
'additionalProperties': false,
3131
},
3232
],
3333
},
3434

3535
create: context => {
36+
const {
37+
ignoreTypeImports = DEFAULT_IGNORE_TYPE_IMPORTS,
38+
} = context.options[0] || {};
39+
3640
const dependencies = new Set(); // keep track of dependencies
3741
let lastNode; // keep track of the last node to report on
3842

3943
return Object.assign({
4044
'Program:exit': function () {
4145
countDependencies(dependencies, lastNode, context);
4246
},
43-
}, moduleVisitor((source) => {
44-
dependencies.add(source.value);
47+
}, moduleVisitor((source, { importKind }) => {
48+
if (importKind !== TYPE_IMPORT || !ignoreTypeImports) {
49+
dependencies.add(source.value);
50+
}
4551
lastNode = source;
4652
}, { commonjs: true }));
4753
},

tests/src/rules/max-dependencies.js

+54-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { test } from '../utils';
1+
import { test, getTSParsers } from '../utils';
22

33
import { RuleTester } from 'eslint';
44

@@ -74,5 +74,58 @@ ruleTester.run('max-dependencies', rule, {
7474
'Maximum number of dependencies (1) exceeded.',
7575
],
7676
}),
77+
78+
test({
79+
code: 'import type { x } from \'./foo\'; import type { y } from \'./bar\'; import type { z } from \'./baz\'',
80+
parser: require.resolve('babel-eslint'),
81+
options: [{
82+
max: 2,
83+
ignoreTypeImports: false,
84+
}],
85+
errors: [
86+
'Maximum number of dependencies (2) exceeded.',
87+
],
88+
}),
7789
],
7890
});
91+
92+
context('TypeScript', function() {
93+
getTSParsers().forEach((parser) => {
94+
ruleTester.run('max-dependencies', rule, {
95+
valid: [
96+
test({
97+
code: 'import type { x } from \'./foo\'; import { y } from \'./bar\';',
98+
parser: parser,
99+
options: [{
100+
max: 1,
101+
ignoreTypeImports: true,
102+
}],
103+
}),
104+
],
105+
invalid: [
106+
test({
107+
code: 'import type { x } from \'./foo\'; import type { y } from \'./bar\'',
108+
parser: parser,
109+
options: [{
110+
max: 1,
111+
}],
112+
errors: [
113+
'Maximum number of dependencies (1) exceeded.',
114+
],
115+
}),
116+
117+
test({
118+
code: 'import type { x } from \'./foo\'; import type { y } from \'./bar\'; import type { z } from \'./baz\'',
119+
parser: parser,
120+
options: [{
121+
max: 2,
122+
ignoreTypeImports: false,
123+
}],
124+
errors: [
125+
'Maximum number of dependencies (2) exceeded.',
126+
],
127+
}),
128+
],
129+
});
130+
});
131+
});

0 commit comments

Comments
 (0)