Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

AddMutationVisitor#remove API to remove nodes from the tree#305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
nvasilevski wants to merge1 commit intoruby-syntax-tree:main
base:main
Choose a base branch
Loading
fromShopify:allow-remove-nodes-from-the-tree

Conversation

nvasilevski
Copy link

@nvasilevskinvasilevski commentedFeb 9, 2023
edited
Loading

This PR allows extendsMutationVisitor to allow removing nodes from the tree.
The API we are going with is havingremove helper method along withmutate to define patterns for nodes to be removed from the tree.
It's not a necessity but provides a nicer API compared to:

visitor.mutate("<pattern for node to be removed>")do |node|RemovedNode.newend

allowing forvisitor.remove("<pattern for node to be removed>") to be used.

Under the hood we are mutating the tree by substituting nodes that match the removal pattern with an instance ofRemovedNode which purpose is to keep the tree valid and be formatted as an empty string during tree dumping.

@nvasilevskinvasilevskiforce-pushed theallow-remove-nodes-from-the-tree branch fromc1fbb73 toa178845CompareFebruary 13, 2023 16:26
Copy link
Member

@kddnewtonkddnewton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This is a great start!

I have one or two issues at the moment. The biggest issue is that I think I didn't design the#copy interface well. If you passnil as the value of one of the fields, it uses the value that's already present.

e.g., if you were to runIfNode#copy and passpredicate: nil, then it wouldn't change the tree at all.

In this case I think one of the precursors to this PR could be changing the copy methods to instead use the current values as the default values, as in change:

defcopy(predicate:nil,statements:nil,consequent:nil,location:nil)node=IfNode.new(predicate:predicate ||self.predicate,statements:statements ||self.statements,consequent:consequent ||self.consequent,location:location ||self.location)node.comments.concat(comments.map(&:copy))nodeend

to

defcopy(predicate:self.predicate,statements:self.statements,consequent:self.consequent,location:self.location)node=IfNode.new(predicate:predicate,statements:statements,consequent:consequent,location:location)node.comments.concat(comments.map(&:copy))nodeend

At the moment most of the places where you're removing nodes is going to result in not actually removing them because of this.

The other thing I have an issue with is that removing nodes from the tree could easily break the tree's expectations.

For example, in your example you have it removing a vcall withdo_more_work as the value. If that were the predicate of an if node, as in:if do_more_work and the predicate went away, then you now can't format the tree at all.

For these reasons, I think we need aRemovedNode that you put in place of the existing node when you replace it. ThatRemovedNode should have the same API as the other nodes in the tree so that it can be queried as normal.

@nvasilevskinvasilevskiforce-pushed theallow-remove-nodes-from-the-tree branch 4 times, most recently frombf2e468 to9a49f30CompareJuly 28, 2023 17:41
@nvasilevskinvasilevskiforce-pushed theallow-remove-nodes-from-the-tree branch from9a49f30 tob1db957CompareJuly 28, 2023 17:48
@nvasilevski
Copy link
Author

Hey@kddnewton, sorry for a delay, I finally have found some time to make changes on the PR

For these reasons, I think we need a RemovedNode that you put in place of the existing node when you replace it.

That's what I end up doing, could you have another look? Thanks!

At the moment most of the places where you're removing nodes is going to result in not actually removing them because of this.

Correct me if I'm wrong but I don't think this is a concern anymore as long as we go with a "truthy"RemovedNode instances. I'm willing to create a separate PR to change the design ofcopy methods but I don't think it's a strict requirement for the#remove API to be shipped

# [Array[ Comment | EmbDoc ]] the comments attached to this node
attr_reader :comments

def initialize(location:)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I wasn't sure about exact API of theRemovedNode. I think we may wantRemovedNode to point to an instance of the node it substitutes but I at the moment I don't have use-cases for it so I decided not to overcomplicate. Perhaps I should have simplified it even further and dropped thecomments andlocation properties but some existing code expected every node to respond tocomments so I decided to avoid breaking this expectation

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@kddnewtonkddnewtonAwaiting requested review from kddnewton

Requested changes must be addressed to merge this pull request.

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@nvasilevski@kddnewton

[8]ページ先頭

©2009-2025 Movatter.jp