Skip to content

Commit 0a97000

Browse files
authored
Merge pull request #166 from ruby-syntax-tree/fix-163
Handle formatting blocks outside of tree
2 parents e139174 + bcdf200 commit 0a97000

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

lib/syntax_tree/node.rb

+19-7
Original file line numberDiff line numberDiff line change
@@ -1955,11 +1955,12 @@ def format(q)
19551955
# If the receiver of this block a Command or CommandCall node, then there
19561956
# are no parentheses around the arguments to that command, so we need to
19571957
# break the block.
1958-
receiver = q.parent.call
1959-
if receiver.is_a?(Command) || receiver.is_a?(CommandCall)
1958+
case q.parent
1959+
in { call: Command | CommandCall }
19601960
q.break_parent
19611961
format_break(q, break_opening, break_closing)
19621962
return
1963+
else
19631964
end
19641965

19651966
q.group do
@@ -1978,16 +1979,26 @@ def unchangeable_bounds?(q)
19781979
# If we hit a statements, then we're safe to use whatever since we
19791980
# know for certain we're going to get split over multiple lines
19801981
# anyway.
1981-
break false if parent.is_a?(Statements) || parent.is_a?(ArgParen)
1982-
1983-
[Command, CommandCall].include?(parent.class)
1982+
case parent
1983+
in Statements | ArgParen
1984+
break false
1985+
in Command | CommandCall
1986+
true
1987+
else
1988+
false
1989+
end
19841990
end
19851991
end
19861992

19871993
# If we're a sibling of a control-flow keyword, then we're going to have to
19881994
# use the do..end bounds.
19891995
def forced_do_end_bounds?(q)
1990-
[Break, Next, Return, Super].include?(q.parent.call.class)
1996+
case q.parent
1997+
in { call: Break | Next | Return | Super }
1998+
true
1999+
else
2000+
false
2001+
end
19912002
end
19922003

19932004
# If we're the predicate of a loop or conditional, then we're going to have
@@ -2314,7 +2325,8 @@ def comments
23142325
end
23152326

23162327
def format(q)
2317-
if operator == :"::" || (operator.is_a?(Op) && operator.value == "::")
2328+
case operator
2329+
in :"::" | Op[value: "::"]
23182330
q.text(".")
23192331
else
23202332
operator.format(q)

0 commit comments

Comments
 (0)