-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds the ability to spellcheck commits. It supports US, UK, and GB locales. Signed-off-by: Andrew Rynhard <[email protected]>
- Loading branch information
1 parent
8726189
commit b3b6e65
Showing
6 changed files
with
88 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package commit | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/talos-systems/conform/internal/policy" | ||
|
||
"github.com/golangci/misspell" | ||
) | ||
|
||
// SpellCheck represents to spell check policy. | ||
type SpellCheck struct { | ||
Locale string `mapstructure:"locale"` | ||
} | ||
|
||
// SpellingCheck enforces correct spelling. | ||
type SpellingCheck struct { | ||
errors []error | ||
} | ||
|
||
// Name returns the name of the check. | ||
func (h SpellingCheck) Name() string { | ||
return "Spellcheck" | ||
} | ||
|
||
// Message returns to check message. | ||
func (h SpellingCheck) Message() string { | ||
return fmt.Sprintf("Commit contains %d misspellings", len(h.errors)) | ||
} | ||
|
||
// Errors returns any violations of the check. | ||
func (h SpellingCheck) Errors() []error { | ||
return h.errors | ||
} | ||
|
||
// ValidateSpelling checks the spelling. | ||
func (c Commit) ValidateSpelling() policy.Check { | ||
check := &SpellingCheck{} | ||
|
||
r := misspell.Replacer{ | ||
Replacements: misspell.DictMain, | ||
} | ||
|
||
switch strings.ToUpper(c.SpellCheck.Locale) { | ||
case "": | ||
case "US": | ||
r.AddRuleList(misspell.DictAmerican) | ||
case "UK", "GB": | ||
r.AddRuleList(misspell.DictBritish) | ||
case "NZ", "AU", "CA": | ||
check.errors = append(check.errors, fmt.Errorf("unknown locale: %q", c.SpellCheck.Locale)) | ||
} | ||
|
||
r.Compile() | ||
|
||
_, diffs := r.Replace(c.msg) | ||
|
||
for _, diff := range diffs { | ||
check.errors = append(check.errors, fmt.Errorf("`%s` is a misspelling of `%s`", diff.Original, diff.Corrected)) | ||
} | ||
|
||
return check | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters