mgechev / revive

🔥 ~6x faster, stricter, configurable, extensible, and beautiful drop-in replacement for golint
https://revive.run
MIT License
4.73k stars 276 forks source link

Linter rule to check against import alias naming restrictions #880

Closed denisvmedia closed 1 year ago

denisvmedia commented 1 year ago

Is your feature request related to a problem? Please describe.

Go gives some recommendations on how packages should be named. This should also imply the rules for the aliases. I'd like to check that aliases for packages in our project don't include CAPS and underscores.

Describe the solution you'd like I have two options:

The first one is more universal, the second one is simpler and more precise. We can also combine both options but then it might bring confusion.

NB: in any case we'll skip _ import alias used for side effects.

Describe alternatives you've considered I checked importas linter and it didn't have this feature. I submitted a feature request there, but I'm not sure if it fits the idea of importas.

Additional context Example:

package foo

import (
    fooBAR "foo/bar" // MATCH /import alias should not include capital letters/
    bar_foo "bar/foo" // MATCH /import alias should not include underscores/
    _ "far/boo" // this is fine
)

OR

package foo

import (
    foo_BAR "foo/bar" // MATCH /import alias must much the regular expression: "^[a-z][a-z0-9]{0,}$"/
    _ "far/boo" // this is fine
)
chavacava commented 1 year ago

Hi @denisvmedia, thanks for the proposal. I think we could add the rule. By default, it only accepts ^[a-z]+$ or _ and optionally takes a regex as argument (that overwrites defaults) What do you think?

denisvmedia commented 1 year ago

Works for me. I'd just extend the default regex to ^[a-z][a-z0-9]{0,}$ (which by default will allow digits in the name - sometimes it can be useful when importing multiple versions of one and the same package).

If you agree, I can start working on adding such a rule.

chavacava commented 1 year ago

Go @denisvmedia !