Skip to content

Commit 7758c0d

Browse files
Updated to use Inspect and srctesting
1 parent 40bddb9 commit 7758c0d

File tree

3 files changed

+7
-35
lines changed

3 files changed

+7
-35
lines changed

build/build.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,14 +406,14 @@ func pruneImports(file *ast.File) {
406406
}
407407

408408
// Remove "unused import" for any import which is used.
409-
ast.Walk(astutil.NewCallbackVisitor(func(n ast.Node) bool {
409+
ast.Inspect(file, func(n ast.Node) bool {
410410
if sel, ok := n.(*ast.SelectorExpr); ok {
411411
if id, ok := sel.X.(*ast.Ident); ok && id.Obj == nil {
412412
delete(unused, id.Name)
413413
}
414414
}
415415
return len(unused) > 0
416-
}), file)
416+
})
417417
if len(unused) == 0 {
418418
return
419419
}

build/build_test.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package build
22

33
import (
4-
"bytes"
54
"fmt"
65
gobuild "go/build"
7-
"go/printer"
86
"go/token"
97
"strconv"
108
"testing"
@@ -390,16 +388,11 @@ func TestOverlayAugmentation(t *testing.T) {
390388
augmentOverlayFile(fileSrc, overrides)
391389
pruneImports(fileSrc)
392390

393-
buf := &bytes.Buffer{}
394-
_ = printer.Fprint(buf, fsetSrc, fileSrc)
395-
got := buf.String()
391+
got := srctesting.Format(t, fsetSrc, fileSrc)
396392

397393
fsetWant := token.NewFileSet()
398394
fileWant := srctesting.Parse(t, fsetWant, pkgName+test.want)
399-
400-
buf.Reset()
401-
_ = printer.Fprint(buf, fsetWant, fileWant)
402-
want := buf.String()
395+
want := srctesting.Format(t, fsetWant, fileWant)
403396

404397
if got != want {
405398
t.Errorf("augmentOverlayFile and pruneImports got unexpected code:\n"+
@@ -536,7 +529,7 @@ func TestOriginalAugmentation(t *testing.T) {
536529
537530
func NewFoo(bar int) *Foo { return &Foo{bar: bar} }`,
538531
// NewFoo is not removed automatically since
539-
// only functions with Foo as a receiver is removed.
532+
// only functions with Foo as a receiver are removed.
540533
want: `func NewFoo(bar int) *Foo { return &Foo{bar: bar} }`,
541534
}, {
542535
desc: `remove generics`,
@@ -591,16 +584,11 @@ func TestOriginalAugmentation(t *testing.T) {
591584
augmentOriginalFile(fileSrc, test.info)
592585
pruneImports(fileSrc)
593586

594-
buf := &bytes.Buffer{}
595-
_ = printer.Fprint(buf, fsetSrc, fileSrc)
596-
got := buf.String()
587+
got := srctesting.Format(t, fsetSrc, fileSrc)
597588

598589
fsetWant := token.NewFileSet()
599590
fileWant := srctesting.Parse(t, fsetWant, pkgName+test.want)
600-
601-
buf.Reset()
602-
_ = printer.Fprint(buf, fsetWant, fileWant)
603-
want := buf.String()
591+
want := srctesting.Format(t, fsetWant, fileWant)
604592

605593
if got != want {
606594
t.Errorf("augmentOriginalImports, augmentOriginalFile, and pruneImports got unexpected code:\n"+

compiler/astutil/astutil.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -251,19 +251,3 @@ func Squeeze[E ast.Node, S ~[]E](s S) S {
251251
}
252252
return s[:dest]
253253
}
254-
255-
type CallbackVisitor struct {
256-
predicate func(node ast.Node) bool
257-
}
258-
259-
func NewCallbackVisitor(predicate func(node ast.Node) bool) *CallbackVisitor {
260-
return &CallbackVisitor{predicate: predicate}
261-
}
262-
263-
func (v *CallbackVisitor) Visit(node ast.Node) ast.Visitor {
264-
if v.predicate != nil && v.predicate(node) {
265-
return v
266-
}
267-
v.predicate = nil
268-
return nil
269-
}

0 commit comments

Comments
 (0)