Skip to content
This repository has been archived by the owner on Jun 9, 2021. It is now read-only.

think whether B.C should match A.B.C #12

Open
mvdan opened this issue Sep 19, 2017 · 4 comments
Open

think whether B.C should match A.B.C #12

mvdan opened this issue Sep 19, 2017 · 4 comments

Comments

@mvdan
Copy link
Owner

mvdan commented Sep 19, 2017

Right now it doesn't. A.B does match the beginning of A.B.C. This is because of the nature of how these trees of selectors are built - (A.B).C. It should be noted, however, that $x.C does match the entire A.B.C because of this.

Is there a use case for this? Haven't found a real one. Found this quirk while playing.

/cc @rogpeppe

mvdan added a commit that referenced this issue Sep 19, 2017
See #12 for why the second one is not a match.
@rogpeppe
Copy link
Collaborator

I think this is correct. I've never needed B.C to match A.B.C when writing gofmt -r expressions, and I think it's useful that it doesn't because I don't think I ever want io.Write to match something.io.Write, for example. In fact I've definitely relied on this property before.

Field names and method names are qualitatively different from expressions, and I don't think it's useful to maintain that distinction.

@mvdan
Copy link
Owner Author

mvdan commented Sep 19, 2017

Ah, good point. Idea shut down.

@mvdan mvdan closed this as completed Sep 19, 2017
@mvdan mvdan reopened this Jan 10, 2018
@mvdan
Copy link
Owner Author

mvdan commented Jan 10, 2018

I hear the point about sometimes not wanting B.C to match A.B.C. But I have just found one case that is the opposite.

I wanted to match all exec.Command("go", ...) call expressions. I tried:

gogrep 'exec.Command("go", $*_)'

Which works almost everywhere, except in the exec package itself - because there, we have just Command("go", ...).

Perhaps another way to go around this would be to match exec.Command by its definition (via type information) rather than AST matching. Then, if the user did import exec1 "os/exec" and exec1.Command("go", ...), it would match too.

@rogpeppe
Copy link
Collaborator

Yeah, that's an interesting point. It applies to type aliases too. For example we might do:

gogrep 'http.Request'

but that won't find places that it's mentioned via a type alias (which we probably want).
Maybe we could consider being able to add constraints for identity.

e.g. (straw man)

 gogrep -is x=exec.Command '$x("go", $*_)'

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants