From 0cca585f33f92399cb8f53605d3d49388a5cd235 Mon Sep 17 00:00:00 2001 From: Pavol Vargovcik Date: Wed, 11 Mar 2020 14:01:04 +0100 Subject: [PATCH] fix: detect identifiers in comments using regex, not strings.Fields --- parse/parse.go | 9 +++++---- parse/parse_test.go | 6 ++++++ parse/test/bugreports/comment_generic.go | 8 ++++++++ parse/test/bugreports/comment_int.go | 8 ++++++++ 4 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 parse/test/bugreports/comment_generic.go create mode 100644 parse/test/bugreports/comment_int.go diff --git a/parse/parse.go b/parse/parse.go index 0e06941..a8685ec 100644 --- a/parse/parse.go +++ b/parse/parse.go @@ -10,6 +10,7 @@ import ( "go/token" "io" "os" + "regexp" "strings" "unicode" @@ -55,11 +56,11 @@ func subIntoLiteral(lit, typeTemplate, specificType string) string { } func subTypeIntoComment(line, typeTemplate, specificType string) string { - var subbed string - for _, w := range strings.Fields(line) { - subbed = subbed + subIntoLiteral(w, typeTemplate, specificType) + " " + re := regexp.MustCompile(`[a-zA-Z_][a-zA-Z0-9_]*`) + f := func(lit string) string { + return subIntoLiteral(lit, typeTemplate, specificType) } - return subbed + return re.ReplaceAllStringFunc(line, f) } // Does the heavy lifting of taking a line of our code and diff --git a/parse/parse_test.go b/parse/parse_test.go index 3414887..daad098 100644 --- a/parse/parse_test.go +++ b/parse/parse_test.go @@ -110,6 +110,12 @@ var tests = []struct { types: []map[string]string{{"SomeThing": "string"}}, expectedOut: `test/bugreports/negation_string.go`, }, + { + filename: "generic_comment.go", + in: `test/bugreports/comment_generic.go`, + types: []map[string]string{{"SomeThing": "int"}}, + expectedOut: `test/bugreports/comment_int.go`, + }, } func TestParse(t *testing.T) { diff --git a/parse/test/bugreports/comment_generic.go b/parse/test/bugreports/comment_generic.go new file mode 100644 index 0000000..c26b31c --- /dev/null +++ b/parse/test/bugreports/comment_generic.go @@ -0,0 +1,8 @@ +package bugreports + +import "github.com/cheekybits/genny/generic" + +type SomeThing generic.Type + +// foo=SomeThing,OtherThing +func foo(x SomeThing) {} diff --git a/parse/test/bugreports/comment_int.go b/parse/test/bugreports/comment_int.go new file mode 100644 index 0000000..05983d2 --- /dev/null +++ b/parse/test/bugreports/comment_int.go @@ -0,0 +1,8 @@ +// This file was automatically generated by genny. +// Any changes will be lost if this file is regenerated. +// see https://github.com/cheekybits/genny + +package bugreports + +// foo=int,OtherThing +func foo(x int) {}