# ast-tree-query

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

Latest version **1.0.2** (published 2020-12-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install ast-tree-query
pnpm add ast-tree-query
yarn add ast-tree-query
bun add ast-tree-query
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2020-12-28 |
| First published | 2020-12-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 15.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Xavier Hamel |
| Maintainers | xavierhamel |
| Keywords | AST, tree, query, traversal |

## Links

- npm: https://www.npmjs.com/package/ast-tree-query
- npm.io page: https://npm.io/package/ast-tree-query

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2020-12-28
- 1.0.1 — 2020-12-28
- 1.0.0 — 2020-12-26

## 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
```javascript
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.
```javascript
// ...
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]`
```javascript
// ...
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")`.
```javascript
// ...
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
```javascript
// ...
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 `.`
```javascript
// ...
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
```javascript
// ...
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:
```javascript
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] } }
```

---
_Source: https://npm.io/package/ast-tree-query · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
