getgrit / gritql

GritQL is a query language for searching, linting, and modifying code.

Home Page:https://docs.grit.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Retrieve / check the direct ancestor of a binding

morgante opened this issue · comments

Ideally, there would be an easy way to directly check the parent of a node against a pattern. Within is not quite sufficient, since it traverses upwards forever.

Use cases

  • Confirming the direct parent of a statement is the program itself here

Possible approach: built-in parent function

New parent($node) built in that retrieves the parent of a node.

Reusing within

We could allow within to take a modifier, determining how far it would traverse upwards.

Ex. this would make sure a const is directly within a specific parent:

`const $x;` as $match where $match <: within(1) statement_block()

This could be used to implement a parent function in user space:

function parent($pattern) {
   $pattern <: within(1) $parent,
   return $parent
}