npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

ast-tree-query

v1.0.2

Published

Searching nodes in a javascript AST with a very simple syntax.

Readme

AST Tree Query

This is a very small lib for searching nodes in a javascript AST with a very simple syntax and no other dependencies.

This project was created for working on javascript AST but can also be used to work with any javascript tree-like object.

Getting Started

Install using npm :

npm install ast-tree-query

Example

const source = `
function a() {
	return 1 + 2;
}`;
const acorn = require("acorn");
const query = require("ast-tree-query");
const ast = acorn.parse(source, {ecmaVersion:2017});
console.log("Current node :", query(ast, "[.body][type=FunctionDeclaration & id.name=a][.body][.body][:first]"))
// Current node : 
// Node {
//    type: 'ReturnStatement',
//    start: 17,
//    end: 30,
//    argument:
//     Node {
//       type: 'BinaryExpression',
//       start: 24,
//       end: 29,
//       left: [Node],
//       operator: '+',
//       right: [Node] } }

Querying language

Selecting a property [.propertyName]

Get the value of a property.

// ...
const tree = {propertyName:"value"}
query(tree, "[.propertyName]"); // "value"

Selecting by index [:first] [:last] [:nth=index]

Get the first, last or nth element in the array. Similar to array[n]

// ...
const tree = ["one", ["two", "tree"]];
query(tree, "[:last][:first]"); // "two"
query(tree, "[:nth=0]"); // "one"

Filtering an array with property inside it [property=value]

Only keep elements with the property that is equal to the value. Similar to array.filter((element) => element.property === "value").

// ...
const tree = [{prop:"value1"}, {prop:"value2"}];
query(tree, "[prop=value1]"); // [{prop:"value1"}]

You can also filter for multiple properties and values with the & to join multiple filters

// ...
const tree = [{prop:"value", prop2:"value1"}, {prop:"value", prop2:"value2"}];
query(tree, "[prop=value & prop2=value1]"); // [{prop:"value", prop2:"value1"}]

You can also access a member of a property that is deeper than the current property with a .

// ...
const tree = [{prop:"value1", id:{name:"identifier"}}, {prop:"value2", id:{name:"identifier2"}}];
query(tree, "[id.name=identifier2]"); // [{prop:"value2", id:{name:"identifier2"}}]

Chaining query selectors

// ...
const tree = [{property:"property1", values:[{value:"value1"}]}, {property:"property2", values:[{value:"value2"}, {value:"value3"}]}];
query(tree, "[property=property2][.values][:last]"); // "value3"

For querying an ast:

const source = `
function a() {
	return 1 + 2;
}`;
const acorn = require("acorn");
const query = require("ast-tree-query");
const ast = acorn.parse(source, {ecmaVersion:2017});
console.log("Current node :", query(ast, "[.body][type=FunctionDeclaration & id.name=a][.body][.body][:nth=0]"))
// Current node : 
// Node {
//    type: 'ReturnStatement',
//    start: 17,
//    end: 30,
//    argument:
//     Node {
//       type: 'BinaryExpression',
//       start: 24,
//       end: 29,
//       left: [Node],
//       operator: '+',
//       right: [Node] } }