Skip to content

Commit

Permalink
importas: allow multiple empty aliases (#5222)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Dec 13, 2024
1 parent dafd655 commit b2e24d6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pkg/golinters/importas/importas.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ func New(settings *config.ImportAsSettings) *goanalysis.Linter {
uniqPackages[a.Pkg] = a
}

// skip the duplication check when the alias is a regular expression replacement pattern (ie. contains `$`).
if v, ok := uniqAliases[a.Alias]; ok && !strings.Contains(a.Alias, "$") {
// Skips the duplication check when:
// - the alias is empty.
// - the alias is a regular expression replacement pattern (ie. contains `$`).
v, ok := uniqAliases[a.Alias]
if ok && a.Alias != "" && !strings.Contains(a.Alias, "$") {
lintCtx.Log.Errorf("invalid configuration, multiple packages with the same alias: alias=%s packages=[%s,%s]", a.Alias, a.Pkg, v.Pkg)
} else {
uniqAliases[a.Alias] = a
Expand Down
16 changes: 16 additions & 0 deletions pkg/golinters/importas/testdata/importas_several_empty_aliases.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//golangcitest:args -Eimportas
//golangcitest:config_path testdata/importas_several_empty_aliases.yml
//golangcitest:expected_exitcode 0
package testdata

import (
"fmt"
"math"
"os"
)

func _() {
fmt.Println("a")
fmt.Fprint(os.Stderr, "b")
println(math.MaxInt)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
linters-settings:
importas:
alias:
- pkg: fmt
alias: ''
- pkg: os
alias: ''
- pkg: math
alias: ''

0 comments on commit b2e24d6

Please sign in to comment.