-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Iskander Sharipov <[email protected]>
- Loading branch information
Showing
3 changed files
with
80 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package contrib | ||
|
||
import ( | ||
"go/ast" | ||
"strings" | ||
|
||
"github.com/go-lintpack/lintpack" | ||
"github.com/go-lintpack/lintpack/astwalk" | ||
) | ||
|
||
func init() { | ||
var info lintpack.CheckerInfo | ||
info.Name = "importPackageName" | ||
info.Tags = []string{"style"} | ||
info.Summary = "Detects when imported package names are unnecessary renamed" | ||
info.Before = `import lint "github.com/go-critic/go-critic/lint"` | ||
info.After = `import "github.com/go-critic/go-critic/lint"` | ||
|
||
lintpack.AddChecker(&info, func(ctx *lintpack.CheckerContext) lintpack.FileWalker { | ||
return &importPackageNameChecker{ctx: ctx} | ||
}) | ||
} | ||
|
||
type importPackageNameChecker struct { | ||
astwalk.WalkHandler | ||
ctx *lintpack.CheckerContext | ||
} | ||
|
||
func (c *importPackageNameChecker) WalkFile(file *ast.File) { | ||
for _, imp := range file.Imports { | ||
var pkgName string | ||
for _, pkgImport := range c.ctx.Pkg.Imports() { | ||
if pkgImport.Path() == strings.Trim(imp.Path.Value, `"`) { | ||
pkgName = pkgImport.Name() | ||
break | ||
} | ||
} | ||
|
||
if imp.Name != nil && imp.Name.Name == pkgName { | ||
c.warn(imp) | ||
} | ||
} | ||
} | ||
|
||
func (c *importPackageNameChecker) warn(cause ast.Node) { | ||
c.ctx.Warn(cause, "unnecessary rename of import package") | ||
} |
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,15 @@ | ||
package checker_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
linter "github.com/go-critic/go-critic/lint" | ||
|
||
dummy "github.com/go-critic/go-critic/lint/internal/dummy" | ||
) | ||
|
||
func noWarnings() { | ||
dummy.Dummy() | ||
|
||
fmt.Printf("Hello Rule=%v", linter.Rule{}) | ||
} |
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,18 @@ | ||
package checker_test | ||
|
||
import ( | ||
/// unnecessary rename of import package | ||
fmt "fmt" | ||
|
||
/// unnecessary rename of import package | ||
lint "github.com/go-critic/go-critic/lint" | ||
|
||
/// unnecessary rename of import package | ||
dummypkg "github.com/go-critic/go-critic/lint/internal/dummy" | ||
) | ||
|
||
func warnings() { | ||
dummypkg.Dummy() | ||
|
||
fmt.Printf("Hello Rule=%v\n", lint.Rule{}) | ||
} |