Skip to content

Don't rely on parent being present #300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1539,7 +1539,7 @@ def ===(other)
private

def format_contents(q)
q.parent.format_key(q, key)
(q.parent || HashKeyFormatter::Identity.new).format_key(q, key)
return unless value

if key.comments.empty? && AssignFormatting.skip_indent?(value)
Expand Down Expand Up @@ -1756,6 +1756,20 @@ def format_key(q, key)
end
end

# When formatting a single assoc node without the context of the parent
# hash, this formatter is used. It uses whatever is present in the node,
# because there is nothing to be consistent with.
class Identity
def format_key(q, key)
if key.is_a?(Label)
q.format(key)
else
q.format(key)
q.text(" =>")
end
end
end

def self.for(container)
labels =
container.assocs.all? do |assoc|
Expand Down Expand Up @@ -4328,7 +4342,7 @@ def format(q)
# are no parentheses around the arguments to that command, so we need to
# break the block.
case q.parent
when Command, CommandCall
when nil, Command, CommandCall
q.break_parent
format_break(q, break_opening, break_closing)
return
Expand Down Expand Up @@ -4382,7 +4396,7 @@ def unchangeable_bounds?(q)
# If we're a sibling of a control-flow keyword, then we're going to have to
# use the do..end bounds.
def forced_do_end_bounds?(q)
case q.parent.call
case q.parent&.call
when Break, Next, ReturnNode, Super
true
else
Expand Down