The following is the code that I have written
`js
var esprima = require('esprima');
var escodegen = require('escodegen');
var a = "var a = 2";
var ast = esprima.tokenize(a);
var output = escodegen.generate(ast);
console.log(output);
`
I am able to tokenize the code string but I am getting error generating the code back. I went through multiple samples, Everywhere the same pattern is followed. I don't understand what I am doing wrong.
The function
esprima.tokenize
does not generate an AST, just an array of tokens. What you want to use isesprima.parse
.Try this:
It will work