Skip to content

Commit 8195aa3

Browse files
committed
[max-dependencies]: add option 'ignoreTypeImports'
1 parent 843055c commit 8195aa3

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/rules/max-dependencies.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import isStaticRequire from '../core/staticRequire'
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 }
@@ -26,6 +28,7 @@ module.exports = {
2628
'type': 'object',
2729
'properties': {
2830
'max': { 'type': 'number' },
31+
'ignoreTypeImports': { 'type': 'boolean' },
2932
},
3033
'additionalProperties': false,
3134
},
@@ -36,9 +39,17 @@ module.exports = {
3639
const dependencies = new Set() // keep track of dependencies
3740
let lastNode // keep track of the last node to report on
3841

42+
const {ignoreTypeImports} = context.options[0] || { ignoreTypeImports: DEFAULT_IGNORE_TYPE_IMPORTS }
43+
3944
return {
4045
ImportDeclaration(node) {
41-
dependencies.add(node.source.value)
46+
if (node.importKind === TYPE_IMPORT) {
47+
if (!ignoreTypeImports) {
48+
dependencies.add(node.source.value)
49+
}
50+
} else {
51+
dependencies.add(node.source.value)
52+
}
4253
lastNode = node.source
4354
},
4455

tests/src/rules/max-dependencies.js

+21
Original file line numberDiff line numberDiff line change
@@ -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,17 @@ 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
})

0 commit comments

Comments
 (0)