question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

traverse/transform: path.remove() or any other way to change the collection does not work

See original GitHub issue

Hi,

Changing the parent collection (e.g. expressions) in a traverse() or transform() has an effect on the traversion. E.g. the next node will be skipped if I use this (in coffeescript):

    RegExpTree.traverse(ast, {

      Char: (path) ->
        {node, parent, property, index} = path
        if parent.type == "Alternative"
          if node.value == " " or node.value == "\n"
            path.remove()
      }
    )

I assume, the index is used in the iteration and that changes when removing the item from the collection.

Perhaps I am doing it wrong? but I assume the remove() of NodePath is for exactly this purpose. What is the preferred way to delete a node inside traverse()?

As a workaround I tried to set node.type = null or to an undefined type name, but this gives me an error. Would be nice if traverse() and generate() could skip such a node. Also optimize could remove such nodes.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:36 (36 by maintainers)

github_iconTop GitHub Comments

1reaction
hg42commented, Apr 6, 2017

nope, these lines don’t trigger the problem:

    // Remove 'b' itself.
    path.remove();
    // Remove 'c' as well.
    cPath.remove();

because after deleting both nodes from the array processing is finished, so the wrong index in ast-traverse doesn’t matter any more. The problem only appears when you delete a node and return to ast-traverse and want to process a next node. So, you should change it to:

    if(value == "b")
        path.remove();
    else if(value == "c")
        path.remove();

question is how to check expectations. You already have a second traverse which checks if “b” and “c” are removed. This may be a matter of taste, but IMHO, all this code hides the intention of the test. Many things are already tested in another test. The simplest check would use generate() and expect a result. Or if you do not want to use generate() you could check values of AST nodes instead, e.g. you could look for “a” and “d” at the expected positions.

0reactions
hg42commented, Apr 12, 2017

if the node is totally new, the corresponding NodePath would be created, BUT if the node already has a NodePath it will be reused and has the wrong parent! I already changed getForNode to force given parameters on the cached path. I also had code to create/change NodePaths in replace, but it didn’t work because getForNode only used the cached path as it is. I think I’ll add that to setChild/appendChild, ok?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Removing path from Python search module path
append however when I run the command sys.path.remove to 'delete' the path I appended, I am unable to do so. Is there a...
Read more >
Removing Invalid Entries in the Add/Remove Programs Tool
Changing or removing a program may result in a number of messages about files that cannot be located, but are needed to complete...
Read more >
Delete a directory or file using Python - GeeksforGeeks
os.remove() method in Python is used to remove or delete a file path. This method can not remove or delete a directory.
Read more >
Edit collections - Android - Currents Help - Google Support
Delete a collection ​​ Posts reshared to the collection remain after the collection is deleted. At the bottom, tap Collections. Tap a collection....
Read more >
rm - Remove a directory entry - IBM
rm removes files if it is a valid path name. ... If a file does not have write permission set, you are asked...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found