Skip to content
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

A linter which requires an import alias for all imports where the last part of the path does not match the package name #2401

Closed
mitar opened this issue Dec 2, 2021 · 2 comments
Labels
enhancement New feature or improvement

Comments

@mitar
Copy link
Contributor

mitar commented Dec 2, 2021

Your feature request related to a problem? Please describe.

I have code with the following imports:

import (
	"github.com/deckarep/golang-set"
	"github.com/go-git/go-git/v5"
	"github.com/whilp/git-urls"
	"github.com/xanzy/go-gitlab"
	"github.com/xmidt-org/gokeepachangelog"
)

Without reading their code or documentation it is not really possible to know how to use it and which symbol later in the code relates to which package. This is why I use the following:

import (
	mapset "github.com/deckarep/golang-set"
	git "github.com/go-git/go-git/v5"
	giturls "github.com/whilp/git-urls"
	gitlab "github.com/xanzy/go-gitlab"
	changelog "github.com/xmidt-org/gokeepachangelog"
)

Those aliases are unnecessary, because those imports already provide packages under those names, but they make code much cleaner and easier to read. If I search for "mapset" in the code I can see where it was imported.

Describe the solution you'd like.

I would suggest to have a linter which would check all imports and require an import alias when the last part of the path does not match the package name imported.

Under a configuration flag I would also suggest that one could enable strict mode: where the alias has to match the package name. For an exemption (where such mode would introduce conflicts), one could disable linting on that line, or allow it through another setting.

Describe alternatives you've considered.

I searched around but I could not find an existing solutions. There is importas but it requires one to manually specify aliases which is not what I would want here.

Additional context.

There is some discussion about cognitive load because of this and ways to address it through changes in Go language itself. But I think a linter can already help a lot.

@mitar mitar added the enhancement New feature or improvement label Dec 2, 2021
@boring-cyborg
Copy link

boring-cyborg bot commented Dec 2, 2021

Hey, thank you for opening your first Issue ! 🙂 If you would like to contribute we have a guide for contributors.

@mitar
Copy link
Contributor Author

mitar commented Dec 14, 2021

It looks like goimport already fixes that, but has some exceptions, so you get out:

import (
	mapset "github.com/deckarep/golang-set"
	"github.com/go-git/go-git/v5"
	giturls "github.com/whilp/git-urls"
	"github.com/xanzy/go-gitlab"
	changelog "github.com/xmidt-org/gokeepachangelog"
)

I think this is good enough. And then you can use goimports linting rule to make sure goimports has been run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or improvement
Projects
None yet
Development

No branches or pull requests

1 participant