Skip to content

Commit

Permalink
importas: detect duplicate alias or package in the configuration (gol…
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored and SeigeC committed Apr 4, 2023
1 parent 9076872 commit f4ab40b
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/golinters/importas.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,26 @@ func NewImportAs(settings *config.ImportAsSettings) *goanalysis.Linter {
lintCtx.Log.Errorf("failed to parse configuration: %v", err)
}

uniqPackages := make(map[string]config.ImportAsAlias)
uniqAliases := make(map[string]config.ImportAsAlias)
for _, a := range settings.Alias {
if a.Pkg == "" {
lintCtx.Log.Errorf("invalid configuration, empty package: pkg=%s alias=%s", a.Pkg, a.Alias)
continue
}

if v, ok := uniqPackages[a.Pkg]; ok {
lintCtx.Log.Errorf("invalid configuration, multiple aliases for the same package: pkg=%s aliases=[%s,%s]", a.Pkg, a.Alias, v.Alias)
} else {
uniqPackages[a.Pkg] = a
}

if v, ok := uniqAliases[a.Alias]; ok {
lintCtx.Log.Errorf("invalid configuration, multiple packages with the same alias: pkg=%s packages=[%s,%s]", a.Alias, a.Pkg, v.Pkg)
} else {
uniqAliases[a.Alias] = a
}

err := analyzer.Flags.Set("alias", fmt.Sprintf("%s:%s", a.Pkg, a.Alias))
if err != nil {
lintCtx.Log.Errorf("failed to parse configuration: %v", err)
Expand Down

0 comments on commit f4ab40b

Please sign in to comment.