Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BeginKeywordNode for kwbegin nodes #333

Merged
merged 2 commits into from
Nov 11, 2024

Conversation

dvandersluis
Copy link
Member

Allows for kwbegin nodes to not need destructuring, as in rubocop/rubocop#13426.

@dvandersluis
Copy link
Member Author

@marcandre @bquorning this isn't quite as straightforward as I originally thought. 😅

When the body of a begin is multiple lines, they are expressed as multiple items within the node:

$ ruby-parse -e '
begin
  foo
  bar
end
'
(kwbegin
  (send nil :foo)
  (send nil :bar))

Rubocop seems to act as if the kwbegin body is just the first one, but should it return an array of nodes instead?

I also added test cases for begin...rescue...ensure...end but I don't have it fully working yet.

@dvandersluis dvandersluis changed the title Add BeginNode for kwbegin nodes Add BeginKeywordNode for kwbegin nodes Nov 7, 2024
@dvandersluis
Copy link
Member Author

I've also gone back and forth on if this should be BeginNode, BeginKeywordNode, or KwbeginNode. I've landed on BeginKeywordNode (because begin nodes also exist and could theoretically have a Node class one day) but let me know your thoughts.

@marcandre
Copy link
Contributor

marcandre commented Nov 7, 2024

Without much thought, I'd say body should be an array, and name should be KeywordBeginNode.

@dvandersluis
Copy link
Member Author

name should be KeywordBeginNode.

Ah yes of course, a fourth option I didn't consider 😂 I'll change it up this afternoon.

@dvandersluis
Copy link
Member Author

dvandersluis commented Nov 7, 2024

Maybe body should be the kwbegin node itself when there are multiple nodes? I'm not really sure why parser doesn't create a begin node around the body otherwise. It does when there is an ensure or rescue:

ruby-parse -e '
begin
  foo
  bar
ensure
  baz
end
'
(kwbegin
  (ensure
    (begin
      (send nil :foo)
      (send nil :bar))
    (send nil :baz)))
ruby-parse -e '
begin
  foo
  bar
rescue
  baz
end
'
(kwbegin
  (rescue
    (begin
      (send nil :foo)
      (send nil :bar))
    (resbody nil nil
      (send nil :baz)) nil))

I think it would probably make sense to treat the kwbegin as if it's a begin node then. What do you think?

@dvandersluis dvandersluis force-pushed the kwbegin-node branch 3 times, most recently from faaf5e9 to b613ce0 Compare November 7, 2024 20:11
Copy link
Contributor

@marcandre marcandre left a comment

Choose a reason for hiding this comment

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

Looks good to me 👍

Comment on lines +16 to +21
# Returns the `rescue` node of the `ensure`, if present.
#
# @return [Node, nil] The `rescue` node.
def rescue_node
node_parts[0] if node_parts[0].rescue_type?
end
Copy link
Member Author

Choose a reason for hiding this comment

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

@marcandre do you want me to do this as a separate PR, or create a separate changelog entry for it pointing to this PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

Either work, your choice 😄

@dvandersluis dvandersluis marked this pull request as ready for review November 11, 2024 18:42
@dvandersluis
Copy link
Member Author

Updated.

@marcandre marcandre merged commit 98765dc into rubocop:master Nov 11, 2024
20 checks passed
@marcandre
Copy link
Contributor

Great, thanks. Released as 1.35

@dvandersluis dvandersluis deleted the kwbegin-node branch November 11, 2024 19:02
@dvandersluis
Copy link
Member Author

Thanks @marcandre!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants