Skip to content

Commit ea24a3d

Browse files
Copilotpelikhan
andauthored
test(astutil): cover package selector alias and shadow cases
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
1 parent db8dadf commit ea24a3d

3 files changed

Lines changed: 13 additions & 8 deletions

File tree

pkg/linters/internal/astutil/astutil.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ func IsFmtErrorf(pass *analysis.Pass, call *ast.CallExpr) bool {
7272
return pkgName.Imported().Path() == "fmt"
7373
}
7474

75-
// IsStdPkgSelector reports whether sel is a selector on an imported package
76-
// with the given import path.
77-
func IsStdPkgSelector(pass *analysis.Pass, sel *ast.SelectorExpr, pkgPath string) bool {
75+
// IsPkgSelector reports whether sel is a selector on an imported package with
76+
// the given import path.
77+
func IsPkgSelector(pass *analysis.Pass, sel *ast.SelectorExpr, pkgPath string) bool {
7878
if pass == nil || pass.TypesInfo == nil || sel == nil {
7979
return false
8080
}

pkg/linters/internal/astutil/astutil_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func TestNodeText(t *testing.T) {
6666
}
6767
}
6868

69-
func TestIsStdPkgSelector(t *testing.T) {
69+
func TestIsPkgSelector(t *testing.T) {
7070
t.Parallel()
7171

7272
makePass := func(ident *ast.Ident, obj types.Object) *analysis.Pass {
@@ -84,6 +84,11 @@ func TestIsStdPkgSelector(t *testing.T) {
8484
localIdent := ast.NewIdent("log")
8585

8686
logPkg := types.NewPackage("log", "log")
87+
customType := types.NewNamed(
88+
types.NewTypeName(token.NoPos, nil, "customLogger", nil),
89+
types.NewStruct(nil, nil),
90+
nil,
91+
)
8792

8893
tests := []struct {
8994
name string
@@ -114,7 +119,7 @@ func TestIsStdPkgSelector(t *testing.T) {
114119
},
115120
{
116121
name: "local shadowed identifier",
117-
pass: makePass(localIdent, types.NewVar(token.NoPos, nil, "log", types.Typ[types.String])),
122+
pass: makePass(localIdent, types.NewVar(token.NoPos, nil, "log", types.NewPointer(customType))),
118123
sel: &ast.SelectorExpr{
119124
X: localIdent,
120125
Sel: ast.NewIdent("Printf"),
@@ -137,9 +142,9 @@ func TestIsStdPkgSelector(t *testing.T) {
137142
for _, tt := range tests {
138143
t.Run(tt.name, func(t *testing.T) {
139144
t.Parallel()
140-
got := IsStdPkgSelector(tt.pass, tt.sel, tt.pkgPath)
145+
got := IsPkgSelector(tt.pass, tt.sel, tt.pkgPath)
141146
if got != tt.want {
142-
t.Fatalf("IsStdPkgSelector() = %v, want %v", got, tt.want)
147+
t.Fatalf("IsPkgSelector() = %v, want %v", got, tt.want)
143148
}
144149
})
145150
}

pkg/linters/rawloginlib/rawloginlib.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func run(pass *analysis.Pass) (any, error) {
5555
if !ok {
5656
return
5757
}
58-
if !astutil.IsStdPkgSelector(pass, sel, "log") {
58+
if !astutil.IsPkgSelector(pass, sel, "log") {
5959
return
6060
}
6161
if !rawLogFuncs[sel.Sel.Name] {

0 commit comments

Comments
 (0)