Skip to content

Commit

Permalink
[max-dependencies]: add option 'ignoreTypeImports'
Browse files Browse the repository at this point in the history
  • Loading branch information
rfermann committed Jul 6, 2020
1 parent 843055c commit 8195aa3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/rules/max-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import isStaticRequire from '../core/staticRequire'
import docsUrl from '../docsUrl'

const DEFAULT_MAX = 10
const DEFAULT_IGNORE_TYPE_IMPORTS = false
const TYPE_IMPORT = 'type'

const countDependencies = (dependencies, lastNode, context) => {
const {max} = context.options[0] || { max: DEFAULT_MAX }
Expand All @@ -26,6 +28,7 @@ module.exports = {
'type': 'object',
'properties': {
'max': { 'type': 'number' },
'ignoreTypeImports': { 'type': 'boolean' },
},
'additionalProperties': false,
},
Expand All @@ -36,9 +39,17 @@ module.exports = {
const dependencies = new Set() // keep track of dependencies
let lastNode // keep track of the last node to report on

const {ignoreTypeImports} = context.options[0] || { ignoreTypeImports: DEFAULT_IGNORE_TYPE_IMPORTS }

return {
ImportDeclaration(node) {
dependencies.add(node.source.value)
if (node.importKind === TYPE_IMPORT) {
if (!ignoreTypeImports) {
dependencies.add(node.source.value)
}
} else {
dependencies.add(node.source.value)
}
lastNode = node.source
},

Expand Down
21 changes: 21 additions & 0 deletions tests/src/rules/max-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ ruleTester.run('max-dependencies', rule, {
}),

test({ code: 'import {x, y, z} from "./foo"'}),

test({
code: 'import type { x } from \'./foo\'; import { y } from \'./bar\';',
parser: require.resolve('babel-eslint'),
options: [{
max: 1,
ignoreTypeImports: true,
}],
}),
],
invalid: [
test({
Expand Down Expand Up @@ -74,5 +83,17 @@ ruleTester.run('max-dependencies', rule, {
'Maximum number of dependencies (1) exceeded.',
],
}),

test({
code: 'import type { x } from \'./foo\'; import type { y } from \'./bar\'; import type { z } from \'./baz\'',
parser: require.resolve('babel-eslint'),
options: [{
max: 2,
ignoreTypeImports: false,
}],
errors: [
'Maximum number of dependencies (2) exceeded.',
],
}),
],
})

0 comments on commit 8195aa3

Please sign in to comment.