Skip to content

Commit

Permalink
Merge dc6cedc into 5fe14e3
Browse files Browse the repository at this point in the history
  • Loading branch information
rfermann committed Aug 15, 2020
2 parents 5fe14e3 + dc6cedc commit dba1651
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/rules/max-dependencies.js
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
64 changes: 63 additions & 1 deletion tests/src/rules/max-dependencies.js
@@ -1,4 +1,4 @@
import { test } from '../utils'
import { test, getTSParsers } from '../utils'

import { RuleTester } from 'eslint'

Expand All @@ -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,58 @@ 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.',
],
}),
],
})

context('TypeScript', function() {
getTSParsers().forEach((parser) => {
ruleTester.run('max-dependencies', rule, {
valid: [
test({
code: 'import type { x } from \'./foo\'; import { y } from \'./bar\';',
parser: parser,
options: [{
max: 1,
ignoreTypeImports: true,
}],
}),
],
invalid: [
test({
code: 'import type { x } from \'./foo\'; import type { y } from \'./bar\'',
parser: parser,
options: [{
max: 1,
}],
errors: [
'Maximum number of dependencies (1) exceeded.',
],
}),

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

0 comments on commit dba1651

Please sign in to comment.