Skip to content

Commit

Permalink
feat: add rule for redundant import alias (#854)
Browse files Browse the repository at this point in the history
  • Loading branch information
damif94 authored Jul 31, 2023
1 parent 26bc59f commit 7bd6668
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ List of all available rules. The rules ported from `golint` are left unchanged a
| [`use-any`](./RULES_DESCRIPTIONS.md#use-any) | n/a | Proposes to replace `interface{}` with its alias `any` | no | no |
| [`datarace`](./RULES_DESCRIPTIONS.md#datarace) | n/a | Spots potential dataraces | no | no |
| [`comment-spacings`](./RULES_DESCRIPTIONS.md#comment-spacings) | []string | Warns on malformed comments | no | no |
| [`redundant-import-alias`](./RULES_DESCRIPTIONS.md#redundant-import-alias) | n/a | Warns on import aliases matching the imported package name | no | no |


## Configurable rules
Expand Down
7 changes: 7 additions & 0 deletions RULES_DESCRIPTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ List of all available rules.
- [use-any](#use-any)
- [useless-break](#useless-break)
- [waitgroup-by-value](#waitgroup-by-value)
- [redundant-import-alias](#redundant-import-alias)

## add-constant

Expand Down Expand Up @@ -760,3 +761,9 @@ _Description_: Function parameters that are passed by value, are in fact a copy
This rule warns when a `sync.WaitGroup` expected as a by-value parameter in a function or method.

_Configuration_: N/A

## redundant-import-alias

_Description_: This rule warns on redundant import aliases. This happens when the alias used on the import statement matches the imported package name.

_Configuration_: N/A
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ var allRules = append([]lint.Rule{
&rule.DataRaceRule{},
&rule.CommentSpacingsRule{},
&rule.IfReturnRule{},
&rule.RedundantImportAlias{},
}, defaultRules...)

var allFormatters = []lint.Formatter{
Expand Down
52 changes: 52 additions & 0 deletions rule/redundant-import-alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package rule

import (
"fmt"
"go/ast"
"strings"

"github.com/mgechev/revive/lint"
)

// RedundantImportAlias lints given else constructs.
type RedundantImportAlias struct{}

// Apply applies the rule to given file.
func (*RedundantImportAlias) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
var failures []lint.Failure

for _, imp := range file.AST.Imports {
if imp.Name == nil {
continue
}

if getImportPackageName(imp) == imp.Name.Name {
failures = append(failures, lint.Failure{
Confidence: 1,
Failure: fmt.Sprintf("Import alias \"%s\" is redundant", imp.Name.Name),
Node: imp,
Category: "imports",
})
}
}

return failures
}

// Name returns the rule name.
func (*RedundantImportAlias) Name() string {
return "redundant-import-alias"
}

func getImportPackageName(imp *ast.ImportSpec) string {
const pathSep = "/"
const strDelim = `"`

path := imp.Path.Value
i := strings.LastIndex(path, pathSep)
if i == -1 {
return strings.Trim(path, strDelim)
}

return strings.Trim(path[i+1:], strDelim)
}
11 changes: 11 additions & 0 deletions test/redundant-import-alias_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package test

import (
"github.com/mgechev/revive/rule"
"testing"
)

// TestRedundantImportAlias rule.
func TestRedundantImportAlias(t *testing.T) {
testRule(t, "redundant-import-alias", &rule.RedundantImportAlias{})
}
12 changes: 12 additions & 0 deletions testdata/redundant-import-alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package fixtures

import(
"crypto/md5"
"strings"
_ "crypto/md5"
str "strings"
strings "strings" // MATCH /Import alias "strings" is redundant/
crypto "crypto/md5"
md5 "crypto/md5" // MATCH /Import alias "md5" is redundant/
)

0 comments on commit 7bd6668

Please sign in to comment.