diff --git a/identifier_typo.go b/identifier_typo.go index faa95bd..3a2507b 100644 --- a/identifier_typo.go +++ b/identifier_typo.go @@ -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 @@ -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()) diff --git a/identifier_typo_test.go b/identifier_typo_test.go index e994422..1230c58 100644 --- a/identifier_typo_test.go +++ b/identifier_typo_test.go @@ -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{