Skip to content

Commit

Permalink
Add replacements for non-capital letters in sentences.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krivak committed Nov 2, 2020
1 parent ac3f37d commit cc1247e
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 58 deletions.
25 changes: 19 additions & 6 deletions checks.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package godot

import (
"fmt"
"go/token"
"regexp"
"strings"
Expand Down Expand Up @@ -88,9 +87,12 @@ func checkCommentForPeriod(fset *token.FileSet, c comment) *Issue {
// attached lines. Use `iss.Pos.Column` because it's a position in
// the original line.
original := []rune(c.lines[pos.line-1])
iss.Replacement = fmt.Sprintf("%s.%s",
string(original[:iss.Pos.Column-1]),
string(original[iss.Pos.Column-1:]))
iss.Replacement = string(original[:iss.Pos.Column-1]) + "." +
string(original[iss.Pos.Column-1:])

// Save replacement to raw lines to be able to combine it with
// further replacements
c.lines[pos.line-1] = iss.Replacement

return &iss
}
Expand All @@ -117,7 +119,7 @@ func checkCommentForCapital(fset *token.FileSet, c comment) []Issue {
pos.column += 2
}

issues[i] = Issue{
iss := Issue{
Pos: token.Position{
Filename: start.Filename,
Offset: start.Offset,
Expand All @@ -127,7 +129,18 @@ func checkCommentForCapital(fset *token.FileSet, c comment) []Issue {
Message: noCapitalMessage,
}

// TODO: Make a replacement
// Make a replacement. Use `pos.line` to get an original line from
// attached lines. Use `iss.Pos.Column` because it's a position in
// the original line.
rep := []rune(c.lines[pos.line-1])
rep[iss.Pos.Column-1] = unicode.ToTitle(rep[iss.Pos.Column-1])
iss.Replacement = string(rep)

// Save replacement to raw lines to be able to combine it with
// further replacements
c.lines[pos.line-1] = iss.Replacement

issues[i] = iss
}

return issues
Expand Down
153 changes: 153 additions & 0 deletions coverage.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@

mode: atomic
github.com/tetafro/godot/checks.go:35.88,37.29 2 10
github.com/tetafro/godot/checks.go:54.2,54.15 1 10
github.com/tetafro/godot/checks.go:37.29,38.43 1 209
github.com/tetafro/godot/checks.go:42.3,42.22 1 207
github.com/tetafro/godot/checks.go:48.3,48.23 1 207
github.com/tetafro/godot/checks.go:38.43,39.12 1 2
github.com/tetafro/godot/checks.go:42.22,43.57 1 207
github.com/tetafro/godot/checks.go:43.57,45.5 1 93
github.com/tetafro/godot/checks.go:48.23,49.60 1 207
github.com/tetafro/godot/checks.go:49.60,51.5 1 21
github.com/tetafro/godot/checks.go:59.67,66.8 4 207
github.com/tetafro/godot/checks.go:71.2,72.44 2 93
github.com/tetafro/godot/checks.go:76.2,97.13 5 93
github.com/tetafro/godot/checks.go:66.8,68.3 1 114
github.com/tetafro/godot/checks.go:72.44,74.3 1 87
github.com/tetafro/godot/checks.go:103.69,110.18 4 207
github.com/tetafro/godot/checks.go:114.2,115.25 2 21
github.com/tetafro/godot/checks.go:146.2,146.15 1 21
github.com/tetafro/godot/checks.go:110.18,112.3 1 186
github.com/tetafro/godot/checks.go:115.25,118.45 2 21
github.com/tetafro/godot/checks.go:122.3,143.18 6 21
github.com/tetafro/godot/checks.go:118.45,120.4 1 15
github.com/tetafro/godot/checks.go:152.58,157.39 4 226
github.com/tetafro/godot/checks.go:167.2,167.12 1 226
github.com/tetafro/godot/checks.go:171.2,171.32 1 185
github.com/tetafro/godot/checks.go:175.2,176.19 2 98
github.com/tetafro/godot/checks.go:157.39,159.17 2 288
github.com/tetafro/godot/checks.go:162.3,164.8 3 185
github.com/tetafro/godot/checks.go:159.17,160.12 1 103
github.com/tetafro/godot/checks.go:167.12,169.3 1 41
github.com/tetafro/godot/checks.go:171.32,173.3 1 87
github.com/tetafro/godot/checks.go:183.67,191.15 4 213
github.com/tetafro/godot/checks.go:194.2,194.28 1 213
github.com/tetafro/godot/checks.go:224.2,224.11 1 213
github.com/tetafro/godot/checks.go:191.15,193.3 1 100
github.com/tetafro/godot/checks.go:194.28,198.16 3 8377
github.com/tetafro/godot/checks.go:206.3,206.39 1 8265
github.com/tetafro/godot/checks.go:210.3,210.35 1 8167
github.com/tetafro/godot/checks.go:213.3,213.15 1 8158
github.com/tetafro/godot/checks.go:219.3,219.51 1 7136
github.com/tetafro/godot/checks.go:222.3,222.16 1 7136
github.com/tetafro/godot/checks.go:198.16,201.24 3 112
github.com/tetafro/godot/checks.go:204.4,204.12 1 112
github.com/tetafro/godot/checks.go:201.24,203.5 1 14
github.com/tetafro/godot/checks.go:206.39,208.12 2 98
github.com/tetafro/godot/checks.go:210.35,211.12 1 9
github.com/tetafro/godot/checks.go:213.15,214.24 1 1022
github.com/tetafro/godot/checks.go:217.4,217.12 1 1022
github.com/tetafro/godot/checks.go:214.24,216.5 1 31
github.com/tetafro/godot/checks.go:219.51,221.4 1 31
github.com/tetafro/godot/checks.go:229.42,233.41 1 75
github.com/tetafro/godot/checks.go:236.2,236.14 1 55
github.com/tetafro/godot/checks.go:233.41,235.3 1 20
github.com/tetafro/godot/checks.go:241.41,243.45 1 650
github.com/tetafro/godot/checks.go:247.2,254.36 3 631
github.com/tetafro/godot/checks.go:259.2,263.40 2 551
github.com/tetafro/godot/checks.go:267.2,267.14 1 506
github.com/tetafro/godot/checks.go:243.45,245.3 1 19
github.com/tetafro/godot/checks.go:254.36,256.3 1 80
github.com/tetafro/godot/checks.go:263.40,265.3 1 45
github.com/tetafro/godot/checks.go:270.50,271.34 1 189
github.com/tetafro/godot/checks.go:276.2,276.14 1 100
github.com/tetafro/godot/checks.go:271.34,272.35 1 926
github.com/tetafro/godot/checks.go:272.35,274.4 1 89
github.com/tetafro/godot/getters.go:13.87,14.29 1 12
github.com/tetafro/godot/getters.go:19.2,20.54 2 12
github.com/tetafro/godot/getters.go:23.2,27.64 3 12
github.com/tetafro/godot/getters.go:31.2,33.15 3 12
github.com/tetafro/godot/getters.go:51.2,53.22 2 12
github.com/tetafro/godot/getters.go:14.29,16.3 1 0
github.com/tetafro/godot/getters.go:20.54,22.3 1 0
github.com/tetafro/godot/getters.go:27.64,29.3 1 0
github.com/tetafro/godot/getters.go:34.16,36.47 1 4
github.com/tetafro/godot/getters.go:37.21,43.4 1 4
github.com/tetafro/godot/getters.go:44.10,47.66 1 4
github.com/tetafro/godot/getters.go:58.86,60.34 2 8
github.com/tetafro/godot/getters.go:89.2,89.17 1 8
github.com/tetafro/godot/getters.go:60.34,62.10 2 88
github.com/tetafro/godot/getters.go:66.3,66.20 1 38
github.com/tetafro/godot/getters.go:69.3,69.35 1 16
github.com/tetafro/godot/getters.go:62.10,63.12 1 50
github.com/tetafro/godot/getters.go:66.20,67.12 1 22
github.com/tetafro/godot/getters.go:69.35,71.48 1 468
github.com/tetafro/godot/getters.go:78.4,78.42 1 16
github.com/tetafro/godot/getters.go:81.4,86.6 3 16
github.com/tetafro/godot/getters.go:71.48,72.13 1 452
github.com/tetafro/godot/getters.go:78.42,79.13 1 0
github.com/tetafro/godot/getters.go:93.89,95.34 2 4
github.com/tetafro/godot/getters.go:106.2,106.17 1 4
github.com/tetafro/godot/getters.go:95.34,96.41 1 117
github.com/tetafro/godot/getters.go:99.3,104.5 3 73
github.com/tetafro/godot/getters.go:96.41,97.12 1 44
github.com/tetafro/godot/getters.go:110.92,112.34 2 12
github.com/tetafro/godot/getters.go:132.2,132.17 1 12
github.com/tetafro/godot/getters.go:112.34,114.27 2 132
github.com/tetafro/godot/getters.go:121.3,121.16 1 132
github.com/tetafro/godot/getters.go:125.3,130.5 3 114
github.com/tetafro/godot/getters.go:115.21,116.14 1 57
github.com/tetafro/godot/getters.go:117.22,118.14 1 75
github.com/tetafro/godot/getters.go:121.16,122.12 1 18
github.com/tetafro/godot/getters.go:136.84,138.34 2 4
github.com/tetafro/godot/getters.go:146.2,146.17 1 4
github.com/tetafro/godot/getters.go:138.34,145.3 3 117
github.com/tetafro/godot/getters.go:153.52,156.40 1 424
github.com/tetafro/godot/getters.go:160.2,160.33 1 405
github.com/tetafro/godot/getters.go:179.2,179.17 1 405
github.com/tetafro/godot/getters.go:182.2,182.21 1 404
github.com/tetafro/godot/getters.go:156.40,158.3 1 19
github.com/tetafro/godot/getters.go:160.33,163.38 3 557
github.com/tetafro/godot/getters.go:168.3,168.50 1 557
github.com/tetafro/godot/getters.go:163.38,167.4 3 51
github.com/tetafro/godot/getters.go:168.50,169.27 1 635
github.com/tetafro/godot/getters.go:173.4,173.16 1 503
github.com/tetafro/godot/getters.go:176.4,176.20 1 503
github.com/tetafro/godot/getters.go:169.27,171.13 2 132
github.com/tetafro/godot/getters.go:173.16,175.5 1 376
github.com/tetafro/godot/getters.go:179.17,181.3 1 1
github.com/tetafro/godot/getters.go:186.40,187.25 1 12
github.com/tetafro/godot/getters.go:187.25,188.30 1 114
github.com/tetafro/godot/getters.go:188.30,189.34 1 1565
github.com/tetafro/godot/getters.go:189.34,191.10 2 114
github.com/tetafro/godot/godot.go:43.83,45.16 2 9
github.com/tetafro/godot/godot.go:49.2,52.20 3 9
github.com/tetafro/godot/godot.go:45.16,47.3 1 0
github.com/tetafro/godot/godot.go:56.95,59.16 2 8
github.com/tetafro/godot/godot.go:62.2,62.23 1 7
github.com/tetafro/godot/godot.go:66.2,67.16 2 6
github.com/tetafro/godot/godot.go:72.2,73.29 2 6
github.com/tetafro/godot/godot.go:78.2,79.60 2 6
github.com/tetafro/godot/godot.go:86.2,88.19 2 6
github.com/tetafro/godot/godot.go:59.16,61.3 1 1
github.com/tetafro/godot/godot.go:62.23,64.3 1 1
github.com/tetafro/godot/godot.go:67.16,69.3 1 0
github.com/tetafro/godot/godot.go:73.29,75.3 1 76
github.com/tetafro/godot/godot.go:79.60,81.28 2 774
github.com/tetafro/godot/godot.go:84.3,84.49 1 774
github.com/tetafro/godot/godot.go:81.28,83.4 1 76
github.com/tetafro/godot/godot.go:92.89,94.16 2 4
github.com/tetafro/godot/godot.go:97.2,100.16 3 3
github.com/tetafro/godot/godot.go:104.2,104.60 1 3
github.com/tetafro/godot/godot.go:107.2,107.12 1 3
github.com/tetafro/godot/godot.go:94.16,96.3 1 1
github.com/tetafro/godot/godot.go:100.16,102.3 1 0
github.com/tetafro/godot/godot.go:104.60,106.3 1 0
github.com/tetafro/godot/godot.go:111.30,112.38 1 9
github.com/tetafro/godot/godot.go:112.38,113.49 1 225
github.com/tetafro/godot/godot.go:116.3,116.41 1 225
github.com/tetafro/godot/godot.go:119.3,119.47 1 0
github.com/tetafro/godot/godot.go:113.49,115.4 1 0
github.com/tetafro/godot/godot.go:116.41,118.4 1 225
mode: atomic
83 changes: 61 additions & 22 deletions godot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,28 @@ func TestRun(t *testing.T) {
contains []string
}{
{
name: "scope: decl",
scope: DeclScope,
contains: []string{"[DECL]"},
name: "scope: decl",
scope: DeclScope,
contains: []string{
"[PERIOD_DECL]", "[CAPITAL_DECL]",
},
},
{
name: "scope: top",
scope: TopLevelScope,
contains: []string{"[DECL]", "[TOP]"},
name: "scope: top",
scope: TopLevelScope,
contains: []string{
"[PERIOD_DECL]", "[CAPITAL_DECL]",
"[PERIOD_TOP]", "[CAPITAL_TOP]",
},
},
{
name: "scope: all",
scope: AllScope,
contains: []string{"[DECL]", "[TOP]", "[ALL]"},
name: "scope: all",
scope: AllScope,
contains: []string{
"[PERIOD_DECL]", "[CAPITAL_DECL]",
"[PERIOD_TOP]", "[CAPITAL_TOP]",
"[PERIOD_ALL]", "[CAPITAL_ALL]",
},
},
}

Expand All @@ -49,9 +58,8 @@ func TestRun(t *testing.T) {
continue
}
for _, s := range tt.contains {
if strings.Contains(c.Text(), s) {
expected++
break
if cnt := strings.Count(c.Text(), s); cnt > 0 {
expected += cnt
}
}
}
Expand Down Expand Up @@ -99,7 +107,8 @@ func TestFix(t *testing.T) {
})

t.Run("scope: decl", func(t *testing.T) {
expected := strings.ReplaceAll(string(content), "[DECL]", "[DECL].")
expected := strings.ReplaceAll(string(content), "[PERIOD_DECL]", "[PERIOD_DECL].")
expected = strings.ReplaceAll(expected, "non-capital-decl", "Non-capital-decl")

fixed, err := Fix(testFile, file, fset, Settings{Scope: DeclScope, Period: true, Capital: true})
if err != nil {
Expand All @@ -110,8 +119,10 @@ func TestFix(t *testing.T) {
})

t.Run("scope: top", func(t *testing.T) {
expected := strings.ReplaceAll(string(content), "[DECL]", "[DECL].")
expected = strings.ReplaceAll(expected, "[TOP]", "[TOP].")
expected := strings.ReplaceAll(string(content), "[PERIOD_DECL]", "[PERIOD_DECL].")
expected = strings.ReplaceAll(expected, "[PERIOD_TOP]", "[PERIOD_TOP].")
expected = strings.ReplaceAll(expected, "non-capital-decl", "Non-capital-decl")
expected = strings.ReplaceAll(expected, "non-capital-top", "Non-capital-top")

fixed, err := Fix(testFile, file, fset, Settings{Scope: TopLevelScope, Period: true, Capital: true})
if err != nil {
Expand All @@ -122,9 +133,12 @@ func TestFix(t *testing.T) {
})

t.Run("scope: all", func(t *testing.T) {
expected := strings.ReplaceAll(string(content), "[DECL]", "[DECL].")
expected = strings.ReplaceAll(expected, "[TOP]", "[TOP].")
expected = strings.ReplaceAll(expected, "[ALL]", "[ALL].")
expected := strings.ReplaceAll(string(content), "[PERIOD_DECL]", "[PERIOD_DECL].")
expected = strings.ReplaceAll(expected, "[PERIOD_TOP]", "[PERIOD_TOP].")
expected = strings.ReplaceAll(expected, "[PERIOD_ALL]", "[PERIOD_ALL].")
expected = strings.ReplaceAll(expected, "non-capital-decl", "Non-capital-decl")
expected = strings.ReplaceAll(expected, "non-capital-top", "Non-capital-top")
expected = strings.ReplaceAll(expected, "non-capital-all", "Non-capital-all")

fixed, err := Fix(testFile, file, fset, Settings{Scope: AllScope, Period: true, Capital: true})
if err != nil {
Expand Down Expand Up @@ -164,7 +178,8 @@ func TestReplace(t *testing.T) {
defer func() {
ioutil.WriteFile(testFile, content, mode) // nolint: errcheck,gosec
}()
expected := strings.ReplaceAll(string(content), "[DECL]", "[DECL].")
expected := strings.ReplaceAll(string(content), "[PERIOD_DECL]", "[PERIOD_DECL].")
expected = strings.ReplaceAll(expected, "non-capital-decl", "Non-capital-decl")

err := Replace(testFile, file, fset, Settings{Scope: DeclScope, Period: true, Capital: true})
if err != nil {
Expand All @@ -182,9 +197,33 @@ func TestReplace(t *testing.T) {
defer func() {
ioutil.WriteFile(testFile, content, mode) // nolint: errcheck,gosec
}()
expected := strings.ReplaceAll(string(content), "[DECL]", "[DECL].")
expected = strings.ReplaceAll(expected, "[TOP]", "[TOP].")
expected = strings.ReplaceAll(expected, "[ALL]", "[ALL].")
expected := strings.ReplaceAll(string(content), "[PERIOD_DECL]", "[PERIOD_DECL].")
expected = strings.ReplaceAll(expected, "[PERIOD_TOP]", "[PERIOD_TOP].")
expected = strings.ReplaceAll(expected, "non-capital-decl", "Non-capital-decl")
expected = strings.ReplaceAll(expected, "non-capital-top", "Non-capital-top")

err := Replace(testFile, file, fset, Settings{Scope: TopLevelScope, Period: true, Capital: true})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
fixed, err := ioutil.ReadFile(testFile) // nolint: gosec
if err != nil {
t.Fatalf("Failed to read fixed file %s: %v", testFile, err)
}

assertEqualContent(t, expected, string(fixed))
})

t.Run("scope: all", func(t *testing.T) {
defer func() {
ioutil.WriteFile(testFile, content, mode) // nolint: errcheck,gosec
}()
expected := strings.ReplaceAll(string(content), "[PERIOD_DECL]", "[PERIOD_DECL].")
expected = strings.ReplaceAll(expected, "[PERIOD_TOP]", "[PERIOD_TOP].")
expected = strings.ReplaceAll(expected, "[PERIOD_ALL]", "[PERIOD_ALL].")
expected = strings.ReplaceAll(expected, "non-capital-decl", "Non-capital-decl")
expected = strings.ReplaceAll(expected, "non-capital-top", "Non-capital-top")
expected = strings.ReplaceAll(expected, "non-capital-all", "Non-capital-all")

err := Replace(testFile, file, fset, Settings{Scope: AllScope, Period: true, Capital: true})
if err != nil {
Expand Down
21 changes: 14 additions & 7 deletions testdata/check/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

Main integration test for linter.

- If line contains `[DECL]` string, it should be caught as error by the
linter with the setting `Scope: DeclScope`.
- If line contains `[TOP]` string, it should be caught as error by the
linter with the setting `Scope: TopLevelScope`.
- If line contains `[ALL]` string, it should be caught as error by the
linter with the setting `Scope: AllScope`.
- If line contains `[PASS]` string, it shouldn't be caught.
Tags:
- `[PERIOD_DECL]` - line should be caught as an error by the linter with the
settings `Scope: DeclScope, Period: true`.
- `[PERIOD_TOP]` - line should be caught as an error by the linter with the
settings `Scope: TopLevelScope, Period: true`.
- `[PERIOD_ALL]` - line should be caught as an error by the linter with the
settings `Scope: AllScope, Period: true`.
- `[CAPITAL_DECL]` - line should be caught as an error by the linter with the
settings `Scope: DeclScope, Capital: true`.
- `[CAPITAL_TOP]` - line should be caught as an error by the linter with the
settings `Scope: TopLevelScope, Capital: true`.
- `[CAPITAL_ALL]` - line should be caught as an error by the linter with the
settings `Scope: AllScope, Capital: true`.
- `[PASS]` - line shouldn't be caught.
Loading

0 comments on commit cc1247e

Please sign in to comment.