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

Commit 7312e80

Browse files
committed
Force using braces if the block is within the predicate of a conditional or loop.
1 parent adda1a3 commit 7312e80

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
3333
- Ensure `For` loops keep their trailing commas.
3434
- Replicate content for `__END__` keyword exactly.
3535
- Keep block `If`, `Unless`, `While`, and `Until` forms if there is an assignment in the predicate.
36+
- Force using braces if the block is within the predicate of a conditional or loop.
3637

3738
### Removed
3839

lib/syntax_tree.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,6 +2723,8 @@ def format(q)
27232723
[block_open.value, block_close, block_open.value, block_close]
27242724
elsif forced_do_end_bounds?(q)
27252725
["do", "end", "do", "end"]
2726+
elsif forced_brace_bounds?(q)
2727+
["{", "}", "{", "}"]
27262728
else
27272729
["do", "end", "{", "}"]
27282730
end
@@ -2764,6 +2766,13 @@ def forced_do_end_bounds?(q)
27642766
[Break, Next, Return, Super].include?(q.parent.call.class)
27652767
end
27662768

2769+
# If we're the predicate of a loop or conditional, then we're going to have
2770+
# to go with the {..} bounds.
2771+
def forced_brace_bounds?(q)
2772+
parent = q.parents.to_a[1]
2773+
[If, IfMod, Unless, UnlessMod, While, WhileMod, Until, UntilMod].include?(parent.class) && node == parent.predicate.block
2774+
end
2775+
27672776
def format_break(q, opening, closing)
27682777
q.text(" ")
27692778
q.format(BlockOpenFormatter.new(opening, block_open))

0 commit comments

Comments
 (0)