What is espurify?
The espurify npm package is used to create a deep copy of an ESTree-compliant AST (Abstract Syntax Tree) while removing extra properties that are not part of the ESTree specification. This is useful for ensuring that ASTs are standardized and can be used in various tools and libraries that expect a clean, compliant AST.
What are espurify's main functionalities?
Deep Copy of ESTree AST
This feature allows you to create a deep copy of an ESTree-compliant AST while removing any extra properties that are not part of the ESTree specification. The code sample demonstrates parsing JavaScript code into an AST using Acorn, and then purifying that AST using espurify.
const espurify = require('espurify');
const acorn = require('acorn');
const code = 'const x = 42;';
const ast = acorn.parse(code, { ecmaVersion: 2020 });
const purifiedAst = espurify(ast);
console.log(JSON.stringify(purifiedAst, null, 2));
Other packages similar to espurify
estraverse
Estraverse is a package for traversing and manipulating ESTree-compliant ASTs. While it does not specifically purify ASTs by removing non-standard properties, it provides powerful traversal and manipulation capabilities that can be used in conjunction with espurify.
escodegen
Escodegen is a package for generating JavaScript code from an ESTree-compliant AST. It focuses on code generation rather than purification, but it can be used to regenerate code from a purified AST created by espurify.
esprima
Esprima is a high-performance, standard-compliant JavaScript parser that produces ESTree-compliant ASTs. While it does not purify ASTs, it can be used to generate the initial AST that can then be purified using espurify.
espurify
Eliminate extra properties from AST output
EXAMPLE
var espurify = require('espurify'),
esprima = require('esprima'),
assert = require('assert');
var jsCode = 'assert("foo")';
var ast = esprima.parse(jsCode, {tolerant: true, loc: true});
var purified = espurify(ast);
assert.deepEqual(ast, {
type: 'Program',
body: [
{
type: 'ExpressionStatement',
expression: {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'assert',
loc: {
start: {
line: 1,
column: 0
},
end: {
line: 1,
column: 6
}
}
},
arguments: [
{
type: 'Literal',
value: 'foo',
raw: '"foo"',
loc: {
start: {
line: 1,
column: 7
},
end: {
line: 1,
column: 12
}
}
}
],
loc: {
start: {
line: 1,
column: 0
},
end: {
line: 1,
column: 13
}
}
},
loc: {
start: {
line: 1,
column: 0
},
end: {
line: 1,
column: 13
}
}
}
],
loc: {
start: {
line: 1,
column: 0
},
end: {
line: 1,
column: 13
}
},
errors: []
});
assert.deepEqual(purified, {
type: 'Program',
body: [
{
type: 'ExpressionStatement',
expression: {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'assert'
},
arguments: [
{
type: 'Literal',
value: 'foo'
}
]
}
}
]
});
INSTALL
via npm
Install
$ npm install --save espurify
Use
var espurify = require('espurify');
via bower
Install
$ bower install --save espurify
Load (espurify
function is exported)
<script type="text/javascript" src="./path/to/bower_components/espurify/build/espurify.js"></script>
AUTHOR
LICENSE
Licensed under the MIT license.