-
Notifications
You must be signed in to change notification settings - Fork 56
Allow accessing node parents in visitor function #51
Comments
Hi @yxliang01, can you describe some use cases for this function? I'm not sure I understand well how it would be used. |
@federicobond Like if you want to write a custom traverser of the AST tree, this function can provide information like whether an attribute of a node is yet another AST node (This is actually my case). While the current definition of this function is very simple, it's really useful :) and I feel it's a good idea to export this to provide some encapsulations. |
I understand your concern. However, I would suggest to just check if the node is an object and has a |
I think we can take this opportunity to introduce a better traverser. Please let me know what constraints are driving your custom traverser implementation, as we might be able to do something on that front. |
@federicobond I basically need the object path of each AST node in the AST tree. So that, I can directly visit the previous node visited node by the object path. I deal with it by adapting the |
That is a pretty common usage. The format of the AST is esprima-compatible, so preprocessing it with something like ast-parents should work well too. |
I see. Thanks for pointing this. But, this is quite inefficient given that need to traverse the AST for once more and increases memory space by quite much I feel. |
@federicobond any suggestion for what should be done for this issue? Otherwise, this will be stale :( |
Hey! Sorry, my last few days have been pretty hectic. I think the lib could implement this functionality by passing a second argument to each visit call, with the appropiate parent chain. Would be happy to review a pull request to that end. As for checking whether something is an AST node, I think you can safely rely on a check for an object with a |
@federicobond I am also happy to contribute to that :) . Will do it bit latter after I have done my current important tasks. |
To be sure, @federicobond did you mean passing the parent node object to the visitor? Or, the object path for the whole AST(my need)? |
Yes, passing the full object path makes a lot of sense. |
Since the AST format of this library is customized, I feel exporting the function
_isASTNode
is generally favored as it guarantees that when the format of output of this library changes, this check will/should always be up-to-date.@federicobond do you agree?
The text was updated successfully, but these errors were encountered: