espurify
Clone new AST without extra properties
API
var purifiedAstClone = espurify(originalAst)
Returns new clone of originalAst
but without extra properties.
Leave properties defined in Mozilla JavaScript AST spec only (Note: location informations are eliminated too).
EXAMPLE
var espurify = require('espurify'),
estraverse = require('estraverse'),
esprima = require('esprima'),
syntax = estraverse.Syntax,
assert = require('assert');
var jsCode = 'assert("foo")';
var originalAst = esprima.parse(jsCode, {tolerant: true, loc: true, raw: true});
estraverse.replace(originalAst, {
leave: function (currentNode, parentNode) {
if (currentNode.type === syntax.Literal && typeof currentNode.raw !== 'undefined') {
currentNode['x-verbatim-bar'] = {
content : currentNode.raw,
precedence : 18
};
return currentNode;
} else {
return undefined;
}
}
});
var purifiedClone = espurify(originalAst);
assert.deepEqual(originalAst, {
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
}
},
"x-verbatim-bar": {
content: '"foo"',
precedence: 18
}
}
],
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(purifiedClone, {
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.