tagged-template-to-ast
v2.0.0
Published
Tagged template string function for AST.
Downloads
649
Readme
tagged-template-to-ast
toAST
template tagged function that inline AST Node to source code.
Combine source code with AST Node.
for more information about tagged template strings and see developer.mozilla.org/docs/Web/JavaScript/Reference/template_strings
Installation
npm install tagged-template-to-ast
Usage
toAST
: AST
toAST
tag function help to inline AST Node to source code.
toAST`var foo = ${ASTNode}`; // return AST
Example:
import toAST from "tagged-template-to-ast"
// `function(){}` expression
var nodeForInline = {
"type": "FunctionExpression",
"id": null,
"params": [],
"defaults": [],
"body": {
"type": "BlockStatement",
"body": []
},
"generator": false,
"expression": false
};
var astNode = toAST`var a = ${nodeForInline};`;
// astNode is the AST of `var a = function (){};`
Combine code
with AST Node.
import {parse} from "esprima"
import toAST from "tagged-template-to-ast"
var nodeForInline = parse('"string"');
var astNode = toAST`var a = ${nodeForInline}`;
// astNode is the AST of `var a = "string";`
Could inline to body block too!
import {parse} from "esprima"
import toAST from "tagged-template-to-ast"
import astEqual from "ast-equal"
var aNodeForBody = parse("sum(1);");
var astNode = toAST`if(true){
${aNodeForBody}
}`;
var expected = `if(true){
sum(1);
}`;
// test helper to assert JavaScript AST equality.
// https://github.com/azu/ast-equal
astEqual(astNode, expected);
Tests
npm test
Contributing
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
License
MIT