Skip to content

Conversation

@chokoswitch
Copy link
Collaborator

Fixes #11

@chokoswitch
Copy link
Collaborator Author

@capnspacehook Thanks for the feature request, being able to match against package sounds useful. Would you be able to give this PR a brief check to make sure it accomplishes what you need? Thanks!

Copy link

@capnspacehook capnspacehook left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good overall, however I played around with the packagepattern test and found a case that failed: detecting reassignment of net/http.DefaultClient and net/http.DefaultTransport works fine, but detecting reassignment of a struct field doesn't:

Adding this line to testdata/packagepattern/src/a/a.go will cause the test to fail:

	myhttp.DefaultClient.Jar = nil // want "reassigning variable"

I guessing building a string of changed *ast.SelectorExprs and matching against that might work.


Also, I created a new test to check if dot imports are handled correctly which also failed. Go src file I placed in testdata/dotimport/src/big/big.go:

package big

import (
	. "net/http"
)

func chungus() {
	DefaultClient = nil     // want "reassigning variable"
	DefaultClient.Jar = nil // want "reassigning variable"
}

To fix this, I'm thinking adding logic to the *ast.Ident case that would find the package path of the identifier and match on the package path + the identifier (much like you already do when handling *ast.SelectorExpr) would do the trick.

@chokoswitch
Copy link
Collaborator Author

Thanks a lot for the check @capnspacehook those both look like great issues to fix. I'll go ahead and merge this one as it seems to not block those fixes.

It looks like you've given some thought into the implementations, would you be able to send a PR after I merge this one? Or otherwise I'll try to repro them myself too.

@chokoswitch chokoswitch merged commit 361980f into curioswitch:main Dec 5, 2022
@capnspacehook
Copy link

👍 Makes sense.

I probably won't have time for a few weeks, but I can then. I'll let you know if I start working on it, otherwise go for it. Thanks for taking my feedback into consideration!

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.

Add way to limit patterns to a specific package

2 participants