abstract-syntax-tree
Advanced tools
Comparing version 2.3.0 to 2.4.0
{ | ||
"name": "abstract-syntax-tree", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"description": "abstract syntax tree", | ||
@@ -36,6 +36,6 @@ "main": "index.js", | ||
"devDependencies": { | ||
"ava": "^1.3.1", | ||
"ava": "^1.4.1", | ||
"benchmark": "^2.1.4", | ||
"codecov": "^3.2.0", | ||
"nyc": "^13.3.0", | ||
"nyc": "^14.0.0", | ||
"standard": "^12.0.1" | ||
@@ -42,0 +42,0 @@ }, |
@@ -177,2 +177,4 @@ # abstract-syntax-tree | ||
Remove uses [estraverse](https://github.com/estools/estraverse) and ensures that no useless nodes are left in the tree. It accepts a string, object or callback as the matching strategy. | ||
```js | ||
@@ -182,3 +184,13 @@ const { parse, remove, generate } = require('abstract-syntax-tree') | ||
const tree = parse(source) | ||
remove(tree, { type: 'Literal', value: 'use strict' }) | ||
remove(tree, 'Literal[value="use strict"]') | ||
// or | ||
// remove(tree, { type: 'Literal', value: 'use strict' }) | ||
// or | ||
// remove(tree, (node) => { | ||
// if (node.type === 'Literal' && node.value === 'use strict') return null | ||
// return node | ||
// }) | ||
console.log(generate(tree)) // 'const b = 4;' | ||
@@ -185,0 +197,0 @@ ``` |
@@ -17,6 +17,21 @@ const estraverse = require('estraverse') | ||
function removeByCallback (tree, callback, options) { | ||
estraverse.replace(tree, { | ||
enter (current) { | ||
if (callback(current) === null) { | ||
return this.remove() | ||
} | ||
}, | ||
leave (current) { | ||
if (isNodeEmpty(current)) { | ||
return this.remove() | ||
} | ||
} | ||
}) | ||
} | ||
function removeByNode (tree, compare, options) { | ||
let count = 0 | ||
estraverse.replace(tree, { | ||
enter (current, parent) { | ||
enter (current) { | ||
if (options.first && count === 1) { | ||
@@ -30,10 +45,4 @@ return this.break() | ||
}, | ||
leave (current, parent) { | ||
if ( | ||
current.expression === null || | ||
( | ||
current.type === 'VariableDeclaration' && | ||
current.declarations.length === 0 | ||
) | ||
) { | ||
leave (current) { | ||
if (isNodeEmpty(current)) { | ||
return this.remove() | ||
@@ -45,7 +54,21 @@ } | ||
module.exports = function remove (tree, selector, options = {}) { | ||
if (typeof selector === 'string') { | ||
return removeBySelector(tree, selector, options) | ||
function isExpressionEmpty (node) { | ||
return node.expression === null | ||
} | ||
function isVariableDeclarationEmpty (node) { | ||
return node.type === 'VariableDeclaration' && node.declarations.length === 0 | ||
} | ||
function isNodeEmpty (node) { | ||
return isExpressionEmpty(node) || isVariableDeclarationEmpty(node) | ||
} | ||
module.exports = function remove (tree, handle, options = {}) { | ||
if (typeof handle === 'string') { | ||
return removeBySelector(tree, handle, options) | ||
} else if (typeof handle === 'function') { | ||
return removeByCallback(tree, handle, options) | ||
} | ||
removeByNode(tree, node => equal(node, selector), options) | ||
removeByNode(tree, node => equal(node, handle), options) | ||
} |
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
26674
383
428