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

Example of how to write a rule with sibling nodes #9

Open
interstateone opened this issue Oct 11, 2017 · 2 comments
Open

Example of how to write a rule with sibling nodes #9

interstateone opened this issue Oct 11, 2017 · 2 comments

Comments

@interstateone
Copy link

My use case is I want to warn when a Swift ImportDeclaration with a particular identifier is in a file that also contains a class that conforms to a particular protocol. I'm not sure how to declare that this rule should match an ImportDeclaration and ClassDeclaration that are siblings. I've tried a few things and none of them seem to work (syntax errors). Can you clarify if this is currently possible and maybe provide a small example? I could try to submit a PR with some docs if I got that far.

I see Add support for sibling patterns through conditional variables in the changelog and the closest I can find to something about this is this in a rule. It probably doesn't help that I'm not familiar with OCaml either 😄

@tadeuzagallo
Copy link
Contributor

Hey, you're right, I forgot to add this feature to the documentation, sorry about that. The rule you pointed to is indeed the one example in the repo about it. The idea is that you are allowed to declare local boolean variables, and then you can modify then within whatever matcher you wish. e.g. in your case, you could have has_protocol and has_class both initialised to false, and when you match your class declaration you set has_class to true and similar with the protocol. Finally you add a condition, e.g. condition(has_class && has_protocol) { fail("...") }

However, as I wrote this, I realised you might run into another problem, which is that right now you must specify a top-level matcher, e.g. you can only declare variables after you matched a given node, which is unfortunate. In that case, you couldn't match both a class and a protocol at the top-level. I've filed #10 for that, and I'll look into it pretty soon.

If that makes things clearer and you want to send a PR, that would be great 😃 otherwise, I'll add it to the docs before cutting a new version pretty soon.

PS: sorry for the delay, for some reason I can't seem to get notifications for this repo.

@interstateone
Copy link
Author

No worries about the delay, thanks for the thorough reply. The issue with top-level matchers was what I was seeing, so that makes sense. 👍

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

No branches or pull requests

2 participants