| 1 |
// Copyright 2023 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package astutil_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"go/ast" |
| 9 |
"go/parser" |
| 10 |
"go/token" |
| 11 |
"os" |
| 12 |
"reflect" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"golang.org/x/tools/go/packages" |
| 16 |
"honnef.co/go/tools/internal/xtools-internal/astutil" |
| 17 |
"honnef.co/go/tools/internal/xtools-internal/testenv" |
| 18 |
) |
| 19 |
|
| 20 |
func TestPurgeFuncBodiesCases(t *testing.T) { |
| 21 |
tests := []struct { |
| 22 |
name string |
| 23 |
in string |
| 24 |
want string |
| 25 |
}{ |
| 26 |
{ |
| 27 |
name: "func decl", |
| 28 |
in: "package p\nfunc F(x int) string { return fmt.Sprint(x) }\n", |
| 29 |
want: "package p\nfunc F(x int) string {}\n", |
| 30 |
}, |
| 31 |
{ |
| 32 |
name: "method decl", |
| 33 |
in: "package p\nfunc (r R) M() int { return r.x }\n", |
| 34 |
want: "package p\nfunc (r R) M() int {}\n", |
| 35 |
}, |
| 36 |
{ |
| 37 |
name: "generic func decl", |
| 38 |
in: "package p\nfunc F[T any](x T) T { return x }\n", |
| 39 |
want: "package p\nfunc F[T any](x T) T {}\n", |
| 40 |
}, |
| 41 |
{ |
| 42 |
name: "no body (assembly)", |
| 43 |
in: "package p\nfunc F()\nfunc G() { x() }\n", |
| 44 |
want: "package p\nfunc F()\nfunc G() {}\n", |
| 45 |
}, |
| 46 |
// Result types containing braces: type literal preserved, |
| 47 |
// only the body is purged. |
| 48 |
{ |
| 49 |
name: "struct result type", |
| 50 |
in: "package p\nfunc F() struct{A int} { panic(0) }\n", |
| 51 |
want: "package p\nfunc F() struct{A int} {}\n", |
| 52 |
}, |
| 53 |
{ |
| 54 |
name: "interface result type", |
| 55 |
in: "package p\nfunc F() interface{M()} { return nil }\n", |
| 56 |
want: "package p\nfunc F() interface{M()} {}\n", |
| 57 |
}, |
| 58 |
{ |
| 59 |
name: "ptr struct result type", |
| 60 |
in: "package p\nfunc F() *struct{A int} { return nil }\n", |
| 61 |
want: "package p\nfunc F() *struct{A int} {}\n", |
| 62 |
}, |
| 63 |
{ |
| 64 |
name: "func type result", |
| 65 |
in: "package p\nfunc F() func() int { return nil }\n", |
| 66 |
want: "package p\nfunc F() func() int {}\n", |
| 67 |
}, |
| 68 |
// Length-elided array literals ([...]T) are preserved: the |
| 69 |
// element count is part of the type. (Nested elements are |
| 70 |
// preserved verbatim along with the outer body.) |
| 71 |
{ |
| 72 |
name: "auto-sized array", |
| 73 |
in: "package p\nvar X = [...]int{1, 2, 3}\n", |
| 74 |
want: "package p\nvar X = [...]int{1, 2, 3}\n", |
| 75 |
}, |
| 76 |
{ |
| 77 |
name: "auto-sized array with key", |
| 78 |
in: "package p\nvar X = [...]int{5: 0}\n", |
| 79 |
want: "package p\nvar X = [...]int{5: 0}\n", |
| 80 |
}, |
| 81 |
{ |
| 82 |
name: "auto-sized array of struct", |
| 83 |
in: "package p\nvar X = [...]struct{A int}{{1}, {2}}\n", |
| 84 |
want: "package p\nvar X = [...]struct{A int}{{1}, {2}}\n", |
| 85 |
}, |
| 86 |
{ |
| 87 |
name: "const from auto-sized array", |
| 88 |
in: "package p\nconst N = len([...]int{1, 2, 3})\n", |
| 89 |
want: "package p\nconst N = len([...]int{1, 2, 3})\n", |
| 90 |
}, |
| 91 |
{ |
| 92 |
name: "auto-sized array then unrelated literal", |
| 93 |
in: "package p\nvar X = [...]int{1}\nvar Y = T{2}\n", |
| 94 |
want: "package p\nvar X = [...]int{1}\nvar Y = T{}\n", |
| 95 |
}, |
| 96 |
// Other composite literals are purged: their contents don't |
| 97 |
// affect the type of the enclosing declaration. |
| 98 |
{ |
| 99 |
name: "slice composite literal", |
| 100 |
in: "package p\nvar X = []int{1, 2, 3}\n", |
| 101 |
want: "package p\nvar X = []int{}\n", |
| 102 |
}, |
| 103 |
{ |
| 104 |
name: "fixed-size array composite literal", |
| 105 |
in: "package p\nvar X = [3]int{1, 2, 3}\n", |
| 106 |
want: "package p\nvar X = [3]int{}\n", |
| 107 |
}, |
| 108 |
{ |
| 109 |
name: "map composite literal", |
| 110 |
in: "package p\nvar X = map[string]func(){\"a\": f}\n", |
| 111 |
want: "package p\nvar X = map[string]func(){}\n", |
| 112 |
}, |
| 113 |
{ |
| 114 |
name: "struct composite literal", |
| 115 |
in: "package p\nvar X = struct{Y int}{Y: 1}\n", |
| 116 |
want: "package p\nvar X = struct{Y int}{}\n", |
| 117 |
}, |
| 118 |
// Func-literal bodies are purged. |
| 119 |
{ |
| 120 |
name: "func literal in initializer", |
| 121 |
in: "package p\nvar F = func() int { return 1 }\n", |
| 122 |
want: "package p\nvar F = func() int {}\n", |
| 123 |
}, |
| 124 |
// Type-declaration bodies are preserved. |
| 125 |
{ |
| 126 |
name: "type decl struct body", |
| 127 |
in: "package p\ntype T struct{ X int }\n", |
| 128 |
want: "package p\ntype T struct{ X int }\n", |
| 129 |
}, |
| 130 |
// "..." in non-array contexts does not trigger preservation. |
| 131 |
{ |
| 132 |
name: "variadic param", |
| 133 |
in: "package p\nfunc F(x ...int) { y(x) }\n", |
| 134 |
want: "package p\nfunc F(x ...int) {}\n", |
| 135 |
}, |
| 136 |
} |
| 137 |
for _, tt := range tests { |
| 138 |
t.Run(tt.name, func(t *testing.T) { |
| 139 |
got := string(astutil.PurgeFuncBodies([]byte(tt.in))) |
| 140 |
if got != tt.want { |
| 141 |
t.Errorf("PurgeFuncBodies:\n in: %q\ngot: %q\nwant: %q", tt.in, got, tt.want) |
| 142 |
} |
| 143 |
}) |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
// TestPurgeFuncBodies tests PurgeFuncBodies by comparing it against a |
| 148 |
// (less efficient) reference implementation that purges after parsing. |
| 149 |
func TestPurgeFuncBodies(t *testing.T) { |
| 150 |
testenv.NeedsGoBuild(t) // we need the source code for std |
| 151 |
|
| 152 |
// Load a few standard packages. |
| 153 |
config := packages.Config{Mode: packages.NeedCompiledGoFiles} |
| 154 |
pkgs, err := packages.Load(&config, "encoding/...") |
| 155 |
if err != nil { |
| 156 |
t.Fatal(err) |
| 157 |
} |
| 158 |
|
| 159 |
// preorder returns the nodes of tree f in preorder. |
| 160 |
preorder := func(f *ast.File) (nodes []ast.Node) { |
| 161 |
ast.Inspect(f, func(n ast.Node) bool { |
| 162 |
if n != nil { |
| 163 |
nodes = append(nodes, n) |
| 164 |
} |
| 165 |
return true |
| 166 |
}) |
| 167 |
return nodes |
| 168 |
} |
| 169 |
|
| 170 |
packages.Visit(pkgs, nil, func(p *packages.Package) { |
| 171 |
for _, filename := range p.CompiledGoFiles { |
| 172 |
content, err := os.ReadFile(filename) |
| 173 |
if err != nil { |
| 174 |
t.Fatal(err) |
| 175 |
} |
| 176 |
|
| 177 |
fset := token.NewFileSet() |
| 178 |
|
| 179 |
// Parse then purge (reference implementation). |
| 180 |
f1, _ := parser.ParseFile(fset, filename, content, parser.SkipObjectResolution) |
| 181 |
ast.Inspect(f1, func(n ast.Node) bool { |
| 182 |
switch n := n.(type) { |
| 183 |
case *ast.FuncDecl: |
| 184 |
if n.Body != nil { |
| 185 |
n.Body.List = nil |
| 186 |
} |
| 187 |
case *ast.FuncLit: |
| 188 |
n.Body.List = nil |
| 189 |
case *ast.CompositeLit: |
| 190 |
if at, _ := n.Type.(*ast.ArrayType); at != nil { |
| 191 |
if _, ok := at.Len.(*ast.Ellipsis); ok { |
| 192 |
// [...]T literal: preserve verbatim |
| 193 |
// (don't recur, since nested elements |
| 194 |
// are preserved too). |
| 195 |
return false |
| 196 |
} |
| 197 |
} |
| 198 |
n.Elts = nil |
| 199 |
} |
| 200 |
return true |
| 201 |
}) |
| 202 |
|
| 203 |
// Purge before parse (logic under test). |
| 204 |
f2, _ := parser.ParseFile(fset, filename, astutil.PurgeFuncBodies(content), parser.SkipObjectResolution) |
| 205 |
|
| 206 |
// Compare sequence of node types. |
| 207 |
nodes1 := preorder(f1) |
| 208 |
nodes2 := preorder(f2) |
| 209 |
if len(nodes2) < len(nodes1) { |
| 210 |
t.Errorf("purged file has fewer nodes: %d vs %d", |
| 211 |
len(nodes2), len(nodes1)) |
| 212 |
nodes1 = nodes1[:len(nodes2)] // truncate |
| 213 |
} |
| 214 |
for i := range nodes1 { |
| 215 |
x, y := nodes1[i], nodes2[i] |
| 216 |
if reflect.TypeOf(x) != reflect.TypeOf(y) { |
| 217 |
t.Errorf("%s: got %T, want %T", |
| 218 |
fset.Position(x.Pos()), y, x) |
| 219 |
break |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
}) |
| 224 |
} |