Skip to content

Commit 973ae65

Browse files
author
Sergey Vilgelm
committed
Integrate ImportAs linter
1 parent eefb974 commit 973ae65

File tree

7 files changed

+57
-0
lines changed

7 files changed

+57
-0
lines changed

.golangci.example.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,11 @@ linters-settings:
444444
- ginkgo\\.F.* # these are used just for local development
445445
# Exclude godoc examples from forbidigo checks. Default is true.
446446
exclude_godoc_examples: false
447+
importas:
448+
# using `servingv1` alias for `knative.dev/serving/pkg/apis/serving/v1` package
449+
servingv1: knative.dev/serving/pkg/apis/serving/v1
450+
# using `autoscalingv1alpha1` alias for `knative.dev/serving/pkg/apis/autoscaling/v1alpha1` package
451+
autoscalingv1alpha1: knative.dev/serving/pkg/apis/autoscaling/v1alpha1
447452

448453
# The custom section can be used to define linter plugins to be loaded at runtime. See README doc
449454
# for more info.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ require (
3434
github.com/jgautheron/goconst v0.0.0-20201117150253-ccae5bf973f3
3535
github.com/jingyugao/rowserrcheck v0.0.0-20210130005344-c6a0c12dd98d
3636
github.com/jirfag/go-printf-func-name v0.0.0-20191110105641-45db9963cdd3
37+
github.com/julz/importas v0.0.0-20210226073942-60b4fa260dd0
3738
github.com/kisielk/errcheck v1.6.0
3839
github.com/kulti/thelper v0.4.0
3940
github.com/kunwardeep/paralleltest v1.0.2

go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package golinters
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/julz/importas" // nolint: misspell
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+
if cfg != nil {
16+
for alias, pkg := range *cfg {
17+
if err := analyzer.Flags.Set("alias", fmt.Sprintf("%s:%s", pkg, alias)); err != nil {
18+
panic(err.Error())
19+
}
20+
}
21+
}
22+
23+
return goanalysis.NewLinter(
24+
analyzer.Name,
25+
analyzer.Doc,
26+
[]*analysis.Analyzer{analyzer},
27+
nil,
28+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
29+
}

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()).

test/testdata/importas.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//args: -Eimportas
2+
//config: linters-settings.importas.fff=fmt
3+
package testdata
4+
5+
import (
6+
wrong_alias "fmt" // ERROR "import \"fmt\" imported as \"wrong_alias\" but must be \"fff\" according to config"
7+
)
8+
9+
func ImportAsWrongAlias() {
10+
wrong_alias.Println("foo")
11+
}

0 commit comments

Comments
 (0)