Skip to content

Empty HshPtn #49

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
Apr 16, 2022
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4536,6 +4536,7 @@ def deconstruct_keys(keys)
def format(q)
parts = keywords.map { |(key, value)| KeywordFormatter.new(key, value) }
parts << KeywordRestFormatter.new(keyword_rest) if keyword_rest

contents = -> do
q.seplist(parts) { |part| q.format(part, stackable: false) }
end
Expand All @@ -4546,8 +4547,9 @@ def format(q)
return
end

parent = q.parent
if PATTERNS.include?(parent.class)
if parts.empty?
q.text("{}")
elsif PATTERNS.include?(q.parent.class)
q.text("{ ")
contents.call
q.text(" }")
Expand Down
12 changes: 9 additions & 3 deletions lib/syntax_tree/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1501,13 +1501,19 @@ def on_heredoc_end(value)
# (nil | VarField) keyword_rest
# ) -> HshPtn
def on_hshptn(constant, keywords, keyword_rest)
parts = [constant, keywords, keyword_rest].flatten(2).compact
parts = [constant, *keywords&.flatten(1), keyword_rest].compact
location =
if parts.empty?
find_token(LBrace).location.to(find_token(RBrace).location)
else
parts[0].location.to(parts[-1].location)
end

HshPtn.new(
constant: constant,
keywords: keywords,
keywords: keywords || [],
keyword_rest: keyword_rest,
location: parts[0].location.to(parts[-1].location)
location: location
)
end

Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/hshptn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@
case foo
in Foo[**bar]
end
%
case foo
in {}
end