Skip to content

Commit

Permalink
Update importas to HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Apr 26, 2021
1 parent 8f3ad45 commit e66ad2a
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 15 deletions.
21 changes: 13 additions & 8 deletions .golangci.example.yml
Expand Up @@ -397,14 +397,19 @@ linters-settings:
max-decl-chars: 30

importas:
# using `servingv1` alias for `knative.dev/serving/pkg/apis/serving/v1` package
servingv1: knative.dev/serving/pkg/apis/serving/v1
# using `autoscalingv1alpha1` alias for `knative.dev/serving/pkg/apis/autoscaling/v1alpha1` package
autoscalingv1alpha1: knative.dev/serving/pkg/apis/autoscaling/v1alpha1
# You can specify the package path by regular expression,
# and alias by regular expression expansion syntax like below.
# see https://github.com/julz/importas#use-regular-expression for details
"$1$2": 'knative.dev/serving/pkg/apis/(\w+)/(v[\w\d]+)'
# if set to `true`, force to use alias.
no-unaliased: true
# List of aliases
alias:
# using `servingv1` alias for `knative.dev/serving/pkg/apis/serving/v1` package
servingv1: knative.dev/serving/pkg/apis/serving/v1
# using `autoscalingv1alpha1` alias for `knative.dev/serving/pkg/apis/autoscaling/v1alpha1` package
autoscalingv1alpha1: knative.dev/serving/pkg/apis/autoscaling/v1alpha1
# You can specify the package path by regular expression,
# and alias by regular expression expansion syntax like below.
# see https://github.com/julz/importas#use-regular-expression for details
"$1$2": 'knative.dev/serving/pkg/apis/(\w+)/(v[\w\d]+)'


lll:
# max line length, lines longer will be reported. Default is 120.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -37,7 +37,7 @@ require (
github.com/jgautheron/goconst v1.4.0
github.com/jingyugao/rowserrcheck v0.0.0-20210315055705-d907ca737bb1
github.com/jirfag/go-printf-func-name v0.0.0-20200119135958-7558a9eaa5af
github.com/julz/importas v0.0.0-20210405141620-a22c8f743dc9
github.com/julz/importas v0.0.0-20210419104244-841f0c0fe66d
github.com/kisielk/errcheck v1.6.0
github.com/kulti/thelper v0.4.0
github.com/kunwardeep/paralleltest v1.0.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion pkg/config/linters_settings.go
Expand Up @@ -303,7 +303,10 @@ type IfshortSettings struct {
MaxDeclChars int `mapstructure:"max-decl-chars"`
}

type ImportAsSettings map[string]string
type ImportAsSettings struct {
Alias map[string]string
NoUnaliased bool `mapstructure:"no-unaliased"`
}

type LllSettings struct {
LineLength int `mapstructure:"line-length"`
Expand Down
8 changes: 7 additions & 1 deletion pkg/golinters/importas.go
Expand Up @@ -2,6 +2,7 @@ package golinters

import (
"fmt"
"strconv"

"github.com/julz/importas" // nolint: misspell
"golang.org/x/tools/go/analysis"
Expand All @@ -24,7 +25,12 @@ func NewImportAs(settings *config.ImportAsSettings) *goanalysis.Linter {
return
}

for alias, pkg := range *settings {
err := analyzer.Flags.Set("no-unaliased", strconv.FormatBool(settings.NoUnaliased))
if err != nil {
lintCtx.Log.Errorf("failed to parse configuration: %v", err)
}

for alias, pkg := range settings.Alias {
err := analyzer.Flags.Set("alias", fmt.Sprintf("%s:%s", pkg, alias))
if err != nil {
lintCtx.Log.Errorf("failed to parse configuration: %v", err)
Expand Down
3 changes: 2 additions & 1 deletion test/testdata/configs/importas.yml
@@ -1,4 +1,5 @@
linters-settings:
importas:
importas:
alias:
fff: fmt
std_os: os
6 changes: 6 additions & 0 deletions test/testdata/configs/importas_strict.yml
@@ -0,0 +1,6 @@
linters-settings:
importas:
no-unaliased: true
alias:
fff: fmt
std_os: os
4 changes: 3 additions & 1 deletion test/testdata/importas.go
Expand Up @@ -3,11 +3,13 @@
package testdata

import (
wrong_alias "fmt" // ERROR `import "fmt" imported as "wrong_alias" but must be "fff" according to config`
wrong_alias "fmt" // ERROR `import "fmt" imported as "wrong_alias" but must be "fff" according to config`
"os"
wrong_alias_again "os" // ERROR `import "os" imported as "wrong_alias_again" but must be "std_os" according to config`
)

func ImportAsWrongAlias() {
wrong_alias.Println("foo")
wrong_alias_again.Stdout.WriteString("bar")
os.Stdout.WriteString("test")
}
15 changes: 15 additions & 0 deletions test/testdata/importas_strict.go
@@ -0,0 +1,15 @@
//args: -Eimportas
//config_path: testdata/configs/importas_strict.yml
package testdata

import (
wrong_alias "fmt" // ERROR `import "fmt" imported as "wrong_alias" but must be "fff" according to config`
"os" // ERROR `import "os" imported without alias but must be with alias "std_os" according to config`
wrong_alias_again "os" // ERROR `import "os" imported as "wrong_alias_again" but must be "std_os" according to config`
)

func ImportAsStrictWrongAlias() {
wrong_alias.Println("foo")
wrong_alias_again.Stdout.WriteString("bar")
os.Stdout.WriteString("test")
}

0 comments on commit e66ad2a

Please sign in to comment.