Skip to content

Commit fadddf6

Browse files
authored
Merge pull request #319 from ruby-syntax-tree/keywords-bare-hashes
Handle keywords with bare hashes
2 parents d2278ce + b0ba92e commit fadddf6

File tree

5 files changed

+16
-1
lines changed

5 files changed

+16
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
1414

1515
- The `nesting` field on the results of the indexing operation is no longer a single flat array. Instead it is an array of arrays, where each array is a single nesting level. This more accurately reflects the nesting of the nodes in the tree. For example, `class Foo::Bar::Baz; end` would result in `[Foo, Bar, Baz]`, but that incorrectly implies that you can see constants at each of those levels. Now this would result in `[[Foo, Bar, Baz]]` to indicate that it can see either the top level or constants within the scope of `Foo::Bar::Baz` only.
1616
- When formatting hashes that have omitted values and mixed hash rockets with labels, the formatting now maintains whichever delimiter was used in the source. This is because forcing the use of hash rockets with omitted values results in a syntax error.
17+
- Handle the case where a bare hash is used after the `break`, `next`, or `return` keywords. Previously this would result in hash labels which is not valid syntax. Now it maintains the delimiters used in the source.
1718

1819
## [6.0.0] - 2023-02-10
1920

lib/syntax_tree/node.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -1874,7 +1874,15 @@ def ===(other)
18741874
end
18751875

18761876
def format_key(q, key)
1877-
(@key_formatter ||= HashKeyFormatter.for(self)).format_key(q, key)
1877+
@key_formatter ||=
1878+
case q.parents.take(3).last
1879+
when Break, Next, ReturnNode
1880+
HashKeyFormatter::Identity.new
1881+
else
1882+
HashKeyFormatter.for(self)
1883+
end
1884+
1885+
@key_formatter.format_key(q, key)
18781886
end
18791887
end
18801888

test/fixtures/break.rb

+2
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@
3333
qux
3434
end
3535
)
36+
%
37+
break :foo => "bar"

test/fixtures/next.rb

+2
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,5 @@
7272
fun foo do
7373
end
7474
)
75+
%
76+
next :foo => "bar"

test/fixtures/return.rb

+2
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,5 @@
3737
return []
3838
%
3939
return [1]
40+
%
41+
return :foo => "bar"

0 commit comments

Comments
 (0)