Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Navigate ASTs with x-path-like queries (github.com/rse)
28 points by bdg on June 30, 2022 | hide | past | favorite | 10 comments


... "for JavaScript" would have helped

I hope in addition to the cute operators they also support actual XPath, since it'd be less reinventing the wheel for those who already know XPath

Also, prior art for static analysis using XPath: https://pmd.github.io/pmd-6.47.0/


What would it have helped with?


It would have helped potential readers to know that this project is both in, and for, JavaScript, unlike the PMD project that I mentioned which offers that same XPath querying but for a bunch of target languages


This is great! I think I've seen a similar thing used for picking up issues like deprecated usages and things like that.

I've found myself manually writing code for finding things in python's AST but a tool like this would be much more succinct

Edit: the author's website is a delight - http://engelschall.com/


for work, i wrote a tool that takes an astroid AST tree and translates it into XML (in memory), which are very close in spirit since they're both tree-like structures. this lets one write linter tools using familiar XPath queries and assert on the results. it's good for some kinds of pattern/anti-pattern enforcement.

https://pylint.pycqa.org/projects/astroid/en/latest/index.ht...


That's a clever way of leveraging existing tooling! I hadn't heard of astroid before, I've always used the built in ast module


Just curious, what do you use the resultant XML for? Analysis? Rewriting code?


no rewrites (the AST->XML bridging is one way), we use it for checking for forbidden patterns that we don't want developers using.


>I've found myself manually writing code for finding things in python's AST but a tool like this would be much more succinct

Wow, I also am writing a tool for finding things in the Python AST: https://github.com/Incognito/python-architecture-linter

It does other things too, but one of the key features is reading the AST. It's a bit of a prototype but if you want to jam together on a project I'd be open to it.


Your tool is _much_ more powerful than what I've been doing. I've been looking at things like 'what proportion of functions have docstrings, and what format are they in?' or 'how much of our codebase is using typing?'

Yours is much more ambitious, if I understand it correctly?




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: