Skip to content

Commit

Permalink
Merge pull request #2 from jackwilsdon/hyphenated-camelCase
Browse files Browse the repository at this point in the history
Change hyphenated corrections to camelCase
  • Loading branch information
Alex Kohler authored Sep 15, 2018
2 parents 51cf163 + d8ffa54 commit dcb8a79
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
29 changes: 29 additions & 0 deletions identifier_typo.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,31 @@ func CheckForIdentiferTypos(args []string, flags Flags) error {
return processIdentifiers(fset, files, flags)
}

// hyphenToCamelCase converts a hyphenated word into camelCase.
// This method preserves any capitalisation of the original input text.
// Example: all-time -> allTime
func hyphenToCamelCase(s string) string {
words := strings.Split(s, "-")

// if there's only one word then there's nothing to do (i.e. it's not hyphenated)
if len(words) <= 1 {
return s
}

r := strings.Builder{}

for i, p := range words {
// we don't want to convert the first word to upper case
if i > 0 {
p = strings.Title(p)
}

r.WriteString(p)
}

return r.String()
}

func processIdentifiers(fset *token.FileSet, files []*ast.File, flags Flags) error {
all := !flags.FunctionsOnly && !flags.ConstantsOnly && !flags.VariablesOnly

Expand Down Expand Up @@ -70,6 +95,10 @@ func processIdentifiers(fset *token.FileSet, files []*ast.File, flags Flags) err
for _, ident := range retVis.identifiers {
for _, word := range camelcase.Split(ident.Name) {
v, d := retVis.replacer.Replace(word)

// convert any hyphenated words into camelCase
v = hyphenToCamelCase(v)

if len(d) > 0 {
exitStatus = 1
file := retVis.f.File(ident.Pos())
Expand Down
32 changes: 32 additions & 0 deletions identifier_typo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,38 @@ func Test_processIdentifiers(t *testing.T) {
},
},
},
{name: "misspelled function with no receiver which is corrected by hyphenation",
args: args{
testFiles: []*testFile{
{
src: `package main
func Alltime() {
}`,
name: "file.go",
wantLogs: []string{"file.go:2 \"Alltime\" should be AllTime in Alltime\n"},
},
},
flags: Flags{
Ignores: "",
},
},
},
{name: "misspelled (unexported) function with no receiver which is corrected by hyphenation",
args: args{
testFiles: []*testFile{
{
src: `package main
func alltime() {
}`,
name: "file.go",
wantLogs: []string{"file.go:2 \"alltime\" should be allTime in alltime\n"},
},
},
flags: Flags{
Ignores: "",
},
},
},
{name: "single misspelled function matching ignore",
args: args{
testFiles: []*testFile{
Expand Down

0 comments on commit dcb8a79

Please sign in to comment.