Skip to content

Commit

Permalink
Fix hashes that require lvar omissions
Browse files Browse the repository at this point in the history
[fix #367]
  • Loading branch information
mbj committed Jun 10, 2024
1 parent 56d8028 commit 461e8aa
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
21 changes: 16 additions & 5 deletions lib/unparser/emitter/pair.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,30 @@ class Pair < self
private

def dispatch
if colon?(key)
write(key.children.first.to_s, ': ')
if colon?
emit_colon
unless implicit_value?
write(' ')
visit(value)
end
else
visit(key)
write(' => ')
visit(value)
end

visit(value)
end

def colon?(key)
def colon?
n_sym?(key) && BAREWORD.match?(key.children.first)
end

def emit_colon
write(key.children.first.to_s, ':')
end

def implicit_value?
n_lvar?(value) && value.children.first.equal?(key.children.first)
end
end
end
end
1 change: 1 addition & 0 deletions lib/unparser/node_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def n?(type, node)
kwargs
kwsplat
lambda
lvar
match_rest
pair
rescue
Expand Down
4 changes: 4 additions & 0 deletions test/corpus/literal/def.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,7 @@ def f
def f
%()
end

def foo(return:)
{ return: }
end
2 changes: 2 additions & 0 deletions test/corpus/literal/send.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,5 @@ def foo
foo&.!
foo.~(b)
a&.+(b)
a = nil
foo(bar: a)

0 comments on commit 461e8aa

Please sign in to comment.