Skip to content

Always use do/end for multiline lambdas #380

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 7 additions & 26 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7210,36 +7210,17 @@ def format(q)
q.text(" ")
q
.if_break do
force_parens =
q.parents.any? do |node|
node.is_a?(Command) || node.is_a?(CommandCall)
end

if force_parens
q.text("{")
q.text("do")

unless statements.empty?
q.indent do
q.breakable_space
q.format(statements)
end
unless statements.empty?
q.indent do
q.breakable_space
q.format(statements)
end

q.text("}")
else
q.text("do")

unless statements.empty?
q.indent do
q.breakable_space
q.format(statements)
end
end

q.breakable_space
q.text("end")
end

q.breakable_space
q.text("end")
end
.if_flat do
q.text("{")
Expand Down
28 changes: 28 additions & 0 deletions test/fixtures/lambda.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,31 @@
-> do # comment1
# comment2
end
% # multiline lambda in a command
command "arg" do
-> {
multi
line
}
end
-
command "arg" do
-> do
multi
line
end
end
% # multiline lambda in a command call
command.call "arg" do
-> {
multi
line
}
end
-
command.call "arg" do
-> do
multi
line
end
end