Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Fix nested hash patterns from accidentally adding a then to their output #73

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
May 10, 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
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

## [Unreleased]

## [2.4.1] - 2022-05-10

- Fix nested hash patterns from accidentally adding a `then` to their output.

## [2.4.0] - 2022-05-07

### Added
Expand Down Expand Up @@ -209,7 +213,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

- 🎉 Initial release! 🎉

[unreleased]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.4.0...HEAD
[unreleased]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.4.1...HEAD
[2.4.1]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.4.0...v2.4.1
[2.4.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.3.1...v2.4.0
[2.3.1]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.3.0...v2.3.1
[2.3.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.2.0...v2.3.0
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
syntax_tree (2.4.0)
syntax_tree (2.4.1)

GEM
remote: https://rubygems.org/
Expand Down
7 changes: 5 additions & 2 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5064,13 +5064,16 @@ def format(q)
parts = keywords.map { |(key, value)| KeywordFormatter.new(key, value) }
parts << KeywordRestFormatter.new(keyword_rest) if keyword_rest

nested = PATTERNS.include?(q.parent.class)
contents = -> do
q.group { q.seplist(parts) { |part| q.format(part, stackable: false) } }

# If there isn't a constant, and there's a blank keyword_rest, then we
# have an plain ** that needs to have a `then` after it in order to
# parse correctly on the next parse.
q.text(" then") if !constant && keyword_rest && keyword_rest.value.nil?
if !constant && keyword_rest && keyword_rest.value.nil? && !nested
q.text(" then")
end
end

# If there is a constant, we're going to format to have the constant name
Expand All @@ -5097,7 +5100,7 @@ def format(q)

# If there's only one pair, then we'll just print the contents provided
# we're not inside another pattern.
if !PATTERNS.include?(q.parent.class) && parts.size == 1
if !nested && parts.size == 1
contents.call
return
end
Expand Down
10 changes: 8 additions & 2 deletions lib/syntax_tree/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1671,9 +1671,15 @@ def on_heredoc_end(value)
# (nil | VarField) keyword_rest
# ) -> HshPtn
def on_hshptn(constant, keywords, keyword_rest)
# Create an artificial VarField if we find an extra ** on the end
if !keyword_rest && (token = find_token(Op, "**", consume: false))
if keyword_rest
# We're doing this to delete the token from the list so that it doesn't
# confuse future patterns by thinking they have an extra ** on the end.
find_token(Op, "**")
elsif (token = find_token(Op, "**", consume: false))
tokens.delete(token)

# Create an artificial VarField if we find an extra ** on the end. This
# means the formatting will be a little more consistent.
keyword_rest = VarField.new(value: nil, location: token.location)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/syntax_tree/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module SyntaxTree
VERSION = "2.4.0"
VERSION = "2.4.1"
end
5 changes: 5 additions & 0 deletions test/fixtures/hshptn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,8 @@
case foo
in **nil
end
%
case foo
in bar, { baz:, **nil }
in qux:
end