Wrapping a list of statements

Skip to first unread message


Dec 30, 2011, 5:30:40 PM12/30/11
to JSShaper
Hi Olov,

I'm trying to figure out how I might wrap the statements in a function
body within a while statement. I.e. something like this:

function () {
var i = 0;

gets transformed to

function () {
while (true) {
var i = 0;

simply copying the children array does not seem to work, and I'm not
too clear how I should use insertBefore/After. Could you explain why
they need to take a Ref, and what the parameters of that Ref serve to

Olov Lassus

Dec 30, 2011, 7:01:44 PM12/30/11
to jssh...@googlegroups.com
On Fri, Dec 30, 2011 at 11:30 PM, jez <jez...@gmail.com> wrote:
> Hi Olov,

Thanks for trying out Shaper and posting your questions to the list.

[Assuming that you have the function node bound to var f, and that you
want to wrap the entire f body inside the while loop]

You can use the Shaper.insertBefore and Shaper.remove helpers if you
wish. This should get you started:
Shaper.insertBefore(new Ref(f.body, "children", 0),
Shaper.parse("while (true) { $; }"))

Ref's are used to provide another level of indirection, in essence a
pointer to the pointer. This is a consequence of Shaper not storing
up-pointers in each node (i.e. node.parent and node.parentProperty).

You can also manipulate the children and srcs arrays directly. srcs is
an array of children.length + 1 text fragments (leading, separating
and trailing) so it must be kept updated accordingly.

var w = Shaper.parse("while (true) { }");

w.body.children = f.body.children;
w.body.srcs = f.body.srcs;

f.body.children = [w];
f.body.srcs = ["{ ", " }"];

A third alternative is to go half-nuts and assign w.body = f.body as
long as you stay alert (further modifications and w.body is of BLOCK
type while f.body is a SCRIPT).

Having said all this, we should create a multiple-statement-wrapping
helper (and probably a few others too). Also, once Shaper.replace is
reworked the same way Shaper.match was it should be able to do most of


Reply all
Reply to author
0 new messages