Skip to content

Commit

Permalink
importas: new option no-extra-aliases (golangci#2494)
Browse files Browse the repository at this point in the history
  • Loading branch information
butuzov authored and SeigeC committed Apr 4, 2023
1 parent d07d8e3 commit 3615426
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
4 changes: 3 additions & 1 deletion .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,10 @@ linters-settings:
max-decl-chars: 30

importas:
# if set to `true`, force to use alias.
# Do not allow unaliased imports of aliased packages.
no-unaliased: true
# Do not allow non-required aliases.
no-extra-aliases: true
# List of aliases
alias:
# using `servingv1` alias for `knative.dev/serving/pkg/apis/serving/v1` package
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ require (
github.com/jgautheron/goconst v1.5.1
github.com/jingyugao/rowserrcheck v1.1.1
github.com/jirfag/go-printf-func-name v0.0.0-20200119135958-7558a9eaa5af
github.com/julz/importas v0.0.0-20210419104244-841f0c0fe66d
github.com/julz/importas v0.1.0
github.com/kisielk/errcheck v1.6.0
github.com/kulti/thelper v0.5.0
github.com/kunwardeep/paralleltest v1.0.3
Expand Down
4 changes: 4 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,9 @@ type IfshortSettings struct {
}

type ImportAsSettings struct {
Alias []ImportAsAlias
NoUnaliased bool `mapstructure:"no-unaliased"`
Alias []ImportAsAlias
NoUnaliased bool `mapstructure:"no-unaliased"`
NoExtraAliases bool `mapstructure:"no-extra-aliases"`
}

type ImportAsAlias struct {
Expand Down
7 changes: 5 additions & 2 deletions pkg/golinters/importas.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ func NewImportAs(settings *config.ImportAsSettings) *goanalysis.Linter {
lintCtx.Log.Infof("importas settings found, but no aliases listed. List aliases under alias: key.") // nolint: misspell
}

err := analyzer.Flags.Set("no-unaliased", strconv.FormatBool(settings.NoUnaliased))
if err != nil {
if err := analyzer.Flags.Set("no-unaliased", strconv.FormatBool(settings.NoUnaliased)); err != nil {
lintCtx.Log.Errorf("failed to parse configuration: %v", err)
}

if err := analyzer.Flags.Set("no-extra-aliases", strconv.FormatBool(settings.NoUnaliased)); err != nil {
lintCtx.Log.Errorf("failed to parse configuration: %v", err)
}

Expand Down

0 comments on commit 3615426

Please sign in to comment.