Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

abstract-syntax-tree

Package Overview
Dependencies
Maintainers
3
Versions
67
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

abstract-syntax-tree - npm Package Compare versions

Comparing version 2.1.1 to 2.1.2

src/reduce.js

4

CHANGELOG.md
# abstract-syntax-tree changelog
## 2.1.2
add: reduce method
## 2.1.1

@@ -4,0 +8,0 @@

@@ -16,2 +16,3 @@ const find = require('./src/find')

const parse = require('./src/parse')
const reduce = require('./src/reduce')
const template = require('./src/template')

@@ -86,2 +87,6 @@ const sourcemap = require('./src/sourcemap')

static reduce (tree, callback, accumulator) {
return reduce(tree, callback, accumulator)
}
constructor (source, options = {}) {

@@ -155,2 +160,6 @@ if (typeof source === 'string') {

reduce (callback, accumulator) {
return reduce(this._tree, callback, accumulator)
}
prepend (node) {

@@ -157,0 +166,0 @@ return prepend(this._tree, node)

2

package.json
{
"name": "abstract-syntax-tree",
"version": "2.1.1",
"version": "2.1.2",
"description": "abstract syntax tree",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -5,5 +5,17 @@ # abstract-syntax-tree

> Abstract Syntax Tree
## What is an abstract syntax tree?
## Table of Contents
- [Background](#background)
- [Install](#install)
- [Usage](#usage)
- [API](#api)
- [REPL](https://buxlabs.pl/en/tools/js/ast)
- [Maintainers](#maintainers)
- [Contributing](#contributing)
- [License](#license)
## Background
An abstract syntax tree is a way to represent the source code. In case of this library it is represented in the [estree](https://github.com/estree/estree) format.

@@ -44,11 +56,13 @@

## Installation
The goal of this library is to consolidate common abstract syntax tree operations in one place. It uses a variety of libriaries under the hood based on their performance and flexibility, e.g. [cherow](https://github.com/cherow/cherow) for parsing and [astring](https://github.com/davidbonnet/astring) for source code generation.
`npm install abstract-syntax-tree`
The library exposes a set of utility methods that can be useful for analysis or transformation of abstract syntax trees. It supports functional and object-oriented programming style.
## How does it work?
## Install
The library exposes a set of utility methods that can be useful for analysis or transformation of abstract syntax trees. It supports functional and object-oriented programming style.
```bash
npm install abstract-syntax-tree
```
## Examples
## Usage

@@ -75,3 +89,3 @@ ```js

```javascript
```js
const { parse } = require('abstract-syntax-tree')

@@ -83,5 +97,17 @@ const source = 'const answer = 42'

The library uses [cherow](https://github.com/cherow/cherow) to create an [estree](https://github.com/estree/estree) compatible abstract syntax tree. All [cherow parsing options](https://github.com/cherow/cherow#options) can be passed to the parse method.
```js
const { parse } = require('abstract-syntax-tree')
const source = 'const answer = 42'
const tree = parse(source, {
loc: true,
ranges: true
})
console.log(tree) // { type: 'Program', body: [ ... ], loc: {...} }
```
#### generate
```javascript
```js
const { parse, generate } = require('abstract-syntax-tree')

@@ -93,5 +119,7 @@ const source = 'const answer = 42'

The library uses [astring](https://github.com/davidbonnet/astring) to generate the source code. All [astring generate options](https://github.com/davidbonnet/astring#api) can be passed to the generate method.
#### walk
```javascript
```js
const { parse, walk } = require('abstract-syntax-tree')

@@ -108,3 +136,3 @@ const source = 'const answer = 42'

```javascript
```js
const { parse, find } = require('abstract-syntax-tree')

@@ -119,3 +147,3 @@ const source = 'const answer = 42'

```javascript
```js
const { parse, traverse } = require('abstract-syntax-tree')

@@ -132,3 +160,3 @@ const source = 'const answer = 42'

```javascript
```js
const { parse, replace } = require('abstract-syntax-tree')

@@ -149,3 +177,3 @@ const source = 'const answer = 42'

```javascript
```js
const { parse, remove, generate } = require('abstract-syntax-tree')

@@ -160,3 +188,3 @@ const source = '"use strict"; const b = 4;'

```javascript
```js
const { parse, each } = require('abstract-syntax-tree')

@@ -172,3 +200,3 @@ const source = 'const foo = 1; const bar = 2;'

```javascript
```js
const { parse, first } = require('abstract-syntax-tree')

@@ -182,3 +210,3 @@ const source = 'const answer = 42'

```javascript
```js
const { parse, last } = require('abstract-syntax-tree')

@@ -190,5 +218,20 @@ const source = 'const answer = 42'

#### reduce
```js
const { parse, reduce } = require('abstract-syntax-tree')
const source = 'const a = 1, b = 2'
const tree = parse(source)
const value = reduce(tree, (sum, node) => {
if (node.type === 'Literal') {
sum += node.value
}
return sum
}, 0)
console.log(value) // 3
```
#### has
```javascript
```js
const { parse, has } = require('abstract-syntax-tree')

@@ -203,3 +246,3 @@ const source = 'const answer = 42'

```javascript
```js
const { parse, count } = require('abstract-syntax-tree')

@@ -214,3 +257,3 @@ const source = 'const answer = 42'

```javascript
```js
const { parse, prepend } = require('abstract-syntax-tree')

@@ -230,3 +273,3 @@ const source = 'const a = 1;'

```javascript
```js
const { parse, append } = require('abstract-syntax-tree')

@@ -246,3 +289,3 @@ const source = 'const answer = 42'

```javascript
```js
const { equal } = require('abstract-syntax-tree')

@@ -255,3 +298,3 @@ console.log(equal({ type: 'Literal', value: 42 }, { type: 'Literal', value: 42 })) // true

```javascript
```js
const { template } = require('abstract-syntax-tree')

@@ -268,3 +311,3 @@ const literal = template(42)

```javascript
```js
const AbstractSyntaxTree = require('abstract-syntax-tree')

@@ -279,3 +322,3 @@ const tree = new AbstractSyntaxTree('const a = 1')

```javascript
```js
const AbstractSyntaxTree = require('abstract-syntax-tree')

@@ -307,3 +350,3 @@ const source = 'const a = 1'

```javascript
```js
const AbstractSyntaxTree = require('abstract-syntax-tree')

@@ -313,3 +356,3 @@ const source = '(function () { console.log(1); }())'

tree.unwrap()
console.log(tree.source) // 'console.log(1);'
console.log(tree.source) // console.log(1);
```

@@ -319,12 +362,39 @@

#### type
#### body
Gives you the body of the root node.
#### source
Gives you access to the source code representation of the abstract syntax tree.
```js
const AbstractSyntaxTree = require('abstract-syntax-tree')
const source = 'const foo = "bar";'
const tree = new AbstractSyntaxTree(source)
console.log(tree.source) // const foo = "bar";
```
#### map
Gives you the source map of the source code.
### Setters
#### body
Lets you set the body of the root node.
## Maintainers
[@emilos](https://github.com/emilos).
## Contributing
All contributions are highly appreciated! [Open an issue](https://github.com/buxlabs/abstract-syntax-tree/issues/new) or a submit PR.
The lib follows the tdd approach and is expected to have a high code coverage. Please follow the [Contributor Covenant Code of Conduct](https://github.com/buxlabs/abstract-syntax-tree/blob/master/CODE_OF_CONDUCT.md).
## License
MIT © buxlabs
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc