From 239dda47df57b20f32eaeb018cf5b9097ea75c16 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Thu, 26 Jan 2023 11:30:20 -0500 Subject: [PATCH] Use do..end when you can because of binary node --- lib/syntax_tree/node.rb | 16 +++++++++++++++- test/fixtures/binary.rb | 14 ++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/syntax_tree/node.rb b/lib/syntax_tree/node.rb index fc5517cf..b14d1ed1 100644 --- a/lib/syntax_tree/node.rb +++ b/lib/syntax_tree/node.rb @@ -4388,8 +4388,22 @@ def forced_brace_bounds?(q) when Paren, Statements # If we hit certain breakpoints then we know we're safe. return false - when IfNode, IfOp, UnlessNode, WhileNode, UntilNode + when IfNode, IfOp, UnlessNode + # If this node is the predicate of an if/unless node, then we're + # going to have to use the {..} bounds. return true if parent.predicate == previous + when WhileNode, UntilNode + if previous.is_a?(Binary) + # If this node is the predicate of a while/until node, then we're + # going to have to use the {..} bounds if the loop is in the + # modifier form because otherwise the do/end could be confused for + # the optional do keyword of the loop. + return parent.predicate == previous && parent.modifier? + else + # If this node is the predicate of a while/until node, then we're + # going to have to use the {..} bounds. + return true if parent.predicate == previous + end end previous = parent diff --git a/test/fixtures/binary.rb b/test/fixtures/binary.rb index f8833cdc..8d1ff483 100644 --- a/test/fixtures/binary.rb +++ b/test/fixtures/binary.rb @@ -9,3 +9,17 @@ - foo * barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr +% +foooooooooooooooooooooo || barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } +- +foooooooooooooooooooooo || + barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? do |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz + end +% +foooooooooooooooooooooo && barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } +- +foooooooooooooooooooooo && + barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? do |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz + end