-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add rule for redundant import alias (#854)
- Loading branch information
Showing
6 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ | ||
) | ||
|