Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c05de43

Browse files
committed
Force using braces if the block is within a Binary within the predicate of a loop or conditional.
1 parent f794ace commit c05de43

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
5656
- Force using braces if the block is within the predicate of a ternary.
5757
- Properly handle trailing comments after a `then` operator on a `When` or `In` clause.
5858
- Ensure nested `HshPtn` nodes use braces.
59+
- Force using braces if the block is within a `Binary` within the predicate of a loop or conditional.
5960

6061
### Removed
6162

lib/syntax_tree.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,11 +2288,14 @@ def format(q)
22882288
q.group do
22892289
q.group { q.format(left) }
22902290
q.text(" ") unless power
2291-
q.text(operator)
22922291

2293-
q.indent do
2294-
q.breakable(power ? "" : " ")
2295-
q.format(right)
2292+
q.group do
2293+
q.text(operator)
2294+
2295+
q.indent do
2296+
q.breakable(power ? "" : " ")
2297+
q.format(right)
2298+
end
22962299
end
22972300
end
22982301
end
@@ -2788,8 +2791,14 @@ def forced_do_end_bounds?(q)
27882791
# If we're the predicate of a loop or conditional, then we're going to have
27892792
# to go with the {..} bounds.
27902793
def forced_brace_bounds?(q)
2791-
parent, grandparent, = q.parents.to_a
2792-
[If, IfMod, IfOp, Unless, UnlessMod, While, WhileMod, Until, UntilMod].include?(grandparent.class) && parent == grandparent.predicate
2794+
parents = q.parents.to_a
2795+
parents.each_with_index.any? do |parent, index|
2796+
# If we hit certain breakpoints then we know we're safe.
2797+
break false if [Paren, Statements].include?(parent.class)
2798+
2799+
[If, IfMod, IfOp, Unless, UnlessMod, While, WhileMod, Until, UntilMod].include?(parent.class) &&
2800+
parent.predicate == parents[index - 1]
2801+
end
27932802
end
27942803

27952804
def format_break(q, opening, closing)

0 commit comments

Comments
 (0)