Skip to content

Commit 05836e4

Browse files
Sergey Vilgelmldez
Sergey Vilgelm
andauthored
Integrate ImportAs linter (#1783)
* Integrate ImportAs linter * review: don't panic :) Co-authored-by: Fernandez Ludovic <[email protected]>
1 parent 34e46c7 commit 05836e4

File tree

8 files changed

+68
-0
lines changed

8 files changed

+68
-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 v1.4.0
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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
"github.com/golangci/golangci-lint/pkg/lint/linter"
12+
)
13+
14+
func NewImportAs(settings *config.ImportAsSettings) *goanalysis.Linter {
15+
analyzer := importas.Analyzer
16+
17+
return goanalysis.NewLinter(
18+
analyzer.Name,
19+
analyzer.Doc,
20+
[]*analysis.Analyzer{analyzer},
21+
nil,
22+
).WithContextSetter(func(lintCtx *linter.Context) {
23+
if settings == nil {
24+
return
25+
}
26+
27+
for alias, pkg := range *settings {
28+
err := analyzer.Flags.Set("alias", fmt.Sprintf("%s:%s", pkg, alias))
29+
if err != nil {
30+
lintCtx.Log.Errorf("failed to parse configuration: %v", err)
31+
}
32+
}
33+
}).WithLoadMode(goanalysis.LoadModeTypesInfo)
34+
}

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/configs/importas.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
linters-settings:
2+
importas:
3+
fff: fmt
4+
std_os: os

test/testdata/importas.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//args: -Eimportas
2+
//config_path: testdata/configs/importas.yml
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+
wrong_alias_again "os" // ERROR `import "os" imported as "wrong_alias_again" but must be "std_os" according to config`
8+
)
9+
10+
func ImportAsWrongAlias() {
11+
wrong_alias.Println("foo")
12+
wrong_alias_again.Stdout.WriteString("bar")
13+
}

0 commit comments

Comments
 (0)