Skip to content

Commit 44adc03

Browse files
authored
Merge pull request #300 from ruby-syntax-tree/dont-rely-on-parent
Don't rely on parent being present
2 parents c4c78b3 + 946bc61 commit 44adc03

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

lib/syntax_tree/node.rb

+17-3
Original file line numberDiff line numberDiff line change
@@ -1539,7 +1539,7 @@ def ===(other)
15391539
private
15401540

15411541
def format_contents(q)
1542-
q.parent.format_key(q, key)
1542+
(q.parent || HashKeyFormatter::Identity.new).format_key(q, key)
15431543
return unless value
15441544

15451545
if key.comments.empty? && AssignFormatting.skip_indent?(value)
@@ -1756,6 +1756,20 @@ def format_key(q, key)
17561756
end
17571757
end
17581758

1759+
# When formatting a single assoc node without the context of the parent
1760+
# hash, this formatter is used. It uses whatever is present in the node,
1761+
# because there is nothing to be consistent with.
1762+
class Identity
1763+
def format_key(q, key)
1764+
if key.is_a?(Label)
1765+
q.format(key)
1766+
else
1767+
q.format(key)
1768+
q.text(" =>")
1769+
end
1770+
end
1771+
end
1772+
17591773
def self.for(container)
17601774
labels =
17611775
container.assocs.all? do |assoc|
@@ -4328,7 +4342,7 @@ def format(q)
43284342
# are no parentheses around the arguments to that command, so we need to
43294343
# break the block.
43304344
case q.parent
4331-
when Command, CommandCall
4345+
when nil, Command, CommandCall
43324346
q.break_parent
43334347
format_break(q, break_opening, break_closing)
43344348
return
@@ -4382,7 +4396,7 @@ def unchangeable_bounds?(q)
43824396
# If we're a sibling of a control-flow keyword, then we're going to have to
43834397
# use the do..end bounds.
43844398
def forced_do_end_bounds?(q)
4385-
case q.parent.call
4399+
case q.parent&.call
43864400
when Break, Next, ReturnNode, Super
43874401
true
43884402
else

0 commit comments

Comments
 (0)