Skip to content

importas: allow multiple empty aliases #5222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: ''
Loading