estools/estraverse

Option to ignore properties in fallback="iteration" mode

nzakas opened this issue · 4 comments

Right now, the option for fallback: "iteration" works great so long as all properties containing arrays/objects are intended to be traversed. However, there are some cases where extra information has been added into the AST (such as a parent property) where traversing according to regular rules isn't desirable.

I'd like to add an option that works only when fallback: "iteration" is set that allows us to ignore specific properties by name. So something like:

controller.traverse(tree, {
    enter: function() {},
    exit: function() {},
    fallback: "iteration",
    ignoreKeys: [ "parent" ]
});

So ignoreKeys would be an array of keys that should never be traversed regardless of where they appear in the tree.

I would much rather see fallback take a function from the object to the keys. We can continue to accept the string "iteration" for backwards compatibility, but in the future people should pass Object.keys in to get this behaviour.

I'm not sure I understand what you're describing. Can you give an example?

controller.traverse(tree, {
  enter: function() {},
  exit: function() {},
  fallback: function(node) {
    return Object.keys(node).filter(x => x !== "parent");
  }
});

Ah gotcha. Yeah, that's nicer.