Skip to content

Commit

Permalink
Integrate ImportAs linter
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Vilgelm committed Feb 26, 2021
1 parent eefb974 commit 8b0922a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/config/config.go
Expand Up @@ -275,6 +275,7 @@ type LintersSettings struct {
Ifshort IfshortSettings
Predeclared PredeclaredSettings
Cyclop Cyclop
ImportAs ImportAsSettings

Custom map[string]CustomLinterSettings
}
Expand Down Expand Up @@ -461,6 +462,8 @@ type Cyclop struct {
SkipTests bool `mapstructure:"skip-tests"`
}

type ImportAsSettings map[string]string

var defaultLintersSettings = LintersSettings{
Lll: LllSettings{
LineLength: 120,
Expand Down
25 changes: 25 additions & 0 deletions 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)
}
6 changes: 6 additions & 0 deletions pkg/lint/lintersdb/manager.go
Expand Up @@ -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
Expand All @@ -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{
Expand Down Expand Up @@ -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()).
Expand Down

0 comments on commit 8b0922a

Please sign in to comment.