abstract-syntax-tree
Advanced tools
Comparing version 2.2.0 to 2.3.0
# abstract-syntax-tree changelog | ||
## 2.3.0 | ||
* edit: append and prepend mutate existing arrays | ||
## 2.2.0 | ||
@@ -4,0 +8,0 @@ |
@@ -92,3 +92,3 @@ const find = require('./src/find') | ||
if (typeof source === 'string') { | ||
this._tree = typeof source === 'string' ? parse(source, { loc: true, ...options }) : source | ||
this._tree = parse(source, { loc: true, ...options }) | ||
} else { | ||
@@ -95,0 +95,0 @@ this._tree = source |
{ | ||
"name": "abstract-syntax-tree", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "abstract syntax tree", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "ava 'test/*.js'", | ||
"test": "ava \"test/*.js\"", | ||
"lint": "standard", | ||
"benchmark": "ava 'test/benchmarks/*.js'", | ||
"coverage": "nyc npm test" | ||
"coverage": "nyc npm test", | ||
"coverage:report": "nyc --reporter=text-lcov npm test > coverage.lcov && codecov" | ||
}, | ||
@@ -37,2 +38,3 @@ "engines": { | ||
"benchmark": "^2.1.4", | ||
"codecov": "^3.2.0", | ||
"nyc": "^13.3.0", | ||
@@ -39,0 +41,0 @@ "standard": "^12.0.1" |
# abstract-syntax-tree | ||
![npm](https://img.shields.io/npm/v/abstract-syntax-tree.svg) ![build](https://img.shields.io/codeship/c6391230-e90c-0136-c202-269c372fd6f7/master.svg) | ||
![build](https://img.shields.io/codeship/c6391230-e90c-0136-c202-269c372fd6f7/master.svg) ![Codecov](https://img.shields.io/codecov/c/github/buxlabs/abstract-syntax-tree.svg) ![npm](https://img.shields.io/npm/v/abstract-syntax-tree.svg) | ||
> Abstract Syntax Tree | ||
> A library for working with abstract syntax trees. | ||
@@ -87,2 +87,4 @@ ## Table of Contents | ||
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 | ||
@@ -95,4 +97,2 @@ const { parse } = require('abstract-syntax-tree') | ||
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 | ||
@@ -110,2 +110,4 @@ const { parse } = require('abstract-syntax-tree') | ||
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. | ||
```js | ||
@@ -118,6 +120,6 @@ const { parse, generate } = require('abstract-syntax-tree') | ||
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 | ||
Walk method is a thin layer over [estraverse](https://github.com/estools/estraverse). | ||
```js | ||
@@ -135,2 +137,4 @@ const { parse, walk } = require('abstract-syntax-tree') | ||
Find supports two traversal methods. You can pass an [esquery](https://github.com/estools/esquery) compatible selector or pass an object that will be compared to every node in the tree. The method returns an array of nodes. | ||
```js | ||
@@ -146,2 +150,4 @@ const { parse, find } = require('abstract-syntax-tree') | ||
Traverse method accepts a configuration object with enter and leave callbacks. It allows multiple transformations in one traversal. | ||
```js | ||
@@ -159,2 +165,4 @@ const { parse, traverse } = require('abstract-syntax-tree') | ||
Replace extends [estraverse](https://github.com/estools/estraverse) by handling replacement of give node with multiple nodes. It will also remove given node if `null` is returned. | ||
```js | ||
@@ -250,3 +258,3 @@ const { parse, replace } = require('abstract-syntax-tree') | ||
Append lets you to push nodes to the body of the abstract syntax tree. It accepts estree nodes as input. | ||
Append pushes nodes to the body of the abstract syntax tree. It accepts estree nodes as input. | ||
@@ -283,3 +291,3 @@ ```js | ||
It also lets you pass raw strings that will be converted into abstract syntax tree under the hood. Please note that this approach might make the code run a bit slower due to an extra interpretation step. | ||
Strings will be converted into abstract syntax tree under the hood. Please note that this approach might make the code run a bit slower due to an extra interpretation step. | ||
@@ -295,3 +303,3 @@ ```js | ||
Prepend lets you unshift nodes to the body of the abstract syntax tree. Accepts estree nodes or strings as input, same as append. | ||
Prepend unshifts nodes to the body of the abstract syntax tree. Accepts estree nodes or strings as input, same as append. | ||
@@ -382,7 +390,7 @@ ```js | ||
Gives you the body of the root node. | ||
Gives the body of the root node. | ||
#### source | ||
Gives you access to the source code representation of the abstract syntax tree. | ||
Gives access to the source code representation of the abstract syntax tree. | ||
@@ -398,3 +406,3 @@ ```js | ||
Gives you the source map of the source code. | ||
Gives the source map of the source code. | ||
@@ -405,3 +413,3 @@ ### Setters | ||
Lets you set the body of the root node. | ||
Sets the body of the root node. | ||
@@ -408,0 +416,0 @@ ## Maintainers |
const template = require('./template') | ||
function getNode (input) { | ||
function normalizeInput (input) { | ||
if (typeof input === 'string') return template(input) | ||
@@ -8,16 +8,18 @@ return input | ||
function addNode (tree, node) { | ||
if (Array.isArray(node)) return tree.concat(node) | ||
tree.push(node) | ||
return tree | ||
function appendInput (tree, input) { | ||
if (Array.isArray(input)) { | ||
input.forEach(node => tree.push(node)) | ||
} else { | ||
tree.push(input) | ||
} | ||
} | ||
module.exports = function append (tree, input) { | ||
const node = getNode(input) | ||
input = normalizeInput(input) | ||
if (Array.isArray(tree)) { | ||
tree = addNode(tree, node) | ||
appendInput(tree, input) | ||
} else if (Array.isArray(tree.body)) { | ||
tree.body = addNode(tree.body, node) | ||
appendInput(tree.body, input) | ||
} | ||
return tree | ||
} |
const template = require('./template') | ||
function getNode (input) { | ||
function normalizeInput (input) { | ||
if (typeof input === 'string') return template(input) | ||
@@ -8,16 +8,18 @@ return input | ||
function addNode (tree, node) { | ||
if (Array.isArray(node)) return node.concat(tree) | ||
tree.unshift(node) | ||
return tree | ||
function prependNode (tree, input) { | ||
if (Array.isArray(input)) { | ||
input.reverse().forEach(node => tree.unshift(node)) | ||
} else { | ||
tree.unshift(input) | ||
} | ||
} | ||
module.exports = function append (tree, input) { | ||
const node = getNode(input) | ||
module.exports = function prepend (tree, input) { | ||
input = normalizeInput(input) | ||
if (Array.isArray(tree)) { | ||
tree = addNode(tree, node) | ||
prependNode(tree, input) | ||
} else if (Array.isArray(tree.body)) { | ||
tree.body = addNode(tree.body, node) | ||
prependNode(tree.body, input) | ||
} | ||
return tree | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25768
364
416
5