Skip to content

Commit 49babc0

Browse files
committed
Allow repeated importas aliases
The standalone importas takes arguments in the form "{package}:{alias}" and stores them in a map in which package is the key. The intent is to describe the one alias that is appropriate for a unique package path. This aligns our configuration with the standalone tool.
1 parent 07a0568 commit 49babc0

File tree

7 files changed

+18
-11
lines changed

7 files changed

+18
-11
lines changed

.golangci.example.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -410,13 +410,13 @@ linters-settings:
410410
# List of aliases
411411
alias:
412412
# using `servingv1` alias for `knative.dev/serving/pkg/apis/serving/v1` package
413-
servingv1: knative.dev/serving/pkg/apis/serving/v1
413+
- knative.dev/serving/pkg/apis/serving/v1:servingv1
414414
# using `autoscalingv1alpha1` alias for `knative.dev/serving/pkg/apis/autoscaling/v1alpha1` package
415-
autoscalingv1alpha1: knative.dev/serving/pkg/apis/autoscaling/v1alpha1
415+
- knative.dev/serving/pkg/apis/autoscaling/v1alpha1:autoscalingv1alpha1
416416
# You can specify the package path by regular expression,
417417
# and alias by regular expression expansion syntax like below.
418418
# see https://github.com/julz/importas#use-regular-expression for details
419-
"$1$2": 'knative.dev/serving/pkg/apis/(\w+)/(v[\w\d]+)'
419+
- 'knative.dev/serving/pkg/apis/(\w+)/(v[\w\d]+):$1$2'
420420

421421
lll:
422422
# max line length, lines longer will be reported. Default is 120.

pkg/config/linters_settings.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ type IfshortSettings struct {
312312
}
313313

314314
type ImportAsSettings struct {
315-
Alias map[string]string
315+
Alias []string
316316
NoUnaliased bool `mapstructure:"no-unaliased"`
317317
}
318318

pkg/golinters/importas.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package golinters
22

33
import (
4-
"fmt"
54
"strconv"
65

76
"github.com/julz/importas" // nolint: misspell
@@ -33,8 +32,8 @@ func NewImportAs(settings *config.ImportAsSettings) *goanalysis.Linter {
3332
lintCtx.Log.Errorf("failed to parse configuration: %v", err)
3433
}
3534

36-
for alias, pkg := range settings.Alias {
37-
err := analyzer.Flags.Set("alias", fmt.Sprintf("%s:%s", pkg, alias))
35+
for i := range settings.Alias {
36+
err := analyzer.Flags.Set("alias", settings.Alias[i])
3837
if err != nil {
3938
lintCtx.Log.Errorf("failed to parse configuration: %v", err)
4039
}

test/testdata/configs/importas.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
linters-settings:
22
importas:
33
alias:
4-
fff: fmt
5-
std_os: os
4+
- fmt:fff
5+
- os:std_os
6+
- github.com/pkg/errors:pkgerr

test/testdata/configs/importas_strict.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ linters-settings:
22
importas:
33
no-unaliased: true
44
alias:
5-
fff: fmt
6-
std_os: os
5+
- fmt:fff
6+
- os:std_os
7+
- github.com/pkg/errors:pkgerr

test/testdata/importas.go

+3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import (
66
wrong_alias "fmt" // ERROR `import "fmt" imported as "wrong_alias" but must be "fff" according to config`
77
"os"
88
wrong_alias_again "os" // ERROR `import "os" imported as "wrong_alias_again" but must be "std_os" according to config`
9+
10+
wrong "github.com/pkg/errors" // ERROR `import "github.com/pkg/errors" imported as "wrong" but must be "pkgerr" according to config`
911
)
1012

1113
func ImportAsWrongAlias() {
1214
wrong_alias.Println("foo")
1315
wrong_alias_again.Stdout.WriteString("bar")
1416
os.Stdout.WriteString("test")
17+
_ = wrong.New("baz")
1518
}

test/testdata/importas_strict.go

+3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import (
66
wrong_alias "fmt" // ERROR `import "fmt" imported as "wrong_alias" but must be "fff" according to config`
77
"os" // ERROR `import "os" imported without alias but must be with alias "std_os" according to config`
88
wrong_alias_again "os" // ERROR `import "os" imported as "wrong_alias_again" but must be "std_os" according to config`
9+
10+
wrong "github.com/pkg/errors" // ERROR `import "github.com/pkg/errors" imported as "wrong" but must be "pkgerr" according to config`
911
)
1012

1113
func ImportAsStrictWrongAlias() {
1214
wrong_alias.Println("foo")
1315
wrong_alias_again.Stdout.WriteString("bar")
1416
os.Stdout.WriteString("test")
17+
_ = wrong.New("baz")
1518
}

0 commit comments

Comments
 (0)