From 555e77b47048edceb63d5b0ecacda26f6846e118 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Sat, 16 Apr 2022 13:07:34 -0400 Subject: [PATCH] Empty HshPtn --- lib/syntax_tree/node.rb | 6 ++++-- lib/syntax_tree/parser.rb | 12 +++++++++--- test/fixtures/hshptn.rb | 4 ++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/syntax_tree/node.rb b/lib/syntax_tree/node.rb index 9e96bd96..afe50e4e 100644 --- a/lib/syntax_tree/node.rb +++ b/lib/syntax_tree/node.rb @@ -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 @@ -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(" }") diff --git a/lib/syntax_tree/parser.rb b/lib/syntax_tree/parser.rb index 92a58ccb..c99eea4b 100644 --- a/lib/syntax_tree/parser.rb +++ b/lib/syntax_tree/parser.rb @@ -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 diff --git a/test/fixtures/hshptn.rb b/test/fixtures/hshptn.rb index 2efe2fd3..857336d4 100644 --- a/test/fixtures/hshptn.rb +++ b/test/fixtures/hshptn.rb @@ -44,3 +44,7 @@ case foo in Foo[**bar] end +% +case foo +in {} +end