Looks like a winner (see below, or the gist)! Thanks all. I do think we should consider a treesit-node-ancestors function that collects all the parent (of parent) nodes in one go into an (emacs) list, since you basically have to descend the whole tree from root to find the 1st parent anyway. Then people who want to know, e.g., “am I in an if block?” can just test node type down the full ancestor list. Of course, also, node-parent-until/while could be re-written to use node-ancestors, for some additional efficiency.