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: new option no-extra-aliases #2494

Merged
merged 3 commits into from
Jan 19, 2022
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
4 changes: 3 additions & 1 deletion .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,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.4.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 @@ -352,8 +352,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