Skip to content

Commit

Permalink
fix: alias can't have a first digit
Browse files Browse the repository at this point in the history
  • Loading branch information
zoumo committed Aug 28, 2020
1 parent ef3c850 commit c4ea171
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/dave/jennifer

go 1.15
7 changes: 7 additions & 0 deletions jen/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"regexp"
"strings"
"unicode"
"unicode/utf8"
)

// NewFile Creates a new file, with the specified package name.
Expand Down Expand Up @@ -240,5 +242,10 @@ func guessAlias(path string) string {
importsRegex := regexp.MustCompile(`[^a-z0-9]`)
alias = importsRegex.ReplaceAllString(alias, "")

// can't have a first digit, per Go identifier rules, so just skip them
for firstRune, runeLen := utf8.DecodeRuneInString(alias); unicode.IsDigit(firstRune); firstRune, runeLen = utf8.DecodeRuneInString(alias) {
alias = alias[runeLen:]
}

return alias
}
2 changes: 2 additions & 0 deletions jen/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func TestGuessAlias(t *testing.T) {
"a/b-c.d": "bcd",
"a/bb-ccc.dddd": "bbcccdddd",
"a/foo-go": "foogo",
"123a": "a",
"a/321a.b": "ab",
}
for path, expected := range data {
if guessAlias(path) != expected {
Expand Down

0 comments on commit c4ea171

Please sign in to comment.