Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/wire: replace keyword list with token api #203

Merged
merged 1 commit into from Jul 16, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 3 additions & 33 deletions internal/wire/wire.go
Expand Up @@ -845,7 +845,7 @@ func typeVariableName(t types.Type, defaultName string, transform func(string) s

// See if there's an unambiguous name; if so, use it.
for _, name := range names {
if !reservedKeyword[name] && !collides(name) {
if !token.Lookup(name).IsKeyword() && !collides(name) {
return name
}
}
Expand Down Expand Up @@ -903,40 +903,10 @@ func export(name string) string {
return sbuf.String()
}

// reservedKeyword is a set of Go's reserved keywords:
// https://golang.org/ref/spec#Keywords
var reservedKeyword = map[string]bool{
"break": true,
"case": true,
"chan": true,
"const": true,
"continue": true,
"default": true,
"defer": true,
"else": true,
"fallthrough": true,
"for": true,
"func": true,
"go": true,
"goto": true,
"if": true,
"import": true,
"interface": true,
"map": true,
"package": true,
"range": true,
"return": true,
"select": true,
"struct": true,
"switch": true,
"type": true,
"var": true,
}

// disambiguate picks a unique name, preferring name if it is already unique.
// It also disambiguates against Go's reserved keywords.
func disambiguate(name string, collides func(string) bool) string {
if !reservedKeyword[name] && !collides(name) {
if !token.Lookup(name).IsKeyword() && !collides(name) {
return name
}
buf := []byte(name)
Expand All @@ -947,7 +917,7 @@ func disambiguate(name string, collides func(string) bool) string {
for n := 2; ; n++ {
buf = strconv.AppendInt(buf[:base], int64(n), 10)
sbuf := string(buf)
if !reservedKeyword[sbuf] && !collides(sbuf) {
if !token.Lookup(sbuf).IsKeyword() && !collides(sbuf) {
return sbuf
}
}
Expand Down