Skip to content

Commit

Permalink
doc
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolaydubina committed Jun 6, 2023
1 parent 0536fa3 commit f6a8e96
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Example
-: "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
-: "k8s.io/kubernetes/plugin/pkg/admission/podtolerationrestriction/apis
```

## Alternatives
Expand Down
23 changes: 15 additions & 8 deletions analysis/consistentimports/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"golang.org/x/tools/go/analysis/passes/inspect"
)

// Analyzer is inspired by analysis facts example: https://cs.opensource.google/go/x/tools/+/refs/tags/v0.9.3:go/analysis/passes/pkgfact/pkgfact.go
var Analyzer = &analysis.Analyzer{
Name: "consistentimports",
Doc: "detect inconsistent import aliases, reports import paths and aliases count",
Expand All @@ -31,10 +32,10 @@ func run(pass *analysis.Pass) (interface{}, error) {

for _, file := range pass.Files {
for _, spec := range file.Imports {
// upstream packages
var fact pathAliasCountsFact
if pass.ImportPackageFact(imported(pass.TypesInfo, spec), &fact) {
for k, v := range fact {
// merge stats from upstream packages
var upfact pathAliasCountsFact
if pass.ImportPackageFact(imported(pass.TypesInfo, spec), &upfact) {
for k, v := range upfact {
pathAliasCount[k] += v
}
}
Expand All @@ -48,7 +49,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
alias = spec.Name.Name
}

if alias == "" {
if alias == "" || alias == "_" {
continue
}

Expand All @@ -59,10 +60,18 @@ func run(pass *analysis.Pass) (interface{}, error) {
}
}

reportPathAliasCount(pass, pathAliasCount)

return nil, nil
}

func reportPathAliasCount(pass *analysis.Pass, pathAliasCount pathAliasCountsFact) {
var pathAliasAgg map[string][]aliasCount = map[string][]aliasCount{}
for k, v := range pathAliasCount {
pathAliasAgg[k[0]] = append(pathAliasAgg[k[0]], aliasCount{Alias: k[1], Count: v})
}

// sort aliases by count for each path from highest to lowest
for k := range pathAliasAgg {
sort.Slice(pathAliasAgg[k], func(i, j int) bool {
ci := pathAliasAgg[k][i].Count
Expand All @@ -74,7 +83,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
})
}

// sort paths by aliases count
// sort paths by aliases count from highest to lowest
pathsByCount := make([]string, 0, len(pathAliasAgg))
for k := range pathAliasAgg {
pathsByCount = append(pathsByCount, k)
Expand All @@ -96,8 +105,6 @@ func run(pass *analysis.Pass) (interface{}, error) {
}
pass.Reportf(token.NoPos, `%s %s`, path, printPathAliasCount(aliases))
}

return nil, nil
}

type aliasCount struct {
Expand Down

0 comments on commit f6a8e96

Please sign in to comment.