-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ruleguard: implement MatchComment
function from the DSL
#223
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sgtm 🚀
sort.Slice(values, func(i, j int) bool { | ||
return values[i].Name < values[j].Name | ||
}) | ||
|
||
for _, v := range values { | ||
name := v.Name | ||
node := v.Node | ||
|
||
if comment, ok := node.(*ast.Comment); ok { | ||
s := strings.ReplaceAll(comment.Text, "\n", `\n`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tricky :)
} | ||
resultIndex := i * 2 | ||
beginPos := result[resultIndex+0] | ||
endPos := result[resultIndex+1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like we're expecting result
to be even, what about to add a check before loop with a panic? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the docs:
If 'Index' is present, matches and submatches are identified by byte index pairs within the input string: result[2n:2n+1] identifies the indexes of the nth submatch.
https://golang.org/pkg/regexp/#Regexp.FindStringSubmatchIndex
I fixed another issue though. Checking for -1
was too fragile.
If an index is negative or text is nil, it means that subexpression did not match any string in the input.
So, it's safer to check for any negative number than for just -1
.
@@ -0,0 +1,3 @@ | |||
package comments | |||
|
|||
//go:embed data.txt // want `don't use go:embed in _foo files` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, embed, should we bump go version in go.mod
to 1.16 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a testdata. I don't think it should be relevant. :)
//go:noinline | ||
//go:generate foo bar | ||
|
||
//nolint:gocritic // want `hey, this is kinda upsetting` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😂
Most `Where()` filters work for comments as one would expect. Named captures `?P<name>` can be accessed via `m["name"]`. `Report()` and `Suggest()` interpolation can work with named captures. The `$$` variable is bound to the entire regexp match (like `$0`).
05088ed
to
4072238
Compare
Most
Where()
filters work for comments as one would expect.Named captures
?P<name>
can be accessed viam["name"]
.Report()
andSuggest()
interpolation can work with named captures.The
$$
variable is bound to the entire regexp match (like$0
).Quickfixes work as well.
See #222