From 59c6d296ce034bc12c6bbf7dfe478362bd9bffb9 Mon Sep 17 00:00:00 2001 From: Oleg Butuzov Date: Wed, 19 Jan 2022 17:33:12 +0200 Subject: [PATCH] importas: new option `no-extra-aliases` (#2494) --- .golangci.example.yml | 4 +++- go.mod | 2 +- go.sum | 4 ++++ pkg/config/linters_settings.go | 5 +++-- pkg/golinters/importas.go | 7 +++++-- 5 files changed, 16 insertions(+), 6 deletions(-) diff --git a/.golangci.example.yml b/.golangci.example.yml index 41941d54dd24..2ef4f7755142 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -554,8 +554,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 diff --git a/go.mod b/go.mod index 8bb0635cd5d2..e80411ba6131 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 604c663bea91..f5a3e8cfea36 100644 --- a/go.sum +++ b/go.sum @@ -455,6 +455,10 @@ github.com/juju/ratelimit v1.0.1/go.mod h1:qapgC/Gy+xNh9UxzV13HGGl/6UXNN+ct+vwSg github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/julz/importas v0.0.0-20210419104244-841f0c0fe66d h1:XeSMXURZPtUffuWAaq90o6kLgZdgu+QA8wk4MPC8ikI= github.com/julz/importas v0.0.0-20210419104244-841f0c0fe66d/go.mod h1:oSFU2R4XK/P7kNBrnL/FEQlDGN1/6WoxXEjSSXO0DV0= +github.com/julz/importas v0.0.0-20210922140945-27e0a5d4dee2 h1:3sSu9gZvOTazWE4B4wsND7ofCsn75BD8Iz1OCBUZISs= +github.com/julz/importas v0.0.0-20210922140945-27e0a5d4dee2/go.mod h1:oSFU2R4XK/P7kNBrnL/FEQlDGN1/6WoxXEjSSXO0DV0= +github.com/julz/importas v0.1.0 h1:F78HnrsjY3cR7j0etXy5+TU1Zuy7Xt08X/1aJnH5xXY= +github.com/julz/importas v0.1.0/go.mod h1:oSFU2R4XK/P7kNBrnL/FEQlDGN1/6WoxXEjSSXO0DV0= github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k= github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index c8ad6cb84ef4..96f0e6153a55 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -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 { diff --git a/pkg/golinters/importas.go b/pkg/golinters/importas.go index 523aa257b43a..ba217622062a 100644 --- a/pkg/golinters/importas.go +++ b/pkg/golinters/importas.go @@ -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) }