# ast-query

> Declarative JavaScript AST modification façade

Latest version **2.0.0** (published 2016-06-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install ast-query
pnpm add ast-query
yarn add ast-query
bun add ast-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 | 2.0.0 |
| Published | 2016-06-10 |
| First published | 2013-07-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 249 |
| Author | Simon Boudrias |
| Maintainers | sboudrias |
| Keywords | AST, source, traversal, syntax, tree |

## Links

- npm: https://www.npmjs.com/package/ast-query
- Repository: https://github.com/sboudrias/AST-query
- Homepage: https://github.com/sboudrias/AST-query#readme
- Issues: https://github.com/sboudrias/AST-query/issues
- npm.io page: https://npm.io/package/ast-query

## Dependencies (5)

- [lodash](https://npm.io/package/lodash.md) ^4.6.1
- [traverse](https://npm.io/package/traverse.md) ^0.6.6
- [acorn-jsx](https://npm.io/package/acorn-jsx.md) ^3.0.1
- [class-extend](https://npm.io/package/class-extend.md) ^0.1.1
- [escodegen-wallaby](https://npm.io/package/escodegen-wallaby.md) ^1.6.7

## Alternatives

- [update-check](https://npm.io/package/update-check.md) — 4.0M weekly downloads
- [react-native-onesignal](https://npm.io/package/react-native-onesignal.md) — 134.5K weekly downloads
- [react-redux-toastr](https://npm.io/package/react-redux-toastr.md) — 33.7K weekly downloads
- [@nocobase/plugin-notification-manager](https://npm.io/package/@nocobase/plugin-notification-manager.md) — 2.0K weekly downloads
- [react-simple-toasts](https://npm.io/package/react-simple-toasts.md) — 1.9K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2016-06-10
- 1.2.0 — 2016-03-15
- 1.1.1 — 2016-03-15
- 1.1.0 — 2015-12-23
- 1.0.1 — 2015-02-18
- 1.0.0 — 2015-02-18
- 0.3.1 — 2015-01-25
- 0.3.0 — 2014-09-19
- 0.2.5 — 2014-09-19
- 0.2.4 — 2014-05-07
- 0.2.3 — 2014-04-05
- 0.2.2 — 2014-04-05
- 0.2.1 — 2014-04-05
- 0.2.0 — 2014-04-05
- 0.1.0-pre.2 — 2014-02-28
- … 2 more at https://npm.io/package/ast-query/versions

## README

AST Query
================

[![npm](https://badge.fury.io/js/ast-query.svg)](http://badge.fury.io/js/ast-query) [![tests](https://travis-ci.org/SBoudrias/AST-query.svg?branch=master)](http://travis-ci.org/SBoudrias/AST-query) [![dependencies](https://david-dm.org/SBoudrias/AST-query.svg?theme=shields.io)](https://david-dm.org/SBoudrias/AST-query)


This project is a tentative to create a simple JavaScript AST modification library.

If you've ever worked with AST trying to edit source code, you'll know it is a bad time. AST syntax is terse and forces you to loop a tree and use conditional structure a lot. AST Query hide these complexities behind a declarative façade.

Making the simplicity choice means AST Query won't try to cover the full AST API. Rather we strive to answer commons needs.


Getting Started
================

Install: `npm install --save ast-query`

First, you need to pass a program code into AST query:

``` javascript
var program = require("ast-query");
var tree = program("var a = 'foo'");
```

This function returns a wrapped AST tree you can query and modify.

Once you've modified the AST, get the source code back by calling the `toString` method on the tree.

``` javascript
// ...
tree.var("a").value("'bar'");

console.log( tree.toString() );
// LOG: var a = 'bar';
```

Remember that you are editing source code. This mean you provide raw source code strings. This mean you need to double wrap strings (e.g.: `"'foo'"`). If that's not done, AST-query assume you're referencing a variable called `foo`.


API
================

Program
----------------

### `var tree = program( sourceCode, escodegenOptions, esprimaOptions )`
- **sourceCode** (String) - The source code to edit.
- **escodegenOptions** (Object) _optional_ - [escodegen](https://github.com/Constellation/escodegen) option object
- **acornOptions** (Object) _optional_ - [acorn](https://github.com/ternjs/acorn) option object

Returns an AST tree you can then query as explained below:

### `tree.var( name )`
- **name** (String) - The variable name

Find and returns a [`Variable` node](#variable-node).

Given this code

``` js
var bar = 23;
```

You'd call `tree.var('bar')` to get the Variable node.

### `tree.callExpression( name )`
- **name** (String) - The name of the function or method being called.

Find a function or method call and return a [`CallExpression` node](#callexpression-node)

Given this code

```js
grunt.initConfig({});
```

You'd call `tree.callExpression('grunt.initConfig')` to get the CallExpression node.

### `tree.assignment( assignedTo )`
- **assignedTo** (String) - The name (name or object) a value is assigned to

Find and return an [`AssignmentExpression` node](#assignmentexpression-node).

You'd call `tree.assignment('module.exports')` to query the code below:

```js
module.exports = function () {
  // code
};
```
### `tree.body`

Property representing the program body in a [`Body` node](#body-node).

### `tree.verbatim( body )`
- **body** (String) - The source code to inline verbatim

Adds body and return a token assigment.

```js
tree.body.append('var a = 1;' + tree.verbatim('ANYTHING'));
```

Variable node
-----------------

### `.value( value )`
- **value** (String) _optionnal_ - A string containing the new variable value.

It returns the current or new value wrapped in AST query interface.

### `.rename( name )`
- **name** (String) - Change the variable name

CallExpression node
--------------------

### `.filter( iterator )`
- **iterator** (Function) - Function receiving each node as arguments and returning true to keep the current node in the returned set.

Return a new CallExpression nodes collection with nodes passing the iterator test.

### `.arguments`

A property pointing to an [`ArrayExpression` node](#arrayexpression-node) referencing the called function arguments.

AssignmentExpression node
--------------------

### `.value( value )`

Replace the assignment value with a new value or return the current value wrapped in an AST query interface.

Literal node
--------------------

A Literal node represent a raw JavaScript value as a String, a Number or a Boolean.

### `.value( value )`

Get or update the value.

FunctionExpression node
-------------------

Node representing a function declaration (e.g. `function () {}`).

### `.body`

Property pointing to a [`Body` node](#body-node) representing the function expression body.

ObjectExpression node
-------------------

### `.key( name )`
- **name** (String) - Key name
Get a key value object or create a blank placeholder

### `value( value )`

Replace current node with a new value. Returns the new value wrapped.

ArrayExpression node
-------------------

### `.push( value )`
- **value** (String) - value to push in the array

### `.unshift( value )`
- **value** (String) - value to unshift in the array

### `.at( index )`
- **index** (Number) - Index of the value to fetch

Returns a value wrapped in an AST query interface.

### `value( value )`

Replace current node with a new value. Returns the new value wrapped.

Body node
-------------------

### `.prepend( code )`

Preprend the given code lines in the body. If a `"use strict";` statement is present, it always stay first.

### `.append( code )`

Append the given code lines in the body.

Contributing
=====================

**Style Guide**: Please base yourself on [Idiomatic.js](https://github.com/rwldrn/idiomatic.js)
style guide with two space indent
**Unit test**: Unit test are wrote in Mocha. Please add a unit test for every new feature
or bug fix. `npm test` to run the test suite.
**Documentation**: Add documentation for every API change. Feel free to send corrections
or better docs!
**Pull Requests**: Send _fixes_ PR on the `master` branch. Any new features should be send
on the `wip`branch.


License
=====================

Copyright (c) 2013 Simon Boudrias (twitter: @vaxilart)
Licensed under the MIT license.

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