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

Commit 5bf43b2

Browse files
committed
Special handling for RSpec matchers when nesting CommandCall nodes.
1 parent ffcaf5a commit 5bf43b2

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
99
### Added
1010

1111
- [#7](https://github.com/kddnewton/syntax_tree/issues/7) Better formatting for hashes and arrays that are values in hashes.
12+
- [#9](https://github.com/kddnewton/syntax_tree/issues/9) Special handling for RSpec matchers when nesting `CommandCall` nodes.
1213

1314
## [1.1.0] - 2021-12-08
1415

lib/syntax_tree.rb

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3771,14 +3771,8 @@ def format(q)
37713771
end
37723772

37733773
if arguments
3774-
width = doc_width(doc) + 1
37753774
q.text(" ")
3776-
3777-
if width > (q.maxwidth / 2)
3778-
q.format(arguments)
3779-
else
3780-
q.nest(width) { q.format(arguments) }
3781-
end
3775+
q.nest(argument_alignment(q, doc)) { q.format(arguments) }
37823776
end
37833777
end
37843778
end
@@ -3843,6 +3837,28 @@ def doc_width(parent)
38433837

38443838
width
38453839
end
3840+
3841+
def argument_alignment(q, doc)
3842+
# Very special handling case for rspec matchers. In general with rspec
3843+
# matchers you expect to see something like:
3844+
#
3845+
# expect(foo).to receive(:bar).with(
3846+
# 'one',
3847+
# 'two',
3848+
# 'three',
3849+
# 'four',
3850+
# 'five'
3851+
# )
3852+
#
3853+
# In this case the arguments are aligned to the left side as opposed to
3854+
# being aligned with the `receive` call.
3855+
if ["to", "not_to", "to_not"].include?(message.value)
3856+
0
3857+
else
3858+
width = doc_width(doc) + 1
3859+
width > (q.maxwidth / 2) ? 0 : width
3860+
end
3861+
end
38463862
end
38473863

38483864
# :call-seq:

test/fixtures/command_call.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,21 @@
55
-
66
foo.bar barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr,
77
bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
8+
%
9+
expect(foo).to receive(fooooooooooooooooooooooooooooooooooooooooooooooooooooooooo)
10+
-
11+
expect(foo).to receive(
12+
fooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
13+
)
14+
%
15+
expect(foo).not_to receive(fooooooooooooooooooooooooooooooooooooooooooooooooooooooooo)
16+
-
17+
expect(foo).not_to receive(
18+
fooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
19+
)
20+
%
21+
expect(foo).to_not receive(fooooooooooooooooooooooooooooooooooooooooooooooooooooooooo)
22+
-
23+
expect(foo).to_not receive(
24+
fooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
25+
)

0 commit comments

Comments
 (0)