Skip to content

Commit

Permalink
add sync.Map
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodge0 committed Dec 6, 2021
1 parent b408292 commit 517c421
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"strconv"
"strings"
"sync"

"golang.org/x/tools/go/analysis"
)
Expand All @@ -17,7 +18,7 @@ type aliaspath struct {
position token.Position
}

var imports = map[string]aliaspath{}
var imports sync.Map

//nolint:gochecknoglobals
var flagSet flag.FlagSet
Expand Down Expand Up @@ -79,13 +80,18 @@ func run(pass *analysis.Pass) (interface{}, error) {
}

if alias != "" {
val, ok := imports[path]
if ok {
if val.alias != alias {
pass.Reportf(f.Pos(), "package %q have alias %q, conflict with %q in %q", path, alias, val.alias, val.position)
value, exist := imports.Load(path)
if exist {
val, ok := value.(aliaspath)
if ok {
if val.alias != alias {
pass.Reportf(f.Pos(), "package %q have alias %q, conflict with %q in %q", path, alias, val.alias, val.position)
}
} else {
pass.Reportf(f.Pos(), "value.(aliaspath) failed, value : ", value)
}
} else {
imports[path] = aliaspath{alias: alias, position: pass.Fset.Position(f.Pos())}
imports.Store(path, aliaspath{alias: alias, position: pass.Fset.Position(f.Pos())})
}
}

Expand Down

0 comments on commit 517c421

Please sign in to comment.