Skip to content

Commit 787d866

Browse files
authored
Merge pull request #49 from ruby-syntax-tree/empty-hsh-ptn
Empty HshPtn
2 parents dde8d1f + 555e77b commit 787d866

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

lib/syntax_tree/node.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -4536,6 +4536,7 @@ def deconstruct_keys(keys)
45364536
def format(q)
45374537
parts = keywords.map { |(key, value)| KeywordFormatter.new(key, value) }
45384538
parts << KeywordRestFormatter.new(keyword_rest) if keyword_rest
4539+
45394540
contents = -> do
45404541
q.seplist(parts) { |part| q.format(part, stackable: false) }
45414542
end
@@ -4546,8 +4547,9 @@ def format(q)
45464547
return
45474548
end
45484549

4549-
parent = q.parent
4550-
if PATTERNS.include?(parent.class)
4550+
if parts.empty?
4551+
q.text("{}")
4552+
elsif PATTERNS.include?(q.parent.class)
45514553
q.text("{ ")
45524554
contents.call
45534555
q.text(" }")

lib/syntax_tree/parser.rb

+9-3
Original file line numberDiff line numberDiff line change
@@ -1501,13 +1501,19 @@ def on_heredoc_end(value)
15011501
# (nil | VarField) keyword_rest
15021502
# ) -> HshPtn
15031503
def on_hshptn(constant, keywords, keyword_rest)
1504-
parts = [constant, keywords, keyword_rest].flatten(2).compact
1504+
parts = [constant, *keywords&.flatten(1), keyword_rest].compact
1505+
location =
1506+
if parts.empty?
1507+
find_token(LBrace).location.to(find_token(RBrace).location)
1508+
else
1509+
parts[0].location.to(parts[-1].location)
1510+
end
15051511

15061512
HshPtn.new(
15071513
constant: constant,
1508-
keywords: keywords,
1514+
keywords: keywords || [],
15091515
keyword_rest: keyword_rest,
1510-
location: parts[0].location.to(parts[-1].location)
1516+
location: location
15111517
)
15121518
end
15131519

test/fixtures/hshptn.rb

+4
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,7 @@
4444
case foo
4545
in Foo[**bar]
4646
end
47+
%
48+
case foo
49+
in {}
50+
end

0 commit comments

Comments
 (0)