Skip to content

Commit 83b8a57

Browse files
committed
Fix negated flipflop unparsing
Extracted from #387 ```bash bundle exec bin/unparser -e 'not a...b' (string) Original-Source: not a...b Generated-Source: !a...b Original-Node: (send (eflipflop (send nil :a) (send nil :b)) :!) Generated-Node: (erange (send (send nil :a) :!) (send nil :b)) Node-Diff: @@ -1,4 +1,4 @@ -(send - (eflipflop - (send nil :a) - (send nil :b)) :!) +(erange + (send + (send nil :a) :!) + (send nil :b)) ```
1 parent cbbc8ad commit 83b8a57

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

lib/unparser/node_helpers.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ def n?(type, node)
3131
node.type.equal?(type)
3232
end
3333

34+
def n_flipflop?(node)
35+
n_iflipflop?(node) || n_eflipflop?(node)
36+
end
37+
3438
%i[
3539
arg
3640
args
@@ -41,12 +45,14 @@ def n?(type, node)
4145
cbase
4246
const
4347
dstr
48+
eflipflop
4449
empty_else
4550
ensure
4651
gvar
4752
hash
4853
hash_pattern
4954
if
55+
iflipflop
5056
in_pattern
5157
int
5258
kwarg

lib/unparser/writer/send/unary.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ class Unary < self
1414

1515
def dispatch
1616
name = selector
17+
first_child = children.fetch(0)
1718

18-
write(MAP.fetch(name, name).to_s)
19+
if n_flipflop?(first_child)
20+
write 'not '
21+
else
22+
write(MAP.fetch(name, name).to_s)
1923

20-
if n_int?(receiver) && selector.equal?(:+@)
21-
write('+')
24+
if n_int?(receiver) && selector.equal?(:+@)
25+
write('+')
26+
end
2227
end
2328

2429
visit(receiver)

test/corpus/literal/flipflop.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@
88
end
99
if foo..;
1010
end
11+
not foo..bar
12+
not foo...bar
13+
!(foo..bar)
14+
!(foo...bar)

0 commit comments

Comments
 (0)