Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add purge directive #1258

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

Merged
merged 7 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Updated to use Inspect and srctesting
  • Loading branch information
grantnelson-wf committed Dec 19, 2023
commit 7758c0d142d880a546e128b59f8e4b1bb79d5079
4 changes: 2 additions & 2 deletions 4 build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,14 +406,14 @@ func pruneImports(file *ast.File) {
}

// Remove "unused import" for any import which is used.
ast.Walk(astutil.NewCallbackVisitor(func(n ast.Node) bool {
ast.Inspect(file, func(n ast.Node) bool {
if sel, ok := n.(*ast.SelectorExpr); ok {
if id, ok := sel.X.(*ast.Ident); ok && id.Obj == nil {
delete(unused, id.Name)
}
}
return len(unused) > 0
}), file)
})
if len(unused) == 0 {
return
}
Expand Down
22 changes: 5 additions & 17 deletions 22 build/build_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package build

import (
"bytes"
"fmt"
gobuild "go/build"
"go/printer"
"go/token"
"strconv"
"testing"
Expand Down Expand Up @@ -390,16 +388,11 @@ func TestOverlayAugmentation(t *testing.T) {
augmentOverlayFile(fileSrc, overrides)
pruneImports(fileSrc)

buf := &bytes.Buffer{}
_ = printer.Fprint(buf, fsetSrc, fileSrc)
got := buf.String()
got := srctesting.Format(t, fsetSrc, fileSrc)

fsetWant := token.NewFileSet()
fileWant := srctesting.Parse(t, fsetWant, pkgName+test.want)

buf.Reset()
_ = printer.Fprint(buf, fsetWant, fileWant)
want := buf.String()
want := srctesting.Format(t, fsetWant, fileWant)

if got != want {
t.Errorf("augmentOverlayFile and pruneImports got unexpected code:\n"+
Expand Down Expand Up @@ -536,7 +529,7 @@ func TestOriginalAugmentation(t *testing.T) {

func NewFoo(bar int) *Foo { return &Foo{bar: bar} }`,
// NewFoo is not removed automatically since
// only functions with Foo as a receiver is removed.
// only functions with Foo as a receiver are removed.
want: `func NewFoo(bar int) *Foo { return &Foo{bar: bar} }`,
}, {
desc: `remove generics`,
Expand Down Expand Up @@ -591,16 +584,11 @@ func TestOriginalAugmentation(t *testing.T) {
augmentOriginalFile(fileSrc, test.info)
pruneImports(fileSrc)

buf := &bytes.Buffer{}
_ = printer.Fprint(buf, fsetSrc, fileSrc)
got := buf.String()
got := srctesting.Format(t, fsetSrc, fileSrc)

fsetWant := token.NewFileSet()
fileWant := srctesting.Parse(t, fsetWant, pkgName+test.want)

buf.Reset()
_ = printer.Fprint(buf, fsetWant, fileWant)
want := buf.String()
want := srctesting.Format(t, fsetWant, fileWant)

if got != want {
t.Errorf("augmentOriginalImports, augmentOriginalFile, and pruneImports got unexpected code:\n"+
Expand Down
16 changes: 0 additions & 16 deletions 16 compiler/astutil/astutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,19 +251,3 @@ func Squeeze[E ast.Node, S ~[]E](s S) S {
}
return s[:dest]
}

type CallbackVisitor struct {
predicate func(node ast.Node) bool
}

func NewCallbackVisitor(predicate func(node ast.Node) bool) *CallbackVisitor {
return &CallbackVisitor{predicate: predicate}
}

func (v *CallbackVisitor) Visit(node ast.Node) ast.Visitor {
if v.predicate != nil && v.predicate(node) {
return v
}
v.predicate = nil
return nil
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.