-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support custom forbid reason messages (#11)
In order to allow users to communicate intent to collaborators, optionally embed custom messages into each forbidden pattern. The syntax is as follows: `identifier(# message goes here)?` Example: `^fmt\.Errorf(# Please don't use this!)?$` Regular expressions containing custom messages are effectively identical to ones that don't, because the sub-expression containing it is marked optional with a `?`. All this commit does is parse out any recognized custom message, and place it prominently in the tool's output. The regular expression itself is omitted from the tool's output when a custom message is specified.
- Loading branch information
1 parent
e0c18fe
commit 9d87656
Showing
5 changed files
with
127 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package forbidigo | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"regexp/syntax" | ||
"strings" | ||
) | ||
|
||
type pattern struct { | ||
pattern *regexp.Regexp | ||
msg string | ||
} | ||
|
||
func parse(ptrn string) (*pattern, error) { | ||
ptrnRe, err := regexp.Compile(ptrn) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to compile pattern `%s`: %s", ptrn, err) | ||
} | ||
re, err := syntax.Parse(ptrn, syntax.Perl) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to parse pattern `%s`: %s", ptrn, err) | ||
} | ||
msg := extractComment(re) | ||
return &pattern{pattern: ptrnRe, msg: msg}, nil | ||
} | ||
|
||
// Traverse the leaf submatches in the regex tree and extract a comment, if any | ||
// is present. | ||
func extractComment(re *syntax.Regexp) string { | ||
for _, sub := range re.Sub { | ||
if len(sub.Sub) > 0 { | ||
if comment := extractComment(sub); comment != "" { | ||
return comment | ||
} | ||
} | ||
subStr := sub.String() | ||
if strings.HasPrefix(subStr, "#") { | ||
return strings.TrimSpace(strings.TrimPrefix(subStr, "#")) | ||
} | ||
} | ||
return "" | ||
} |
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,57 @@ | ||
package forbidigo | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestParseValidPatterns(t *testing.T) { | ||
for _, tc := range []struct { | ||
name string | ||
ptrn string | ||
expectedComment string | ||
}{ | ||
{ | ||
name: "simple expression, no comment", | ||
ptrn: `fmt\.Errorf`, | ||
}, | ||
{ | ||
name: "anchored expression, no comment", | ||
ptrn: `^fmt\.Errorf$`, | ||
}, | ||
{ | ||
name: "contains multiple subexpression, with comment", | ||
ptrn: `(f)mt\.Errorf(# a comment)?`, | ||
expectedComment: "a comment", | ||
}, | ||
{ | ||
name: "simple expression with comment", | ||
ptrn: `fmt\.Println(# Please don't use this!)?`, | ||
expectedComment: "Please don't use this!", | ||
}, | ||
{ | ||
name: "deeply nested expression with comment", | ||
ptrn: `fmt\.Println((((# Please don't use this!))))?`, | ||
expectedComment: "Please don't use this!", | ||
}, | ||
{ | ||
name: "anchored expression with comment", | ||
ptrn: `^fmt\.Println(# Please don't use this!)?$`, | ||
expectedComment: "Please don't use this!", | ||
}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
ptrn, err := parse(tc.ptrn) | ||
require.Nil(t, err) | ||
assert.Equal(t, tc.ptrn, ptrn.pattern.String()) | ||
assert.Equal(t, tc.expectedComment, ptrn.msg) | ||
}) | ||
} | ||
} | ||
|
||
func TestParseInvalidPattern_ReturnsError(t *testing.T) { | ||
_, err := parse(`fmt\`) | ||
assert.NotNil(t, err) | ||
} |