Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rule for redundant import alias #854

Merged
merged 3 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -1,4 +1,4 @@
package config

Check warning on line 1 in config/config.go

View workflow job for this annotation

GitHub Actions / Lint

should have a package comment

Check warning on line 1 in config/config.go

View workflow job for this annotation

GitHub Actions / Lint

should have a package comment

Check warning on line 1 in config/config.go

View workflow job for this annotation

GitHub Actions / Lint

should have a package comment

import (
"errors"
Expand Down Expand Up @@ -87,6 +87,7 @@
&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/
)

Loading