Skip to content

Commit b743a65

Browse files
rfermannljharb
authored andcommittedJul 6, 2020
[New] max-dependencies: add option ignoreTypeImports
1 parent bba59c4 commit b743a65

File tree

3 files changed

+70
-7
lines changed

3 files changed

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

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

33
import { RuleTester } from 'eslint';
4+
import eslintPkg from 'eslint/package.json';
5+
import semver from 'semver';
46

57
const ruleTester = new RuleTester();
68
const rule = require('rules/max-dependencies');
@@ -74,5 +76,58 @@ ruleTester.run('max-dependencies', rule, {
7476
'Maximum number of dependencies (1) exceeded.',
7577
],
7678
}),
79+
80+
test({
81+
code: 'import type { x } from \'./foo\'; import type { y } from \'./bar\'; import type { z } from \'./baz\'',
82+
parser: require.resolve('babel-eslint'),
83+
options: [{
84+
max: 2,
85+
ignoreTypeImports: false,
86+
}],
87+
errors: [
88+
'Maximum number of dependencies (2) exceeded.',
89+
],
90+
}),
7791
],
7892
});
93+
94+
context('TypeScript', { skip: semver.satisfies(eslintPkg.version, '>5.0.0') }, () => {
95+
getTSParsers().forEach((parser) => {
96+
ruleTester.run(`max-dependencies (${parser.replace(process.cwd(), '.')})`, rule, {
97+
valid: [
98+
test({
99+
code: 'import type { x } from \'./foo\'; import { y } from \'./bar\';',
100+
parser: parser,
101+
options: [{
102+
max: 1,
103+
ignoreTypeImports: true,
104+
}],
105+
}),
106+
],
107+
invalid: [
108+
test({
109+
code: 'import type { x } from \'./foo\'; import type { y } from \'./bar\'',
110+
parser: parser,
111+
options: [{
112+
max: 1,
113+
}],
114+
errors: [
115+
'Maximum number of dependencies (1) exceeded.',
116+
],
117+
}),
118+
119+
test({
120+
code: 'import type { x } from \'./foo\'; import type { y } from \'./bar\'; import type { z } from \'./baz\'',
121+
parser: parser,
122+
options: [{
123+
max: 2,
124+
ignoreTypeImports: false,
125+
}],
126+
errors: [
127+
'Maximum number of dependencies (2) exceeded.',
128+
],
129+
}),
130+
],
131+
});
132+
});
133+
});

0 commit comments

Comments
 (0)
Please sign in to comment.