-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/max dependencies #489
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# max-dependencies | ||
|
||
Forbid modules to have too many dependencies (`import` or `require` statements). | ||
|
||
This is a useful rule because a module with too many dependencies is a code smell, and usually indicates the module is doing too much and/or should be broken up into smaller modules. | ||
|
||
Importing multiple named exports from a single module will only count once (e.g. `import {x, y, z} from './foo'` will only count as a single dependency). | ||
|
||
### Options | ||
|
||
This rule takes the following option: | ||
|
||
`max`: The maximum number of dependencies allowed. Anything over will trigger the rule. **Default is 10** if the rule is enabled and no `max` is specified. | ||
|
||
You can set the option like this: | ||
|
||
```js | ||
"import/max-dependencies": ["error", {"max": 10}] | ||
``` | ||
|
||
|
||
## Example | ||
|
||
Given a max value of `{"max": 2}`: | ||
|
||
### Fail | ||
|
||
```js | ||
import a from './a'; // 1 | ||
const b = require('./b'); // 2 | ||
import c from './c'; // 3 - exceeds max! | ||
``` | ||
|
||
### Pass | ||
|
||
```js | ||
import a from './a'; // 1 | ||
const anotherA = require('./a'); // still 1 | ||
import {x, y, z} from './foo'; // 2 | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you don't care how many dependencies a module has. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import Set from 'es6-set' | ||
import isStaticRequire from '../core/staticRequire' | ||
|
||
const DEFAULT_MAX = 10 | ||
|
||
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.` | ||
) | ||
} | ||
} | ||
|
||
module.exports = context => { | ||
const dependencies = new Set() // keep track of dependencies | ||
let lastNode // keep track of the last node to report on | ||
|
||
return { | ||
ImportDeclaration(node) { | ||
dependencies.add(node.source.value) | ||
lastNode = node.source | ||
}, | ||
|
||
CallExpression(node) { | ||
if (isStaticRequire(node)) { | ||
const [ requirePath ] = node.arguments | ||
dependencies.add(requirePath.value) | ||
lastNode = node | ||
} | ||
}, | ||
|
||
'Program:exit': function () { | ||
countDependencies(dependencies, lastNode, context) | ||
}, | ||
} | ||
} | ||
|
||
module.exports.schema = [ | ||
{ | ||
'type': 'object', | ||
'properties': { | ||
'max': { 'type': 'number' }, | ||
}, | ||
'additionalProperties': false, | ||
}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { test } from '../utils' | ||
|
||
import { RuleTester } from 'eslint' | ||
|
||
const ruleTester = new RuleTester() | ||
, rule = require('rules/max-dependencies') | ||
|
||
ruleTester.run('max-dependencies', rule, { | ||
valid: [ | ||
test({ code: 'import "./foo.js"' }), | ||
|
||
test({ code: 'import "./foo.js"; import "./bar.js";', | ||
options: [{ | ||
max: 2, | ||
}], | ||
}), | ||
|
||
test({ code: 'import "./foo.js"; import "./bar.js"; const a = require("./foo.js"); const b = require("./bar.js");', | ||
options: [{ | ||
max: 2, | ||
}], | ||
}), | ||
|
||
test({ code: 'import {x, y, z} from "./foo"'}), | ||
], | ||
invalid: [ | ||
test({ | ||
code: 'import { x } from \'./foo\'; import { y } from \'./foo\'; import {z} from \'./bar\';', | ||
options: [{ | ||
max: 1, | ||
}], | ||
errors: [ | ||
'Maximum number of dependencies (1) exceeded.', | ||
], | ||
}), | ||
|
||
test({ | ||
code: 'import { x } from \'./foo\'; import { y } from \'./bar\'; import { z } from \'./baz\';', | ||
options: [{ | ||
max: 2, | ||
}], | ||
errors: [ | ||
'Maximum number of dependencies (2) exceeded.', | ||
], | ||
}), | ||
|
||
test({ | ||
code: 'import { x } from \'./foo\'; require("./bar"); import { z } from \'./baz\';', | ||
options: [{ | ||
max: 2, | ||
}], | ||
errors: [ | ||
'Maximum number of dependencies (2) exceeded.', | ||
], | ||
}), | ||
|
||
test({ | ||
code: 'import { x } from \'./foo\'; import { z } from \'./foo\'; require("./bar"); const path = require("path");', | ||
options: [{ | ||
max: 2, | ||
}], | ||
errors: [ | ||
'Maximum number of dependencies (2) exceeded.', | ||
], | ||
}), | ||
|
||
test({ | ||
code: 'import type { x } from \'./foo\'; import type { y } from \'./bar\'', | ||
parser: 'babel-eslint', | ||
options: [{ | ||
max: 1, | ||
}], | ||
errors: [ | ||
'Maximum number of dependencies (1) exceeded.', | ||
], | ||
}), | ||
], | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not quite sure how defaults are handled. I wanted it so that if you enable the rule, but don't specify
{max}
option (is that possible?) it would at least default to a sensible 10.Not sure if that was the right way to go about it... thoughts?