Skip to content

Commit c5ac4b3

Browse files
committed
Fix nested hash patterns from accidentally adding a then to their output.
1 parent 230a179 commit c5ac4b3

File tree

6 files changed

+26
-7
lines changed

6 files changed

+26
-7
lines changed

CHANGELOG.md

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

77
## [Unreleased]
88

9+
## [2.4.1] - 2022-05-10
10+
11+
- Fix nested hash patterns from accidentally adding a `then` to their output.
12+
913
## [2.4.0] - 2022-05-07
1014

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

210214
- 🎉 Initial release! 🎉
211215

212-
[unreleased]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.4.0...HEAD
216+
[unreleased]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.4.1...HEAD
217+
[2.4.1]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.4.0...v2.4.1
213218
[2.4.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.3.1...v2.4.0
214219
[2.3.1]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.3.0...v2.3.1
215220
[2.3.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v2.2.0...v2.3.0

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
syntax_tree (2.4.0)
4+
syntax_tree (2.4.1)
55

66
GEM
77
remote: https://rubygems.org/

lib/syntax_tree/node.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -5064,13 +5064,16 @@ def format(q)
50645064
parts = keywords.map { |(key, value)| KeywordFormatter.new(key, value) }
50655065
parts << KeywordRestFormatter.new(keyword_rest) if keyword_rest
50665066

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

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

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

50985101
# If there's only one pair, then we'll just print the contents provided
50995102
# we're not inside another pattern.
5100-
if !PATTERNS.include?(q.parent.class) && parts.size == 1
5103+
if !nested && parts.size == 1
51015104
contents.call
51025105
return
51035106
end

lib/syntax_tree/parser.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -1671,9 +1671,15 @@ def on_heredoc_end(value)
16711671
# (nil | VarField) keyword_rest
16721672
# ) -> HshPtn
16731673
def on_hshptn(constant, keywords, keyword_rest)
1674-
# Create an artificial VarField if we find an extra ** on the end
1675-
if !keyword_rest && (token = find_token(Op, "**", consume: false))
1674+
if keyword_rest
1675+
# We're doing this to delete the token from the list so that it doesn't
1676+
# confuse future patterns by thinking they have an extra ** on the end.
1677+
find_token(Op, "**")
1678+
elsif (token = find_token(Op, "**", consume: false))
16761679
tokens.delete(token)
1680+
1681+
# Create an artificial VarField if we find an extra ** on the end. This
1682+
# means the formatting will be a little more consistent.
16771683
keyword_rest = VarField.new(value: nil, location: token.location)
16781684
end
16791685

lib/syntax_tree/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module SyntaxTree
4-
VERSION = "2.4.0"
4+
VERSION = "2.4.1"
55
end

test/fixtures/hshptn.rb

+5
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,8 @@
6666
case foo
6767
in **nil
6868
end
69+
%
70+
case foo
71+
in bar, { baz:, **nil }
72+
in qux:
73+
end

0 commit comments

Comments
 (0)