gopls/internal/golang: unexport more declarations

I missed a few in my previous CL:
- FindParam
- ParamInfo and its fields
- TestFn
- TestFns (eliminated entirely)

Change-Id: Ib8dabba73e679be5842bf1af359db80157446993
Reviewed-on: https://go-review.googlesource.com/c/tools/+/587932
Auto-Submit: Alan Donovan <adonovan@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Alan Donovan 2024-05-24 17:27:53 -04:00 коммит произвёл Gopher Robot
Родитель 7045d2e410
Коммит e635bfa66b
3 изменённых файлов: 71 добавлений и 85 удалений

Просмотреть файл

@ -58,30 +58,30 @@ func RemoveUnusedParameter(ctx context.Context, fh file.Handle, rng protocol.Ran
return nil, fmt.Errorf("can't change signatures for packages with parse or type errors: (e.g. %s)", sample)
}
info, err := FindParam(pgf, rng)
info, err := findParam(pgf, rng)
if err != nil {
return nil, err // e.g. invalid range
}
if info.Field == nil {
if info.field == nil {
return nil, fmt.Errorf("failed to find field")
}
// Create the new declaration, which is a copy of the original decl with the
// unnecessary parameter removed.
newDecl := internalastutil.CloneNode(info.Decl)
if info.Name != nil {
names := remove(newDecl.Type.Params.List[info.FieldIndex].Names, info.NameIndex)
newDecl.Type.Params.List[info.FieldIndex].Names = names
newDecl := internalastutil.CloneNode(info.decl)
if info.name != nil {
names := remove(newDecl.Type.Params.List[info.fieldIndex].Names, info.nameIndex)
newDecl.Type.Params.List[info.fieldIndex].Names = names
}
if len(newDecl.Type.Params.List[info.FieldIndex].Names) == 0 {
if len(newDecl.Type.Params.List[info.fieldIndex].Names) == 0 {
// Unnamed, or final name was removed: in either case, remove the field.
newDecl.Type.Params.List = remove(newDecl.Type.Params.List, info.FieldIndex)
newDecl.Type.Params.List = remove(newDecl.Type.Params.List, info.fieldIndex)
}
// Compute inputs into building a wrapper function around the modified
// signature.
var (
params = internalastutil.CloneNode(info.Decl.Type.Params) // "_" names will be modified
params = internalastutil.CloneNode(info.decl.Type.Params) // "_" names will be modified
args []ast.Expr // arguments to delegate
variadic = false // whether the signature is variadic
)
@ -97,7 +97,7 @@ func RemoveUnusedParameter(ctx context.Context, fh file.Handle, rng protocol.Ran
blanks := 0
for i, fld := range params.List {
for j, n := range fld.Names {
if i == info.FieldIndex && j == info.NameIndex {
if i == info.fieldIndex && j == info.nameIndex {
continue
}
if n.Name == "_" {
@ -125,7 +125,7 @@ func RemoveUnusedParameter(ctx context.Context, fh file.Handle, rng protocol.Ran
snapshot: snapshot,
pkg: pkg,
pgf: pgf,
origDecl: info.Decl,
origDecl: info.decl,
newDecl: newDecl,
params: params,
callArgs: args,
@ -140,7 +140,7 @@ func RemoveUnusedParameter(ctx context.Context, fh file.Handle, rng protocol.Ran
// of the inlining should have changed the location of the original
// declaration.
{
idx := findDecl(pgf.File, info.Decl)
idx := findDecl(pgf.File, info.decl)
if idx < 0 {
return nil, bug.Errorf("didn't find original decl")
}
@ -237,17 +237,17 @@ func rewriteSignature(fset *token.FileSet, declIdx int, src0 []byte, newDecl *as
return newSrc, nil
}
// ParamInfo records information about a param identified by a position.
type ParamInfo struct {
Decl *ast.FuncDecl // enclosing func decl (non-nil)
FieldIndex int // index of Field in Decl.Type.Params, or -1
Field *ast.Field // enclosing field of Decl, or nil if range not among parameters
NameIndex int // index of Name in Field.Names, or nil
Name *ast.Ident // indicated name (either enclosing, or Field.Names[0] if len(Field.Names) == 1)
// paramInfo records information about a param identified by a position.
type paramInfo struct {
decl *ast.FuncDecl // enclosing func decl (non-nil)
fieldIndex int // index of Field in Decl.Type.Params, or -1
field *ast.Field // enclosing field of Decl, or nil if range not among parameters
nameIndex int // index of Name in Field.Names, or nil
name *ast.Ident // indicated name (either enclosing, or Field.Names[0] if len(Field.Names) == 1)
}
// FindParam finds the parameter information spanned by the given range.
func FindParam(pgf *parsego.File, rng protocol.Range) (*ParamInfo, error) {
// findParam finds the parameter information spanned by the given range.
func findParam(pgf *parsego.File, rng protocol.Range) (*paramInfo, error) {
start, end, err := pgf.RangePos(rng)
if err != nil {
return nil, err
@ -275,25 +275,25 @@ func FindParam(pgf *parsego.File, rng protocol.Range) (*ParamInfo, error) {
if decl == nil {
return nil, fmt.Errorf("range is not within a function declaration")
}
info := &ParamInfo{
FieldIndex: -1,
NameIndex: -1,
Decl: decl,
info := &paramInfo{
fieldIndex: -1,
nameIndex: -1,
decl: decl,
}
for fi, f := range decl.Type.Params.List {
if f == field {
info.FieldIndex = fi
info.Field = f
info.fieldIndex = fi
info.field = f
for ni, n := range f.Names {
if n == id {
info.NameIndex = ni
info.Name = n
info.nameIndex = ni
info.name = n
break
}
}
if info.Name == nil && len(info.Field.Names) == 1 {
info.NameIndex = 0
info.Name = info.Field.Names[0]
if info.name == nil && len(info.field.Names) == 1 {
info.nameIndex = 0
info.name = info.field.Names[0]
}
break
}

Просмотреть файл

@ -41,30 +41,30 @@ func runTestCodeLens(ctx context.Context, snapshot *cache.Snapshot, fh file.Hand
if err != nil {
return nil, err
}
fns, err := testsAndBenchmarks(pkg, pgf)
testFuncs, benchFuncs, err := testsAndBenchmarks(pkg.TypesInfo(), pgf)
if err != nil {
return nil, err
}
puri := fh.URI()
for _, fn := range fns.Tests {
cmd, err := command.NewTestCommand("run test", puri, []string{fn.Name}, nil)
for _, fn := range testFuncs {
cmd, err := command.NewTestCommand("run test", puri, []string{fn.name}, nil)
if err != nil {
return nil, err
}
rng := protocol.Range{Start: fn.Rng.Start, End: fn.Rng.Start}
rng := protocol.Range{Start: fn.rng.Start, End: fn.rng.Start}
codeLens = append(codeLens, protocol.CodeLens{Range: rng, Command: &cmd})
}
for _, fn := range fns.Benchmarks {
cmd, err := command.NewTestCommand("run benchmark", puri, nil, []string{fn.Name})
for _, fn := range benchFuncs {
cmd, err := command.NewTestCommand("run benchmark", puri, nil, []string{fn.name})
if err != nil {
return nil, err
}
rng := protocol.Range{Start: fn.Rng.Start, End: fn.Rng.Start}
rng := protocol.Range{Start: fn.rng.Start, End: fn.rng.Start}
codeLens = append(codeLens, protocol.CodeLens{Range: rng, Command: &cmd})
}
if len(fns.Benchmarks) > 0 {
if len(benchFuncs) > 0 {
pgf, err := snapshot.ParseGo(ctx, fh, parsego.Full)
if err != nil {
return nil, err
@ -75,8 +75,8 @@ func runTestCodeLens(ctx context.Context, snapshot *cache.Snapshot, fh file.Hand
return nil, err
}
var benches []string
for _, fn := range fns.Benchmarks {
benches = append(benches, fn.Name)
for _, fn := range benchFuncs {
benches = append(benches, fn.name)
}
cmd, err := command.NewTestCommand("run file benchmarks", puri, nil, benches)
if err != nil {
@ -87,21 +87,16 @@ func runTestCodeLens(ctx context.Context, snapshot *cache.Snapshot, fh file.Hand
return codeLens, nil
}
type TestFn struct {
Name string
Rng protocol.Range
type testFunc struct {
name string
rng protocol.Range // of *ast.FuncDecl
}
type TestFns struct {
Tests []TestFn
Benchmarks []TestFn
}
func testsAndBenchmarks(pkg *cache.Package, pgf *parsego.File) (TestFns, error) {
var out TestFns
// testsAndBenchmarks returns all Test and Benchmark functions in the
// specified file.
func testsAndBenchmarks(info *types.Info, pgf *parsego.File) (tests, benchmarks []testFunc, _ error) {
if !strings.HasSuffix(pgf.URI.Path(), "_test.go") {
return out, nil
return nil, nil, nil // empty
}
for _, d := range pgf.File.Decls {
@ -112,30 +107,23 @@ func testsAndBenchmarks(pkg *cache.Package, pgf *parsego.File) (TestFns, error)
rng, err := pgf.NodeRange(fn)
if err != nil {
return out, err
return nil, nil, err
}
if matchTestFunc(fn, pkg, testRe, "T") {
out.Tests = append(out.Tests, TestFn{fn.Name.Name, rng})
}
if matchTestFunc(fn, pkg, benchmarkRe, "B") {
out.Benchmarks = append(out.Benchmarks, TestFn{fn.Name.Name, rng})
if matchTestFunc(fn, info, testRe, "T") {
tests = append(tests, testFunc{fn.Name.Name, rng})
} else if matchTestFunc(fn, info, benchmarkRe, "B") {
benchmarks = append(benchmarks, testFunc{fn.Name.Name, rng})
}
}
return out, nil
return
}
func matchTestFunc(fn *ast.FuncDecl, pkg *cache.Package, nameRe *regexp.Regexp, paramID string) bool {
func matchTestFunc(fn *ast.FuncDecl, info *types.Info, nameRe *regexp.Regexp, paramID string) bool {
// Make sure that the function name matches a test function.
if !nameRe.MatchString(fn.Name.Name) {
return false
}
info := pkg.TypesInfo()
if info == nil {
return false
}
obj, ok := info.ObjectOf(fn.Name).(*types.Func)
if !ok {
return false

Просмотреть файл

@ -416,33 +416,33 @@ func canRemoveParameter(pkg *cache.Package, pgf *parsego.File, rng protocol.Rang
if perrors, terrors := pkg.ParseErrors(), pkg.TypeErrors(); len(perrors) > 0 || len(terrors) > 0 {
return false // can't remove parameters from packages with errors
}
info, err := FindParam(pgf, rng)
info, err := findParam(pgf, rng)
if err != nil {
return false // e.g. invalid range
}
if info.Field == nil {
if info.field == nil {
return false // range does not span a parameter
}
if info.Decl.Body == nil {
if info.decl.Body == nil {
return false // external function
}
if len(info.Field.Names) == 0 {
if len(info.field.Names) == 0 {
return true // no names => field is unused
}
if info.Name == nil {
if info.name == nil {
return false // no name is indicated
}
if info.Name.Name == "_" {
if info.name.Name == "_" {
return true // trivially unused
}
obj := pkg.TypesInfo().Defs[info.Name]
obj := pkg.TypesInfo().Defs[info.name]
if obj == nil {
return false // something went wrong
}
used := false
ast.Inspect(info.Decl.Body, func(node ast.Node) bool {
ast.Inspect(info.decl.Body, func(node ast.Node) bool {
if n, ok := node.(*ast.Ident); ok && pkg.TypesInfo().Uses[n] == obj {
used = true
}
@ -483,23 +483,21 @@ func getInlineCodeActions(pkg *cache.Package, pgf *parsego.File, rng protocol.Ra
// getGoTestCodeActions returns any "run this test/benchmark" code actions for the selection.
func getGoTestCodeActions(pkg *cache.Package, pgf *parsego.File, rng protocol.Range) ([]protocol.CodeAction, error) {
fns, err := testsAndBenchmarks(pkg, pgf)
testFuncs, benchFuncs, err := testsAndBenchmarks(pkg.TypesInfo(), pgf)
if err != nil {
return nil, err
}
var tests, benchmarks []string
for _, fn := range fns.Tests {
if !protocol.Intersect(fn.Rng, rng) {
continue
for _, fn := range testFuncs {
if protocol.Intersect(fn.rng, rng) {
tests = append(tests, fn.name)
}
tests = append(tests, fn.Name)
}
for _, fn := range fns.Benchmarks {
if !protocol.Intersect(fn.Rng, rng) {
continue
for _, fn := range benchFuncs {
if protocol.Intersect(fn.rng, rng) {
benchmarks = append(benchmarks, fn.name)
}
benchmarks = append(benchmarks, fn.Name)
}
if len(tests) == 0 && len(benchmarks) == 0 {