# @buxlabs/ast

> abstract syntax tree class for js

Latest version **0.11.0** (published 2018-05-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install @buxlabs/ast
pnpm add @buxlabs/ast
yarn add @buxlabs/ast
bun add @buxlabs/ast
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.11.0 |
| Published | 2018-05-13 |
| First published | 2016-12-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.11.1 |
| Dependencies | 9 |
| Unpacked size | 12.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 115 |
| Author | Emil Ajdyna |
| Maintainers | buxlabs |
| Keywords | ast, javascript, abstract syntax tree |

## Links

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

## Dependencies (9)

- [cherow](https://npm.io/package/cherow.md) ^1.4.16
- [to-ast](https://npm.io/package/to-ast.md) ^1.0.0
- [astring](https://npm.io/package/astring.md) ^1.3.0
- [esquery](https://npm.io/package/esquery.md) ^1.0.1
- [prettier](https://npm.io/package/prettier.md) ^1.12.1
- [comparify](https://npm.io/package/comparify.md) ^0.2.0
- [estemplate](https://npm.io/package/estemplate.md) ^0.5.1
- [estraverse](https://npm.io/package/estraverse.md) ^4.2.0
- [source-map](https://npm.io/package/source-map.md) ^0.7.2

## 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

- 0.11.0 (latest) — 2018-05-13
- 0.10.2 — 2018-05-10
- 0.10.1 — 2018-05-01
- 0.10.0 — 2018-04-30
- 0.9.6 — 2018-03-17
- 0.9.5 — 2018-03-10
- 0.9.4 — 2018-01-28
- 0.9.3 — 2018-01-20
- 0.9.2 — 2018-01-06
- 0.9.1 — 2018-01-04
- 0.9.0 — 2017-12-17
- 0.8.2 — 2017-11-18
- 0.8.1 — 2017-11-05
- 0.8.0 — 2017-11-05
- 0.7.2 — 2017-11-05
- … 26 more at https://npm.io/package/@buxlabs/ast/versions

## README

# AbstractSyntaxTree

[![Build Status](https://travis-ci.org/buxlabs/ast.svg?branch=master)](https://travis-ci.org/buxlabs/ast) [![NSP Status](https://nodesecurity.io/orgs/buxlabs/projects/4b0bfe3e-43d0-4597-b407-dd44cec2f3d6/badge)](https://nodesecurity.io/orgs/buxlabs/projects/4b0bfe3e-43d0-4597-b407-dd44cec2f3d6)

You might find the class useful if you want to abstract away some ast manipulations such as adding, removing, replacing the nodes and others. The test folder is a good starting point for examples of usage. PRs are highly welcome.

## Installation

`npm install @buxlabs/ast`

## Methods

### has

Check if ast contains a node of given type.

```javascript
const source = 'const a = "x";'
const ast = new AbstractSyntaxTree(source)
ast.has('VariableDeclaration')
```

### count

Count ast nodes of given type.

```javascript
const source = 'const a = "x"; const b = "y";'
const ast = new AbstractSyntaxTree(source)
ast.count('VariableDeclaration')
```

### find

Find all nodes of given type.

```javascript
const source = 'const a = "x";'
const ast = new AbstractSyntaxTree(source)
ast.find('VariableDeclaration')
```

### each

Iterate over all nodes of given type.

```javascript
const source = 'const a = "x";'
const ast = new AbstractSyntaxTree(source)
ast.each('VariableDeclaration', node => {})
```

### first

First first node of given type.

```javascript
const source = 'var a = "x";'
const ast = new AbstractSyntaxTree(source)
ast.first('VariableDeclaration')
```

### last

Find last node of given type.

```javascript
const source = 'const a = "x";'
const ast = new AbstractSyntaxTree(source)
ast.last('VariableDeclaration')
```

### remove

Remove all nodes that match the criteria.

```javascript
const source = '"use strict"; const b = 4;'
const ast = new AbstractSyntaxTree(source)
ast.remove({ type: 'Literal', value: 'use strict' })
```

```javascript
const source = 'function hello () { const foo = "bar"; return "world"; }'
const ast = new AbstractSyntaxTree(source)
ast.remove('BlockStatement > VariableDeclaration')
```

### walk

Walks over all nodes

```javascript
const source = 'const a = 1'
const ast = new AbstractSyntaxTree(source)
ast.walk((node, parent) => {})
```

### traverse

Walks over all nodes

```javascript
const source = 'const a = 1'
const ast = new AbstractSyntaxTree(source)
ast.walk({
  enter (node) {},
  leave (node) {}
})
```

### replace

Replace all nodes that match the criteria.

```javascript
const source = 'const a = 1'
const ast = new AbstractSyntaxTree(source)
ast.replace({
  enter (node) {
    if (node.type === 'VariableDeclaration') {
      node.kind = 'let'
    }
    return node
  }
})
```

### prepend

Prepend a node to the body.

```javascript
const source = 'const a = 1;'
const ast = new AbstractSyntaxTree(source)
ast.prepend({
  type: 'ExpressionStatement',
  expression: {
    type: 'Literal',
    value: 'use strict'
  }
})
```

### append

Append a node to the body.

```javascript
const source = 'const a = 1;'
const ast = new AbstractSyntaxTree(source)
ast.append({
  type: 'ExpressionStatement',
  expression: {
    type: 'Literal',
    value: 'test'
  }
})
```

### wrap

Wrap body with given node.

```javascript
const source = 'const a = 1;'
const ast = new AbstractSyntaxTree(source)
ast.wrap(body => {
    return [
      {
        type: 'ExpressionStatement',
        expression: {
          type: 'CallExpression',
          callee: {
            type: 'FunctionExpression',
            params: [],
            body: {
              type: 'BlockStatement',
              body
            }
          },
          arguments: []
        }
      }
    ]
})
```

### unwrap

Change the code to the first BlockStatement body

```javascript
const source = '(function () { console.log(1); }())'
const ast = new AbstractSyntaxTree(source)
ast.unwrap()
ast.toSource()
```

### template

Create ast partials from templates

```javascript
const source = 'console.log(1);'
const ast = new AbstractSyntaxTree(source)
ast.template('const foo = <%= bar %>;' { bar: { type: 'Literal', value: 1 } })
```

### mark

Add cid to all nodes

```javascript
const ast = new AbstractSyntaxTree('const a = 1;')
ast.mark()
assert(ast.first('Program').cid === 1)
assert(ast.first('VariableDeclaration').cid === 2)
```

### toSource

Convert the ast to string.

```javascript
const source = 'const a = 1;'
const ast = new AbstractSyntaxTree(source)
const source = ast.toSource()
```

```javascript
const source = 'const a = 1;'
const ast = new AbstractSyntaxTree(source)
const { source, map } = ast.toSource({ sourceMap: true })
```

### toSourceMap

Generates a source map.

```javascript
const source = 'const a = 1;'
const ast = new AbstractSyntaxTree(source)
const map = ast.toSourceMap()
```

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