I have a Javascript function (got from Function.toString
), and I want to wrap all variable declarations with a function (also in Javascript), E.g.
const value = 42
to const value = wrapper(42)
.
First I thought of using RegEx to get the original values and location and then replace them with the wrapped value, but the RegEx got too complex very fast because of needing to thing about multiline strings and objects, for example. Using RegEx would also impact the ease of other people contibuting to the project.
After that I looked into using a module for this, I found Acorn (used by Babel, Svelte. Parses the Javascript into an ESTree, the spec for Javascript Abstract Syntax Trees): https://github.com/acornjs/acorn, but I couldn't find a way of parsing the ESTree back to a Javascript function declaration.
So, is there a way of parsing the ESTree back to a function, or another better sulution?
let value, x, y; value = 42
? – trincot