Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nikolaydubina/consistentimports
Detect inconsistent import aliases
https://github.com/nikolaydubina/consistentimports
analyzer linter static-analysis
Last synced: 16 days ago
JSON representation
Detect inconsistent import aliases
- Host: GitHub
- URL: https://github.com/nikolaydubina/consistentimports
- Owner: nikolaydubina
- License: mit
- Created: 2023-06-06T13:23:28.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-10-01T06:34:39.000Z (about 1 month ago)
- Last Synced: 2024-10-09T08:23:04.899Z (about 1 month ago)
- Topics: analyzer, linter, static-analysis
- Language: Go
- Homepage:
- Size: 34.2 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
- Security: SECURITY.md
Awesome Lists containing this project
README
## consistentimports: detect inconsistent import aliases
[![go-recipes](https://raw.githubusercontent.com/nikolaydubina/go-recipes/main/badge.svg?raw=true)](https://github.com/nikolaydubina/go-recipes)
[![Go Report Card](https://goreportcard.com/badge/github.com/nikolaydubina/consistentimports)](https://goreportcard.com/report/github.com/nikolaydubina/consistentimports)
[![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/nikolaydubina/consistentimports/badge)](https://securityscorecards.dev/viewer/?uri=github.com/nikolaydubina/consistentimports)
[![codecov](https://codecov.io/gh/nikolaydubina/consistentimports/graph/badge.svg?token=UvuLC6OGaP)](https://codecov.io/gh/nikolaydubina/consistentimports)Report import paths and aliases count when same import path has multiple aliases.
```bash
go install github.com/nikolaydubina/consistentimports@latest
``````bash
consistentimports ./...
```In example bellow same same import has two alises `passInspect` `!=` `passinspect`. This linter detects that.
```go
// apple.go
import (
passInspect "golang.org/x/tools/go/analysis/passes/inspect"
)// pear.go
import (
passinspect "golang.org/x/tools/go/analysis/passes/inspect"
)
```Example
```
-: "k8s.io/utils/net" netutils:4 netutil:1
-: "k8s.io/client-go/listers/core/v1" corelisters:1 listersv1:1 v1listers:1
-: "k8s.io/client-go/informers/core/v1" coreinformers:1 informers:1
-: "k8s.io/api/rbac/v1" rbacv1:4 v1:2
-: "k8s.io/apimachinery/pkg/runtime" runtime:3 kruntime:1
-: "k8s.io/api/imagepolicy/v1alpha1" imagepolicyv1alpha1:1 v1alpha1:1
```## Alternatives
* https://github.com/julz/importas provides only fixed whitelist list or regex
* https://staticcheck.io/docs/checks/ has no such linter## Appendix A: packages in same module
As of `2023-06-08`, there seem to be no way to in `golang.org/x/tools/go/analysis` to:
* get list of packages in module
* check if two packages are in same module
* what is the current moduleHowever, for practical use we really need to narrow down to current module, or else linter will keep checking recursively way outside of current module.
One simple heuristic is to check that package and the package it imports match long enough prefix.
## Appendix B: integration to linter aggregators
`staticcheck`
- `2023-06-08` proposal raised but rejected: https://github.com/dominikh/go-tools/issues/1413