Skip to content
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

importas: allow multiple empty aliases #5222

Merged
merged 3 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 9 additions & 5 deletions pkg/golinters/importas/importas.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,15 @@ 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, "$") {
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
// Ignore duplication for empty aliases.
if a.Alias != "" {
// skip the duplication check when the alias is a regular expression replacement pattern (ie. contains `$`).
v, ok := uniqAliases[a.Alias]
if ok && !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 {
ldez marked this conversation as resolved.
Show resolved Hide resolved
uniqAliases[a.Alias] = a
}
}

err := analyzer.Flags.Set("alias", fmt.Sprintf("%s:%s", a.Pkg, a.Alias))
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:
ldez marked this conversation as resolved.
Show resolved Hide resolved
- pkg: fmt
alias: ''
- pkg: os
alias: ''
- pkg: math
alias: ''
Loading