uber / NEAL

🔎🐞 A language-agnostic linting platform
https://uber.github.io/NEAL/
MIT License
428 stars 28 forks source link

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

Open interstateone opened 7 years ago

interstateone commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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. :+1: