Skip to content

Commit

Permalink
feat: add capitalize option in substitution
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkato committed Apr 8, 2024
1 parent b1de4bf commit 231fac2
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 4 deletions.
8 changes: 5 additions & 3 deletions internal/check/substitution.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ type Substitution struct {
Definition `mapstructure:",squash"`
Exceptions []string
repl []string
POS string
Swap map[string]string
exceptRe *regexp2.Regexp
pattern *regexp2.Regexp
Ignorecase bool
Nonword bool
Vocab bool
Capitalize bool

// Deprecated
POS string
}

// NewSubstitution creates a new `substitution`-based rule.
Expand Down Expand Up @@ -129,7 +132,7 @@ func (s Substitution) Run(blk nlp.Block, _ *core.File) ([]core.Alert, error) {
if action.Name == "replace" && len(action.Params) == 0 {
action.Params = strings.Split(expected, "|")

if s.Ignorecase && observed == core.CapFirst(observed) {
if s.Capitalize && observed == core.CapFirst(observed) {
cased := []string{}
for _, param := range action.Params {
cased = append(cased, core.CapFirst(param))
Expand All @@ -138,7 +141,6 @@ func (s Substitution) Run(blk nlp.Block, _ *core.File) ([]core.Alert, error) {
}

expected = core.ToSentence(action.Params, "or")

// NOTE: For backwards-compatibility, we need to ensure
// that we don't double quote.
s.Message = convertMessage(s.Message)
Expand Down
3 changes: 3 additions & 0 deletions testdata/features/checks.feature
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ Feature: Checks
test.md:17:14:Bugs.URLCtx:Use 'Term' instead of 'term'
test.md:19:5:Bugs.KeepCase:Use 'mutual TLS' rather than Mutual TLS.
test.md:19:36:Bugs.KeepCase:Use 'vNIC' rather than VNIC.
test.md:21:1:Bugs.TermCase:Use 'JavaScript' rather than Javascript.
test.md:21:53:Bugs.TermCase:Use 'JavaScript' rather than javascript.
test.md:23:1:Bugs.TermCase:Use 'iOS' rather than IOS.
"""

Scenario: Sequence
Expand Down
2 changes: 1 addition & 1 deletion testdata/features/misc.feature
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Feature: Misc
When I test "misc/ids"
Then the output should contain exactly:
"""
test.adoc:7:1:Markup.ID:Use 'Leader' rather than 'Master'.
test.adoc:7:1:Markup.ID:Use 'leader' rather than 'Master'.
test.adoc:12:1:Markup.ID:Use 'leader' rather than 'master'.
test.adoc:14:55:Markup.ID:Use 'leader' rather than 'master'.
"""
Expand Down
1 change: 1 addition & 0 deletions testdata/fixtures/checks/Substitution/.vale.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ Bugs.Newline = YES
Bugs.URLCtx = YES
Bugs.MatchCase = YES
Bugs.KeepCase = YES
Bugs.TermCase = YES
4 changes: 4 additions & 0 deletions testdata/fixtures/checks/Substitution/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ The innovation was good for mankind.
Mankind is a term.

The Mutual TLS is a good practice. VNIC is another word.

Javascript is a programming language. I developed a javascript application.

IOS is a mobile operating system.
1 change: 1 addition & 0 deletions testdata/styles/Bugs/MatchCase.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extends: substitution
message: "Consider using '%s' instead of '%s'."
ignorecase: true
capitalize: true
level: error
action:
name: replace
Expand Down
10 changes: 10 additions & 0 deletions testdata/styles/Bugs/TermCase.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
extends: substitution
ignorecase: true
level: error
message: "Use %s rather than %s."
action:
name: replace
swap:
javascript: JavaScript
ios: iOS

0 comments on commit 231fac2

Please sign in to comment.