gkz/LiveScript

Problem when trying to reassign an object altered with the LS delete operator with the latter's return value

ymeine opened this issue · 4 comments

The title was hard to find for me, to make it clear and concise, but actually the problem is simple.

Whether this is a good practice or not, here is what I've done:

During one of my developments, I tried to do something I often do which is recursively traversing a tree from the root to the leaves, removing some properties on each node.
Here the tree had a simple from:

  • the root
  • each node has only ONE child node, except the leaf of course

Simple right?

Now the problem comes from the fact that the property I wanted to remove on each node during the operation was the child node itself.

So here was my first implementation:

let node = root
    while node.child?
        node = delete node.child

Considering the documentation, I would expect it to work.

However, looking at the compiled JavaScript:

(function(node){
  while (node.child != null) {
    node = node.child, delete node.child;
  }
}.call(this, root));

we see that the problem comes from the order of the operations: the node variable is assigned before the property is deleted, but here we wanted to delete a property of node.

This has been done this way to avoid a temporary variable I guess.

In the case I shew, there is a simple workaround, using an implicit variable acting as the missing temporary variable: the that reference coming from the test.

let node = root
    while node.child?
        delete node.child
        node = that

However I think this is an issue, or otherwise change the documentation to be clear on this fact.

Looks like a bug to me. I believe you're right about the cause, and your solution looks fine.

I wish all issues were done this well.

Agreed with @michaelficarra.
What do we do ? Traverse LHS ?

Edit : could maybe be reporter to @satyr

This has been done this way to avoid a temporary variable I guess.

Yep.

Traverse LHS ?

Or just discard this optimization. It's half-baked anyway:

$ coco -bce '[a, b] = delete c.d'
var ref$, a, b;
ref$ = (ref$ = c.d, delete c.d, ref$), a = ref$[0], b = ref$[1];

satyr/coco@8410f8c (indeed) works.

tagged for 1.2