Skip to content

Commit

Permalink
fix anonymous func arg names and name collision
Browse files Browse the repository at this point in the history
  • Loading branch information
sonalys committed Jun 10, 2024
1 parent 4e18161 commit 151e531
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 24 deletions.
24 changes: 5 additions & 19 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion generator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fake

import (
"os"
"path"
"testing"

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/caching/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/gomod/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/gosum/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 151e531

Please sign in to comment.