importas: allow repeated aliases (#1960)
Some checks failed
Extra / Vulnerability scanner (push) Has been cancelled
CI / go-mod (push) Has been cancelled
CI / golangci-lint (push) Has been cancelled
Release a tag / release (push) Has been cancelled
CI / tests-on-windows (push) Has been cancelled
CI / tests-on-macos (push) Has been cancelled
CI / tests-on-unix (1.15) (push) Has been cancelled
CI / tests-on-unix (1.16) (push) Has been cancelled
CI / check_generated (push) Has been cancelled
Release a tag / docker-release (map[Dockerfile:build/Dockerfile.alpine]) (push) Has been cancelled
Release a tag / docker-release (map[Dockerfile:build/Dockerfile]) (push) Has been cancelled
Some checks failed
Extra / Vulnerability scanner (push) Has been cancelled
CI / go-mod (push) Has been cancelled
CI / golangci-lint (push) Has been cancelled
Release a tag / release (push) Has been cancelled
CI / tests-on-windows (push) Has been cancelled
CI / tests-on-macos (push) Has been cancelled
CI / tests-on-unix (1.15) (push) Has been cancelled
CI / tests-on-unix (1.16) (push) Has been cancelled
CI / check_generated (push) Has been cancelled
Release a tag / docker-release (map[Dockerfile:build/Dockerfile.alpine]) (push) Has been cancelled
Release a tag / docker-release (map[Dockerfile:build/Dockerfile]) (push) Has been cancelled
This commit is contained in:
parent
07a0568df1
commit
5c6adb63c9
@ -410,13 +410,16 @@ linters-settings:
|
|||||||
# List of aliases
|
# List of aliases
|
||||||
alias:
|
alias:
|
||||||
# using `servingv1` alias for `knative.dev/serving/pkg/apis/serving/v1` package
|
# using `servingv1` alias for `knative.dev/serving/pkg/apis/serving/v1` package
|
||||||
servingv1: knative.dev/serving/pkg/apis/serving/v1
|
- pkg: knative.dev/serving/pkg/apis/serving/v1
|
||||||
|
alias: servingv1
|
||||||
# using `autoscalingv1alpha1` alias for `knative.dev/serving/pkg/apis/autoscaling/v1alpha1` package
|
# using `autoscalingv1alpha1` alias for `knative.dev/serving/pkg/apis/autoscaling/v1alpha1` package
|
||||||
autoscalingv1alpha1: knative.dev/serving/pkg/apis/autoscaling/v1alpha1
|
- pkg: knative.dev/serving/pkg/apis/autoscaling/v1alpha1
|
||||||
|
alias: autoscalingv1alpha1
|
||||||
# You can specify the package path by regular expression,
|
# You can specify the package path by regular expression,
|
||||||
# and alias by regular expression expansion syntax like below.
|
# and alias by regular expression expansion syntax like below.
|
||||||
# see https://github.com/julz/importas#use-regular-expression for details
|
# see https://github.com/julz/importas#use-regular-expression for details
|
||||||
"$1$2": 'knative.dev/serving/pkg/apis/(\w+)/(v[\w\d]+)'
|
- pkg: knative.dev/serving/pkg/apis/(\w+)/(v[\w\d]+)
|
||||||
|
alias: $1$2
|
||||||
|
|
||||||
lll:
|
lll:
|
||||||
# max line length, lines longer will be reported. Default is 120.
|
# max line length, lines longer will be reported. Default is 120.
|
||||||
|
@ -312,10 +312,15 @@ type IfshortSettings struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ImportAsSettings struct {
|
type ImportAsSettings struct {
|
||||||
Alias map[string]string
|
Alias []ImportAsAlias
|
||||||
NoUnaliased bool `mapstructure:"no-unaliased"`
|
NoUnaliased bool `mapstructure:"no-unaliased"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ImportAsAlias struct {
|
||||||
|
Pkg string
|
||||||
|
Alias string
|
||||||
|
}
|
||||||
|
|
||||||
type LllSettings struct {
|
type LllSettings struct {
|
||||||
LineLength int `mapstructure:"line-length"`
|
LineLength int `mapstructure:"line-length"`
|
||||||
TabWidth int `mapstructure:"tab-width"`
|
TabWidth int `mapstructure:"tab-width"`
|
||||||
|
@ -33,8 +33,13 @@ func NewImportAs(settings *config.ImportAsSettings) *goanalysis.Linter {
|
|||||||
lintCtx.Log.Errorf("failed to parse configuration: %v", err)
|
lintCtx.Log.Errorf("failed to parse configuration: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for alias, pkg := range settings.Alias {
|
for _, a := range settings.Alias {
|
||||||
err := analyzer.Flags.Set("alias", fmt.Sprintf("%s:%s", pkg, alias))
|
if a.Pkg == "" {
|
||||||
|
lintCtx.Log.Errorf("invalid configuration, empty package: pkg=%s alias=%s", a.Pkg, a.Alias)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
err := analyzer.Flags.Set("alias", fmt.Sprintf("%s:%s", a.Pkg, a.Alias))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lintCtx.Log.Errorf("failed to parse configuration: %v", err)
|
lintCtx.Log.Errorf("failed to parse configuration: %v", err)
|
||||||
}
|
}
|
||||||
|
8
test/testdata/configs/importas.yml
vendored
8
test/testdata/configs/importas.yml
vendored
@ -1,5 +1,9 @@
|
|||||||
linters-settings:
|
linters-settings:
|
||||||
importas:
|
importas:
|
||||||
alias:
|
alias:
|
||||||
fff: fmt
|
- pkg: fmt
|
||||||
std_os: os
|
alias: fff
|
||||||
|
- pkg: os
|
||||||
|
alias: std_os
|
||||||
|
- pkg: github.com/pkg/errors
|
||||||
|
alias: pkgerr
|
||||||
|
8
test/testdata/configs/importas_strict.yml
vendored
8
test/testdata/configs/importas_strict.yml
vendored
@ -2,5 +2,9 @@ linters-settings:
|
|||||||
importas:
|
importas:
|
||||||
no-unaliased: true
|
no-unaliased: true
|
||||||
alias:
|
alias:
|
||||||
fff: fmt
|
- pkg: fmt
|
||||||
std_os: os
|
alias: fff
|
||||||
|
- pkg: os
|
||||||
|
alias: std_os
|
||||||
|
- pkg: github.com/pkg/errors
|
||||||
|
alias: pkgerr
|
||||||
|
3
test/testdata/importas.go
vendored
3
test/testdata/importas.go
vendored
@ -6,10 +6,13 @@ 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"
|
"os"
|
||||||
wrong_alias_again "os" // ERROR `import "os" imported as "wrong_alias_again" but must be "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`
|
||||||
|
|
||||||
|
wrong "github.com/pkg/errors" // ERROR `import "github.com/pkg/errors" imported as "wrong" but must be "pkgerr" according to config`
|
||||||
)
|
)
|
||||||
|
|
||||||
func ImportAsWrongAlias() {
|
func ImportAsWrongAlias() {
|
||||||
wrong_alias.Println("foo")
|
wrong_alias.Println("foo")
|
||||||
wrong_alias_again.Stdout.WriteString("bar")
|
wrong_alias_again.Stdout.WriteString("bar")
|
||||||
os.Stdout.WriteString("test")
|
os.Stdout.WriteString("test")
|
||||||
|
_ = wrong.New("baz")
|
||||||
}
|
}
|
||||||
|
3
test/testdata/importas_strict.go
vendored
3
test/testdata/importas_strict.go
vendored
@ -6,10 +6,13 @@ 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" // ERROR `import "os" imported without alias but must be with alias "std_os" 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`
|
wrong_alias_again "os" // ERROR `import "os" imported as "wrong_alias_again" but must be "std_os" according to config`
|
||||||
|
|
||||||
|
wrong "github.com/pkg/errors" // ERROR `import "github.com/pkg/errors" imported as "wrong" but must be "pkgerr" according to config`
|
||||||
)
|
)
|
||||||
|
|
||||||
func ImportAsStrictWrongAlias() {
|
func ImportAsStrictWrongAlias() {
|
||||||
wrong_alias.Println("foo")
|
wrong_alias.Println("foo")
|
||||||
wrong_alias_again.Stdout.WriteString("bar")
|
wrong_alias_again.Stdout.WriteString("bar")
|
||||||
os.Stdout.WriteString("test")
|
os.Stdout.WriteString("test")
|
||||||
|
_ = wrong.New("baz")
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user