From 151e5310406a0437e2eaf4abbd1354fbadc70ab4 Mon Sep 17 00:00:00 2001 From: Alysson Ribeiro <15274059+sonalys@users.noreply.github.com> Date: Mon, 10 Jun 2024 08:55:38 +0200 Subject: [PATCH] fix anonymous func arg names and name collision --- field.go | 24 +++++------------------- generator_test.go | 3 ++- interface.go | 2 +- internal/caching/hash.go | 2 +- internal/gomod/parser.go | 2 +- internal/gosum/parser.go | 2 +- 6 files changed, 11 insertions(+), 24 deletions(-) diff --git a/field.go b/field.go index c436a4b..3584629 100644 --- a/field.go +++ b/field.go @@ -14,36 +14,22 @@ type ParsedField struct { Type string } -func getFieldName(i int, field *ast.Field) string { - if len(field.Names) == 0 { - return fmt.Sprintf("a%d", i) - } - names := make([]string, 0, len(field.Names)) - for _, name := range field.Names { - names = append(names, name.Name) - } - return strings.Join(names, ", ") +func getFieldName(i int) string { + return fmt.Sprintf("a%d", i) } func getFieldCallingName(i int, field *ast.Field) string { - if len(field.Names) == 0 { - return fmt.Sprintf("a%d", i) - } switch field.Type.(type) { case *ast.Ellipsis: - return fmt.Sprintf("%s...", getFieldName(i, field)) - } - names := make([]string, 0, len(field.Names)) - for _, name := range field.Names { - names = append(names, name.Name) + return fmt.Sprintf("%s...", getFieldName(i)) } - return strings.Join(names, ",") + return getFieldName(i) } func (f *ParsedInterface) PrintAstField(i int, field *ast.Field, printName bool) string { typeName := f.printAstExpr(field.Type) if printName { - return fmt.Sprintf("%s %s", getFieldName(i, field), typeName) + return fmt.Sprintf("%s %s", getFieldName(i), typeName) } return typeName } diff --git a/generator_test.go b/generator_test.go index 55c08bd..ad13166 100644 --- a/generator_test.go +++ b/generator_test.go @@ -1,6 +1,7 @@ package fake import ( + "os" "path" "testing" @@ -10,7 +11,7 @@ import ( func Test_Generate(t *testing.T) { output := t.TempDir() // output := "out" - // os.RemoveAll(output) // no caching + os.RemoveAll(output) // no caching Run([]string{"testdata"}, output, nil) g, err := NewGenerator("mocks", "testdata") require.NoError(t, err) diff --git a/interface.go b/interface.go index 1a10cc4..ebc1ba8 100644 --- a/interface.go +++ b/interface.go @@ -255,7 +255,7 @@ func (i *ParsedInterface) writeMethod(w io.Writer, methodName string, f *ParsedF var argNames []string var argFlag []string for i := range funcType.Params.List { - argNames = append(argNames, getFieldName(i, funcType.Params.List[i])) + argNames = append(argNames, getFieldName(i)) nameLen := len(funcType.Params.List[i].Names) if nameLen == 0 { nameLen = 1 diff --git a/internal/caching/hash.go b/internal/caching/hash.go index 52605d5..6e3bc2a 100644 --- a/internal/caching/hash.go +++ b/internal/caching/hash.go @@ -47,7 +47,7 @@ func GetUncachedFiles(inputs, ignore []string, outputDir string) (map[string]Loc } dependencies, err := gosum.Parse(inputs[0]) if err != nil { - return nil, fmt.Errorf("parsing go.sum file: %w", err) + return nil, fmt.Errorf("parsing go.sum file on %s: %w", inputs[0], err) } goFiles, err := files.ListGoFiles(inputs, append(ignore, outputDir)) if err != nil { diff --git a/internal/gomod/parser.go b/internal/gomod/parser.go index 98a1ca6..c21ea69 100644 --- a/internal/gomod/parser.go +++ b/internal/gomod/parser.go @@ -12,7 +12,7 @@ import ( func Parse(dir string) (map[string]string, error) { goModPath, err := files.FindFile(dir, "go.mod") if err != nil { - return nil, fmt.Errorf("could not find go.sum: %w", err) + return nil, fmt.Errorf("could not find go.mod: %w", err) } f, err := os.Open(goModPath) if err != nil { diff --git a/internal/gosum/parser.go b/internal/gosum/parser.go index 3b62a00..2d1f69a 100644 --- a/internal/gosum/parser.go +++ b/internal/gosum/parser.go @@ -39,7 +39,7 @@ func readGoSum(path string, goMod map[string]string) (map[string]string, error) func Parse(dir string) (map[string]string, error) { goMod, err := gomod.Parse(dir) if err != nil { - return nil, fmt.Errorf("could not read go.mod: %w", err) + return nil, fmt.Errorf("could not parse go.mod: %w", err) } goSumPath, err := files.FindFile(dir, "go.sum") if err != nil {