New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.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.3 to 2.2.0

6

CHANGELOG.md
# abstract-syntax-tree changelog
## 2.2.0
* add: possibility to create a new abstract syntax tree instance without params
* add: append and prepend accept string as input
* update: ava
## 2.1.3

@@ -4,0 +10,0 @@

2

index.js

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

constructor (source, options = {}) {
constructor (source = '', options = {}) {
if (typeof source === 'string') {

@@ -93,0 +93,0 @@ this._tree = typeof source === 'string' ? parse(source, { loc: true, ...options }) : source

{
"name": "abstract-syntax-tree",
"version": "2.1.3",
"version": "2.2.0",
"description": "abstract syntax tree",

@@ -35,3 +35,3 @@ "main": "index.js",

"devDependencies": {
"ava": "^1.2.1",
"ava": "^1.3.1",
"benchmark": "^2.1.4",

@@ -38,0 +38,0 @@ "nyc": "^13.3.0",

@@ -172,3 +172,3 @@ # abstract-syntax-tree

const source = '"use strict"; const b = 4;'
const ast = parse(source)
const tree = parse(source)
remove(tree, { type: 'Literal', value: 'use strict' })

@@ -242,13 +242,32 @@ console.log(generate(tree)) // 'const b = 4;'

#### prepend
#### append
Append lets you to push nodes to the body of the abstract syntax tree. It accepts estree nodes as input.
```js
const { parse, prepend } = require('abstract-syntax-tree')
const source = 'const a = 1;'
const { parse, append } = require('abstract-syntax-tree')
const source = 'const answer = 42'
const tree = parse(source)
prepend(tree, {
append(tree, {
type: 'ExpressionStatement',
expression: {
type: 'Literal',
value: 'use strict'
expression: {
type: "CallExpression",
callee: {
type: 'MemberExpression',
object: {
type: 'Identifier',
name: 'console'
},
property: {
type: 'Identifier',
name: 'log'
},
computed: false
},
arguments: [
{
type: 'Identifier',
name: 'answer'
}
]
}

@@ -258,3 +277,3 @@ })

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

@@ -265,7 +284,18 @@ ```js

const tree = parse(source)
append(tree, {
append(tree, 'console.log(answer)')
```
#### prepend
Prepend lets you unshift nodes to the body of the abstract syntax tree. Accepts estree nodes or strings as input, same as append.
```js
const { parse, prepend } = require('abstract-syntax-tree')
const source = 'const a = 1;'
const tree = parse(source)
prepend(tree, {
type: 'ExpressionStatement',
expression: {
type: 'Literal',
value: 'test'
value: 'use strict'
}

@@ -272,0 +302,0 @@ })

@@ -1,8 +0,22 @@

module.exports = function append (tree, node) {
const template = require('./template')
function getNode (input) {
if (typeof input === 'string') return template(input)
return input
}
function addNode (tree, node) {
if (Array.isArray(node)) return tree.concat(node)
tree.push(node)
return tree
}
module.exports = function append (tree, input) {
const node = getNode(input)
if (Array.isArray(tree)) {
tree.push(node)
tree = addNode(tree, node)
} else if (Array.isArray(tree.body)) {
tree.body.push(node)
tree.body = addNode(tree.body, node)
}
return tree
}

@@ -1,8 +0,22 @@

module.exports = function prepend (tree, node) {
const template = require('./template')
function getNode (input) {
if (typeof input === 'string') return template(input)
return input
}
function addNode (tree, node) {
if (Array.isArray(node)) return node.concat(tree)
tree.unshift(node)
return tree
}
module.exports = function append (tree, input) {
const node = getNode(input)
if (Array.isArray(tree)) {
tree.unshift(node)
tree = addNode(tree, node)
} else if (Array.isArray(tree.body)) {
tree.body.unshift(node)
tree.body = addNode(tree.body, node)
}
return tree
}
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