Skip to content

Commit

Permalink
Fix panic with single parenthesis in comment.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krivak committed Sep 17, 2020
1 parent 30ce07f commit f41657b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions godot.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ func checkLastChar(s string) bool {
}
// Trim parenthesis for cases when the whole sentence is inside parenthesis
s = strings.TrimRight(s, ")")
// Don't panic when comment looks like this: `// )`
// TODO: Check previous line (which is not available in this function right now)
if len(s) == 0 {
return true
}
for _, ch := range lastChars {
if string(s[len(s)-1]) == ch {
return true
Expand Down
24 changes: 24 additions & 0 deletions godot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ func TestCheckComment(t *testing.T) {
ok: false,
pos: position{line: 0, column: 17},
},
{
name: "singleline comment: single closing parenthesis without period",
comment: "// )",
ok: true,
pos: position{},
},
{
name: "singleline comment: empty",
comment: "// ",
ok: true,
pos: position{},
},
// Multiline comments
{
name: "multiline comment: ok",
Expand Down Expand Up @@ -271,6 +283,18 @@ func TestCheckComment(t *testing.T) {
ok: false,
pos: position{line: 2, column: 7},
},
{
name: "multiline comment: single closing parenthesis without period",
comment: "/*\n" + " )\n" + "*/",
ok: true,
pos: position{},
},
{
name: "multiline comment: empty",
comment: "/**/",
ok: true,
pos: position{},
},
}

for _, tt := range testCases {
Expand Down

0 comments on commit f41657b

Please sign in to comment.