> * Is there some way to match based on given ~prefix~? I would like to match "_name" instead of "_.name".
Great timing. That's easily done with the new Shaper.match that just landed:
function isPrefixed(str) {
return str[0] === "_";
}
Shaper.match("$", Shaper.parseExpression("_id"), {$: {value: isPrefixed}});
> * Is there some way to shape the AST dynamically? Ideally it should add "var that = this;" to the scope of the parent. I guess this could get tricky if you have a lot of nesting... Let's assume just one level of nesting for now.
I thought it was pretty dynamic already. ;) Yes it's possible to insert code, not just replace nodes. Keeping track of the inner-most enclosing function is easy via an explicit function stack, have a look at how asserter does it. Inserting a new statement at the beginning of a SCRIPT (which is the type of a function.body) is also possible.
You need to insert the new statement in the script.children and script.srcs array.
The statement node will be inserted at children[0] position (it's now the first statement in the script).
*The script.srcs array contains text fragments before, between and after the children nodes - mostly whitespace. srcs[0] is the text fragment just before the first node (will contain the opening block brace) so don't change it. srcs[1] is text fragment between children[0] and [1] so insert a newline here or something.
Shaper.insertArgument is crafted to do this conveniently for function-call argument lists so you can have a look at that. I'll refactor it or create a similar Shaper.insertStatement some time unless someone beats me to it.
/Olov