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.0.1 to 2.1.0

6

CHANGELOG.md
# abstract-syntax-tree changelog
## 2.1.0
* add: support object selector in the find, has and count methods
* edit: better performance for the remove method (string selector)
* remove: obsolete config.js file
## 2.0.1

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

4

package.json
{
"name": "abstract-syntax-tree",
"version": "2.0.1",
"version": "2.1.0",
"description": "abstract syntax tree",

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

"engines": {
"node": ">=10.14.1"
"node": ">=10.15.0"
},

@@ -16,0 +16,0 @@ "repository": {

@@ -107,4 +107,4 @@ # abstract-syntax-tree

const tree = parse(source)
const nodes = find(tree, 'VariableDeclaration')
console.log(nodes)
console.log(find(tree, 'VariableDeclaration')) // [ { type: 'VariableDeclaration', ... } ]
console.log(find(tree, { type: 'VariableDeclaration' })) // [ { type: 'VariableDeclaration', ... } ]
```

@@ -186,2 +186,3 @@

console.log(has(tree, 'VariableDeclaration')) // true
console.log(has(tree, { type: 'VariableDeclaration' })) // true
```

@@ -196,33 +197,12 @@

console.log(count(tree, 'VariableDeclaration')) // 1
console.log(count(tree, { type: 'VariableDeclaration' })) // 1
```
#### equal
```javascript
const { equal } = require('abstract-syntax-tree')
console.log(equal({ type: 'Literal', value: 42 }, { type: 'Literal', value: 42 })) // true
console.log(equal({ type: 'Literal', value: 41 }, { type: 'Literal', value: 42 })) // false
````
#### template
```javascript
const { template } = require('abstract-syntax-tree')
const literal = template(42)
const nodes = template('const foo = <%= bar %>;', { bar: { type: 'Literal', value: 1 } })
```
### Instance Methods
Almost all of the static methods (excluding parse, generate, template and equal) have their instance equivalents. There are few extra instance methods:
#### prepend
Prepend a node to the body.
```javascript
const AbstractSyntaxTree = require('abstract-syntax-tree')
const { parse, prepend } = require('abstract-syntax-tree')
const source = 'const a = 1;'
const tree = new AbstractSyntaxTree(source)
tree.prepend({
const tree = parse(source)
prepend(tree, {
type: 'ExpressionStatement',

@@ -238,9 +218,7 @@ expression: {

Append a node to the body.
```javascript
const AbstractSyntaxTree = require('abstract-syntax-tree')
const { parse, append } = require('abstract-syntax-tree')
const source = 'const answer = 42'
const tree = new AbstractSyntaxTree(source)
tree.append({
const tree = parse(source)
append(tree, {
type: 'ExpressionStatement',

@@ -254,7 +232,25 @@ expression: {

#### mark
#### equal
Add cid to all nodes
```javascript
const { equal } = require('abstract-syntax-tree')
console.log(equal({ type: 'Literal', value: 42 }, { type: 'Literal', value: 42 })) // true
console.log(equal({ type: 'Literal', value: 41 }, { type: 'Literal', value: 42 })) // false
````
#### template
```javascript
const { template } = require('abstract-syntax-tree')
const literal = template(42)
const nodes = template('const foo = <%= bar %>;', { bar: { type: 'Literal', value: 1 } })
```
### Instance Methods
Almost all of the static methods (excluding parse, generate, template and equal) have their instance equivalents. There are few extra instance methods:
#### mark
```javascript
const AbstractSyntaxTree = require('abstract-syntax-tree')

@@ -269,4 +265,2 @@ const tree = new AbstractSyntaxTree('const a = 1')

Wrap body with given node.
```javascript

@@ -273,0 +267,0 @@ const AbstractSyntaxTree = require('abstract-syntax-tree')

const esquery = require('esquery')
const traverse = require('./traverse')
const equal = require('./equal')
module.exports = function find (tree, selector) {
return esquery(tree, selector)
if (typeof selector === 'string') {
return esquery(tree, selector)
}
let nodes = []
traverse(tree, {
enter (node) {
if (equal(node, selector)) {
nodes.push(node)
}
}
})
return nodes
}
const count = require('./count')
const traverse = require('./traverse')
const equal = require('./equal')
module.exports = function has (tree, selector) {
return count(tree, selector) > 0
if (typeof selector === 'string') {
return count(tree, selector) > 0
}
let found = false
traverse(tree, {
enter (node) {
if (equal(node, selector)) {
found = true
return this.break()
}
}
})
return found
}

@@ -5,18 +5,22 @@ const estraverse = require('estraverse')

function removeBySelector (tree, target, options) {
var nodes = find(tree, target)
// this could be improved by traversing once and
// comparing the current node to the found nodes
// one by one while making the array of nodes smaller too
nodes.forEach(node => removeByNode(tree, node, options))
function removeBySelector (tree, selector, options) {
const nodes = find(tree, selector)
removeByNode(tree, leaf => {
for (let i = 0, ilen = nodes.length; i < ilen; i += 1) {
if (equal(leaf, nodes[i])) {
return true
}
}
return false
}, options)
}
function removeByNode (tree, target, options) {
var count = 0
function removeByNode (tree, compare, options) {
let count = 0
estraverse.replace(tree, {
enter: function (current, parent) {
enter (current, parent) {
if (options.first && count === 1) {
return this.break()
}
if (equal(current, target)) {
if (compare(current)) {
count += 1

@@ -26,3 +30,3 @@ return this.remove()

},
leave: function (current, parent) {
leave (current, parent) {
if (

@@ -41,7 +45,7 @@ current.expression === null ||

module.exports = function remove (tree, target, options = {}) {
if (typeof target === 'string') {
return removeBySelector(tree, target, options)
module.exports = function remove (tree, selector, options = {}) {
if (typeof selector === 'string') {
return removeBySelector(tree, selector, options)
}
removeByNode(tree, target, options)
removeByNode(tree, node => equal(node, selector), options)
}

Sorry, the diff of this file is not supported yet

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