Skip to content
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

Merged
merged 1 commit into from
Apr 6, 2021

Conversation

quasilyte
Copy link
Owner

@quasilyte quasilyte commented Apr 6, 2021

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).

Quickfixes work as well.

See #222

@quasilyte quasilyte requested a review from cristaloleg April 6, 2021 19:01
Copy link
Collaborator

@cristaloleg cristaloleg left a 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`)
Copy link
Collaborator

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]
Copy link
Collaborator

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? :)

Copy link
Owner Author

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`
Copy link
Collaborator

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 ?

Copy link
Owner Author

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`
Copy link
Contributor

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`).
@quasilyte quasilyte force-pushed the quasilyte/feature/comments_matching branch from 05088ed to 4072238 Compare April 6, 2021 23:12
@quasilyte quasilyte merged commit 98814ca into master Apr 6, 2021
@quasilyte quasilyte deleted the quasilyte/feature/comments_matching branch April 6, 2021 23:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants