Skip to content

Commit

Permalink
[New] max-dependencies: add option ignoreTypeImports
Browse files Browse the repository at this point in the history
  • Loading branch information
rfermann authored and ljharb committed Jul 6, 2020
1 parent bba59c4 commit f416d8d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- [`no-dynamic-require`]: add option `esmodule` ([#1223], thanks [@vikr01])
- [`named`]: add `commonjs` option ([#1222], thanks [@vikr01])
- [`no-namespace`]: Add `ignore` option ([#2112], thanks [@aberezkin])
- [`max-dependencies`]: add option `ignoreTypeImports` ([#1847], thanks [@rfermann])

### Fixed
- [`no-duplicates`]: ensure autofix avoids excessive newlines ([#2028], thanks [@ertrzyiks])
Expand Down Expand Up @@ -853,6 +854,7 @@ for info on changes for earlier releases.
[#1878]: https://github.com/benmosher/eslint-plugin-import/pull/1878
[#1860]: https://github.com/benmosher/eslint-plugin-import/pull/1860
[#1848]: https://github.com/benmosher/eslint-plugin-import/pull/1848
[#1847]: https://github.com/benmosher/eslint-plugin-import/pull/1847
[#1846]: https://github.com/benmosher/eslint-plugin-import/pull/1846
[#1836]: https://github.com/benmosher/eslint-plugin-import/pull/1836
[#1835]: https://github.com/benmosher/eslint-plugin-import/pull/1835
Expand Down
18 changes: 12 additions & 6 deletions src/rules/max-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import moduleVisitor from 'eslint-module-utils/moduleVisitor';
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 };

if (dependencies.size > max) {
context.report(
lastNode,
`Maximum number of dependencies (${max}) exceeded.`
);
context.report(lastNode, `Maximum number of dependencies (${max}) exceeded.`);
}
};

Expand All @@ -26,22 +25,29 @@ module.exports = {
'type': 'object',
'properties': {
'max': { 'type': 'number' },
'ignoreTypeImports': { 'type': 'boolean' },
},
'additionalProperties': false,
},
],
},

create: context => {
const {
ignoreTypeImports = DEFAULT_IGNORE_TYPE_IMPORTS,
} = context.options[0] || {};

const dependencies = new Set(); // keep track of dependencies
let lastNode; // keep track of the last node to report on

return Object.assign({
'Program:exit': function () {
countDependencies(dependencies, lastNode, context);
},
}, moduleVisitor((source) => {
dependencies.add(source.value);
}, moduleVisitor((source, { importKind }) => {
if (importKind !== TYPE_IMPORT || !ignoreTypeImports) {
dependencies.add(source.value);
}
lastNode = source;
}, { commonjs: true }));
},
Expand Down
64 changes: 63 additions & 1 deletion tests/src/rules/max-dependencies.js
Original file line number Diff line number Diff line change
@@ -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 f416d8d

Please sign in to comment.