Skip to content

Commit f416d8d

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

File tree

3 files changed

+77
-7
lines changed

3 files changed

+77
-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

+63-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

@@ -22,6 +22,15 @@ ruleTester.run('max-dependencies', rule, {
2222
}),
2323

2424
test({ code: 'import {x, y, z} from "./foo"' }),
25+
26+
test({
27+
code: 'import type { x } from \'./foo\'; import { y } from \'./bar\';',
28+
parser: require.resolve('babel-eslint'),
29+
options: [{
30+
max: 1,
31+
ignoreTypeImports: true,
32+
}],
33+
}),
2534
],
2635
invalid: [
2736
test({
@@ -74,5 +83,58 @@ ruleTester.run('max-dependencies', rule, {
7483
'Maximum number of dependencies (1) exceeded.',
7584
],
7685
}),
86+
87+
test({
88+
code: 'import type { x } from \'./foo\'; import type { y } from \'./bar\'; import type { z } from \'./baz\'',
89+
parser: require.resolve('babel-eslint'),
90+
options: [{
91+
max: 2,
92+
ignoreTypeImports: false,
93+
}],
94+
errors: [
95+
'Maximum number of dependencies (2) exceeded.',
96+
],
97+
}),
7798
],
7899
});
100+
101+
context('TypeScript', function() {
102+
getTSParsers().forEach((parser) => {
103+
ruleTester.run('max-dependencies', rule, {
104+
valid: [
105+
test({
106+
code: 'import type { x } from \'./foo\'; import { y } from \'./bar\';',
107+
parser: parser,
108+
options: [{
109+
max: 1,
110+
ignoreTypeImports: true,
111+
}],
112+
}),
113+
],
114+
invalid: [
115+
test({
116+
code: 'import type { x } from \'./foo\'; import type { y } from \'./bar\'',
117+
parser: parser,
118+
options: [{
119+
max: 1,
120+
}],
121+
errors: [
122+
'Maximum number of dependencies (1) exceeded.',
123+
],
124+
}),
125+
126+
test({
127+
code: 'import type { x } from \'./foo\'; import type { y } from \'./bar\'; import type { z } from \'./baz\'',
128+
parser: parser,
129+
options: [{
130+
max: 2,
131+
ignoreTypeImports: false,
132+
}],
133+
errors: [
134+
'Maximum number of dependencies (2) exceeded.',
135+
],
136+
}),
137+
],
138+
});
139+
});
140+
});

0 commit comments

Comments
 (0)