From 0ef6c878d228bc29801e5fdbd3dc8f57de49595f Mon Sep 17 00:00:00 2001 From: Marcin Federowicz Date: Fri, 22 Dec 2023 16:55:36 +0100 Subject: [PATCH] refactor: invert conditions to reduce indent, restore old testdata, and change usage of getImportPackageName --- rule/redundant-import-alias.go | 81 +++++++++++++--------- testdata/redundant-import-alias-ignored.go | 2 +- testdata/redundant-import-alias.go | 19 +++-- 3 files changed, 57 insertions(+), 45 deletions(-) diff --git a/rule/redundant-import-alias.go b/rule/redundant-import-alias.go index ce1fbcaa1..99a88a0d2 100644 --- a/rule/redundant-import-alias.go +++ b/rule/redundant-import-alias.go @@ -3,6 +3,7 @@ package rule import ( "fmt" "go/ast" + "strings" "sync" "github.com/mgechev/revive/lint" @@ -59,35 +60,39 @@ func (r *RedundantImportAlias) checkRedundantAliases(node ast.Node) map[string]s return true } - if imp.Name != nil && imp.Path != nil { - aliasedPackages[imp.Name.Name] = "redundant" + if imp.Name != nil && imp.Path != nil && imp.Name.Name != "_" && getImportPackageName(imp) == imp.Name.Name { + aliasedPackages[imp.Name.Name] = "redundant" } + return true }) - if r.ignoreUsed { - // Second pass: remove one time used aliases - ast.Inspect(node, func(n ast.Node) bool { - sel, ok := n.(*ast.SelectorExpr) - if !ok { - return true - } - - x, ok := sel.X.(*ast.Ident) - if !ok { - return true - } + if !r.ignoreUsed { + return aliasedPackages + } - // This alias is being used; it's not redundant - if _, exists := aliasedPackages[x.Name]; exists { - delete(aliasedPackages, x.Name) - } + // Second pass: remove one-time used aliases + ast.Inspect(node, func(n ast.Node) bool { + sel, ok := n.(*ast.SelectorExpr) + if !ok { + return true + } + x, ok := sel.X.(*ast.Ident) + if !ok { return true - }) - } + } + + // This alias is being used; it's not redundant + if _, exists := aliasedPackages[x.Name]; exists { + delete(aliasedPackages, x.Name) + } + + return true + }) return aliasedPackages + } func (r *RedundantImportAlias) configure(arguments lint.Arguments) { @@ -95,22 +100,32 @@ func (r *RedundantImportAlias) configure(arguments lint.Arguments) { defer r.Unlock() r.ignoreUsed = defaultIgnoreUsed - if len(arguments) > 0 { - args, ok := arguments[0].(map[string]any) - if !ok { - panic(fmt.Sprintf("Invalid argument to the redundant-import-alias rule. Expecting a k,v map, got %T", arguments[0])) - } + if len(arguments) == 0 { + return + } + + args, ok := arguments[0].(map[string]any) + if !ok { + panic(fmt.Sprintf("Invalid argument to the redundant-import-alias rule. Expecting a k,v map, got %T", arguments[0])) + } - for k, v := range args { - switch k { - case "ignoreUsed": - value, ok := v.(bool) - if !ok { - panic(fmt.Sprintf("Invalid argument to the redundant-import-alias rule, expecting string representation of an bool. Got '%v' (%T)", v, v)) - } - r.ignoreUsed = value + for k, v := range args { + switch k { + case "ignoreUsed": + value, ok := v.(bool) + if !ok { + panic(fmt.Sprintf("Invalid argument to the redundant-import-alias rule, expecting string representation of an bool. Got '%v' (%T)", v, v)) } + r.ignoreUsed = value } } + +} + +func getImportPackageName(imp *ast.ImportSpec) string { + path := strings.Trim(imp.Path.Value, `"`) + parts := strings.Split(path, "/") + packageName := parts[len(parts)-1] + return packageName } diff --git a/testdata/redundant-import-alias-ignored.go b/testdata/redundant-import-alias-ignored.go index 57c3acb17..4061d2e96 100644 --- a/testdata/redundant-import-alias-ignored.go +++ b/testdata/redundant-import-alias-ignored.go @@ -5,7 +5,7 @@ import ( "crypto/md5" md5 "crypto/md5" "strings" - str "strings" // MATCH /Import alias "str" is redundant/ + str "strings" strings "strings" // MATCH /Import alias "strings" is redundant/ ) diff --git a/testdata/redundant-import-alias.go b/testdata/redundant-import-alias.go index 0fc508ac7..c90a26b4d 100644 --- a/testdata/redundant-import-alias.go +++ b/testdata/redundant-import-alias.go @@ -1,16 +1,13 @@ package fixtures -import ( +import( "crypto/md5" - md5 "crypto/md5" // MATCH /Import alias "md5" is redundant/ - - runpb "cloud.google.com/go/run/apiv2/runpb" // MATCH /Import alias "runpb" is redundant/ + "strings" + _ "crypto/md5" + str "strings" + strings "strings" // MATCH /Import alias "strings" is redundant/ + crypto "crypto/md5" + redundant "abc/redundant" // MATCH /Import alias "redundant" is redundant/ + md5 "crypto/md5" // MATCH /Import alias "md5" is redundant/ ) -func UseRunpb() { - runpb.RegisterTasksServer() -} - -func UseMd5() { - fmt.PrintLn(md5.Size) -}