Skip to content

Commit 380b916

Browse files
committed
Fix and and or operators RT broken associativity
Extracted from #387 Without this change, the following code snippets ```ruby a or b and c ``` and ```ruby a[:x] = b[:x] || d(:new) ``` do not round trip.
1 parent 6c6a242 commit 380b916

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

lib/unparser/writer/binary.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,13 @@ def symbol_name
5252
end
5353

5454
def dispatch
55-
left_emitter.write_to_buffer
56-
write(' ', MAP.fetch(effective_symbol), ' ')
57-
visit(right)
55+
if node.type.eql?(:and) && left.type.equal?(:or)
56+
emit_with(KEYWORD_TOKENS)
57+
else
58+
left_emitter.write_to_buffer
59+
write(' ', MAP.fetch(effective_symbol), ' ')
60+
visit(right)
61+
end
5862
end
5963

6064
private

test/corpus/semantic/and.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,16 @@
66

77
if a...b and c...d
88
end
9+
10+
a and b and c
11+
(a and b) and c
12+
a and (b and c)
13+
a and b or c
14+
(a and b) or c
15+
a and (b or c)
16+
a or b and c
17+
(a or b) and c
18+
a or (b and c)
19+
a or b or c
20+
(a or b) or c
21+
a or (b or c)

test/corpus/semantic/or.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a[:x] = b[:x] || d(:new)

0 commit comments

Comments
 (0)