Skip to content

Commit 8b0922a

Browse files
author
Sergey Vilgelm
committed
Integrate ImportAs linter
1 parent eefb974 commit 8b0922a

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

pkg/config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ type LintersSettings struct {
275275
Ifshort IfshortSettings
276276
Predeclared PredeclaredSettings
277277
Cyclop Cyclop
278+
ImportAs ImportAsSettings
278279

279280
Custom map[string]CustomLinterSettings
280281
}
@@ -461,6 +462,8 @@ type Cyclop struct {
461462
SkipTests bool `mapstructure:"skip-tests"`
462463
}
463464

465+
type ImportAsSettings map[string]string
466+
464467
var defaultLintersSettings = LintersSettings{
465468
Lll: LllSettings{
466469
LineLength: 120,

pkg/golinters/importas.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package golinters
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/julz/importas"
7+
"golang.org/x/tools/go/analysis"
8+
9+
"github.com/golangci/golangci-lint/pkg/config"
10+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
11+
)
12+
13+
func NewImportAs(cfg *config.ImportAsSettings) *goanalysis.Linter {
14+
analyzer := importas.Analyzer
15+
for alias, pkg := range *cfg {
16+
analyzer.Flags.Set("alias", fmt.Sprintf("%s:%s", pkg, alias))
17+
}
18+
19+
return goanalysis.NewLinter(
20+
analyzer.Name,
21+
analyzer.Doc,
22+
[]*analysis.Analyzer{analyzer},
23+
nil,
24+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
25+
}

pkg/lint/lintersdb/manager.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
9999
var ifshortCfg *config.IfshortSettings
100100
var reviveCfg *config.ReviveSettings
101101
var cyclopCfg *config.Cyclop
102+
var importAsCfg *config.ImportAsSettings
102103
if m.cfg != nil {
103104
govetCfg = &m.cfg.LintersSettings.Govet
104105
testpackageCfg = &m.cfg.LintersSettings.Testpackage
@@ -110,6 +111,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
110111
ifshortCfg = &m.cfg.LintersSettings.Ifshort
111112
reviveCfg = &m.cfg.LintersSettings.Revive
112113
cyclopCfg = &m.cfg.LintersSettings.Cyclop
114+
importAsCfg = &m.cfg.LintersSettings.ImportAs
113115
}
114116
const megacheckName = "megacheck"
115117
lcs := []*linter.Config{
@@ -380,6 +382,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
380382
WithPresets(linter.PresetStyle).
381383
WithLoadForGoAnalysis().
382384
WithURL("https://github.com/sanposhiho/wastedassign"),
385+
linter.NewConfig(golinters.NewImportAs(importAsCfg)).
386+
WithPresets(linter.PresetStyle).
387+
WithLoadForGoAnalysis().
388+
WithURL("https://github.com/julz/importas"),
383389

384390
// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
385391
linter.NewConfig(golinters.NewNoLintLint()).

0 commit comments

Comments
 (0)