From 8b0922a0197bd5bc390c6521efd8323f5e928857 Mon Sep 17 00:00:00 2001 From: Sergey Vilgelm Date: Thu, 25 Feb 2021 22:47:22 -0600 Subject: [PATCH] Integrate ImportAs linter --- pkg/config/config.go | 3 +++ pkg/golinters/importas.go | 25 +++++++++++++++++++++++++ pkg/lint/lintersdb/manager.go | 6 ++++++ 3 files changed, 34 insertions(+) create mode 100644 pkg/golinters/importas.go diff --git a/pkg/config/config.go b/pkg/config/config.go index fdf57875c0c1..202ae6bc44cb 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -275,6 +275,7 @@ type LintersSettings struct { Ifshort IfshortSettings Predeclared PredeclaredSettings Cyclop Cyclop + ImportAs ImportAsSettings Custom map[string]CustomLinterSettings } @@ -461,6 +462,8 @@ type Cyclop struct { SkipTests bool `mapstructure:"skip-tests"` } +type ImportAsSettings map[string]string + var defaultLintersSettings = LintersSettings{ Lll: LllSettings{ LineLength: 120, diff --git a/pkg/golinters/importas.go b/pkg/golinters/importas.go new file mode 100644 index 000000000000..5ab34e034638 --- /dev/null +++ b/pkg/golinters/importas.go @@ -0,0 +1,25 @@ +package golinters + +import ( + "fmt" + + "github.com/julz/importas" + "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/config" + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" +) + +func NewImportAs(cfg *config.ImportAsSettings) *goanalysis.Linter { + analyzer := importas.Analyzer + for alias, pkg := range *cfg { + analyzer.Flags.Set("alias", fmt.Sprintf("%s:%s", pkg, alias)) + } + + return goanalysis.NewLinter( + analyzer.Name, + analyzer.Doc, + []*analysis.Analyzer{analyzer}, + nil, + ).WithLoadMode(goanalysis.LoadModeTypesInfo) +} diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 17f621898d75..8881bc46f579 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -99,6 +99,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { var ifshortCfg *config.IfshortSettings var reviveCfg *config.ReviveSettings var cyclopCfg *config.Cyclop + var importAsCfg *config.ImportAsSettings if m.cfg != nil { govetCfg = &m.cfg.LintersSettings.Govet testpackageCfg = &m.cfg.LintersSettings.Testpackage @@ -110,6 +111,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { ifshortCfg = &m.cfg.LintersSettings.Ifshort reviveCfg = &m.cfg.LintersSettings.Revive cyclopCfg = &m.cfg.LintersSettings.Cyclop + importAsCfg = &m.cfg.LintersSettings.ImportAs } const megacheckName = "megacheck" lcs := []*linter.Config{ @@ -380,6 +382,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithPresets(linter.PresetStyle). WithLoadForGoAnalysis(). WithURL("https://github.com/sanposhiho/wastedassign"), + linter.NewConfig(golinters.NewImportAs(importAsCfg)). + WithPresets(linter.PresetStyle). + WithLoadForGoAnalysis(). + WithURL("https://github.com/julz/importas"), // nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives linter.NewConfig(golinters.NewNoLintLint()).