Skip to content

Commit

Permalink
fix: detect identifiers in comments using regex, not strings.Fields
Browse files Browse the repository at this point in the history
  • Loading branch information
p4l1ly committed Mar 11, 2020
1 parent df3d48a commit 0cca585
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
9 changes: 5 additions & 4 deletions parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"go/token"
"io"
"os"
"regexp"
"strings"
"unicode"

Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions parse/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions parse/test/bugreports/comment_generic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package bugreports

import "github.com/cheekybits/genny/generic"

type SomeThing generic.Type

// foo=SomeThing,OtherThing
func foo(x SomeThing) {}
8 changes: 8 additions & 0 deletions parse/test/bugreports/comment_int.go
Original file line number Diff line number Diff line change
@@ -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) {}

0 comments on commit 0cca585

Please sign in to comment.