escodegen's verbatim option

115 views Asked by At

I'm stumped by the escodegen verbatim option. It claims that the value of options.verbatim should be a string which is taken to be a property name which, if present on any expression node (using type="Literal" in my tests) the value of that property will be used instead of whatever escodegen would have generated for that node.

Here's my short test program: ...

"use strict";

var esprima = require ("esprima");
var esgen = require ("escodegen");
var tr = require ("estraverse");
var util = require ("util");

var code = `
var x;
while (x) {
doSomething(x);
}
`;


var a = esprima.parse(code);
//console.log (util.inspect(a, {depth:null}));
//console.log (esgen .generate(a));


tr.traverse (a, {
enter: function (node, parent) {
var pre, post;
if (node.type === "BlockStatement") {
// want to wrap the statements within the block in html tags
pre = `<div class="block ${parent.type}">`;
post = `</div>`;
node.body.unshift (literal(pre));
node.body.push (literal(post));
} // if

function literal(_html) {
return {type: "Literal", value: 42, raw: _html};
} // literal

} // enter
});

//console.log (util.inspect(a, null, 4));
console.log (esgen.generate (a, {verbatim: "raw"}));

...

and the output: ...

var x;
while (x) {
    42
    doSomething(x);
    42
}

...

I'm obviously missing something! Any help much appreciated.

-- Rich

0

There are 0 answers