Eslint-plugin-import: Rule: max-imports

Created on 12 Aug 2016  路  8Comments  路  Source: benmosher/eslint-plugin-import

Curious what you would think about a max-imports rule. Usually if a module has, say, more than 10 imports or so, that is code smell and the module should be split up into smaller pieces.

Would you accept such a rule as a PR, or should such a rule live somewhere separately? Or does it already exist and I'm not aware? :)

rule proposal semver-minor style-guide

All 8 comments

Can be an interesting rule.

I'm guessing this would count both imports and requires. Would you count the number of modules, or functions imported. Like if you import the default and 2 named functions of a module, does that count as 1 or 3? I'd say its better to count it as 1.

I'd be up for this.

I agree with @jfmengels that it should count imported modules, not names. (though I could imagine having a parameter that limits names imported per-module, which would coerce you to import a namespace instead if you need more than X...)

name options:

  • import/max
  • import/limit

(something longer is fine, but I want to avoid restating import right of the /)

Great! Yes agreed a shorter name probably makes more sense. I personally like import/max, I'll go with that for now. Will try and get a PR ready today or tomorrow, appreciate any help. Thanks!

I'd go for import/max-dependencies or import/max-imports or something, as max could be applied to many things (like max number of named imports for instance).

@jfmengels but wouldn't that be under the purview of this rule, though?

To me, this should only limit the number of modules/dependencies your file uses, not how you import it. If you want to use destructuring to use 10 lodash methods, that should still be counted as 1 dependency

PR #489 ready for review.

Was this page helpful?
0 / 5 - 0 ratings