Skip to content

Handle keywords with bare hashes #319

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
Feb 26, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

- 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.
- 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.
- 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.

## [6.0.0] - 2023-02-10

Expand Down
10 changes: 9 additions & 1 deletion lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1874,7 +1874,15 @@ def ===(other)
end

def format_key(q, key)
(@key_formatter ||= HashKeyFormatter.for(self)).format_key(q, key)
@key_formatter ||=
case q.parents.take(3).last
when Break, Next, ReturnNode
HashKeyFormatter::Identity.new
else
HashKeyFormatter.for(self)
end

@key_formatter.format_key(q, key)
end
end

Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/break.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@
qux
end
)
%
break :foo => "bar"
2 changes: 2 additions & 0 deletions test/fixtures/next.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,5 @@
fun foo do
end
)
%
next :foo => "bar"
2 changes: 2 additions & 0 deletions test/fixtures/return.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@
return []
%
return [1]
%
return :foo => "bar"