Skip to content

Commit

Permalink
- Fix an error for flip-flop with beginless or endless ranges
Browse files Browse the repository at this point in the history
This PR resolves rubocop/rubocop#12198.

```console
$ ruby-parse -e 'if foo..; end'
Failed on: (fragment:0)
/Users/koic/.rbenv/versions/3.3.0-dev/lib/ruby/gems/3.3.0+0/gems/parser-3.2.2.3/lib/parser/builders/default.rb:1676:
in `check_condition': undefined method `type' for nil (NoMethodError)

      case cond.type
               ^^^^^
        from /Users/koic/.rbenv/versions/3.3.0-dev/lib/ruby/gems/3.3.0+0/gems/parser-3.2.2.3/lib/parser/builders/default.rb:1707:
        in `check_condition'
        from /Users/koic/.rbenv/versions/3.3.0-dev/lib/ruby/gems/3.3.0+0/gems/parser-3.2.2.3/lib/parser/builders/default.rb:1275:
        in `condition'
```

I'm not sure if there's any significance to using the flip-flop syntax with `nil`,
but by using beginless or endless ranges and explicitly specifying `nil`,
the following current behaviors are made compatible:

## `iflipflop` with explicitly `nil`

```console
$ ruby-parse -e 'if foo..nil ; end'
(if
  (iflipflop
    (send nil :foo)
    (nil)) nil nil)
```

```console
$ ruby-parse -e 'if nil..bar ; end'
(if
  (iflipflop
    (nil)
    (send nil :bar)) nil nil)
```

## `eflipflop` with explicitly `nil`

```console
$ ruby-parse -e 'if foo...nil ; end'
(if
  (eflipflop
    (send nil :foo)
    (nil)) nil nil)
```

```console
$ ruby-parse -e 'if nil...bar ; end'
(if
  (eflipflop
    (nil)
    (send nil :bar)) nil nil)
```

The difference in the flip-flop with beginless or endless ranges is
that `s(:nil)` is replaced by `nil` in the flip-flop ASTs.

This is reflected in the tests.
  • Loading branch information
koic committed Oct 4, 2023
1 parent a9c45c5 commit 78adf19
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 9 deletions.
28 changes: 19 additions & 9 deletions lib/parser/builders/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1690,24 +1690,34 @@ def check_condition(cond)
cond
end

when :and, :or, :irange, :erange
when :and, :or
lhs, rhs = *cond

type = case cond.type
when :irange then :iflipflop
when :erange then :eflipflop
end

if [:and, :or].include?(cond.type) &&
@parser.version == 18
if @parser.version == 18
cond
else
cond.updated(type, [
cond.updated(cond.type, [
check_condition(lhs),
check_condition(rhs)
])
end

when :irange, :erange
lhs, rhs = *cond

type = case cond.type
when :irange then :iflipflop
when :erange then :eflipflop
end

lhs_condition = check_condition(lhs) unless lhs.nil?
rhs_condition = check_condition(rhs) unless rhs.nil?

return cond.updated(type, [
lhs_condition,
rhs_condition
])

when :regexp
n(:match_current_line, [ cond ], expr_map(cond.loc.expression))

Expand Down
72 changes: 72 additions & 0 deletions test/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4739,6 +4739,22 @@ def test_cond_iflipflop
%q{ ~~~~~~~~ expression (iflipflop)
| ~~ operator (iflipflop)})

assert_parses(
s(:if, s(:iflipflop, s(:lvar, :foo), s(:nil)),
nil, nil),
%q{if foo..nil; end},
%q{ ~~~~~~~~ expression (iflipflop)
| ~~ operator (iflipflop)},
%w(1.8))

assert_parses(
s(:if, s(:iflipflop, s(:nil), s(:lvar, :bar)),
nil, nil),
%q{if nil..bar; end},
%q{ ~~~~~~~~ expression (iflipflop)
| ~~ operator (iflipflop)},
%w(1.8))

assert_parses(
s(:not, s(:begin, s(:iflipflop, s(:lvar, :foo), s(:lvar, :bar)))),
%q{!(foo..bar)},
Expand All @@ -4754,6 +4770,26 @@ def test_cond_iflipflop
SINCE_1_9)
end

def test_cond_iflipflop_with_endless_range
assert_parses(
s(:if, s(:iflipflop, s(:lvar, :foo), nil),
nil, nil),
%q{if foo..; end},
%q{ ~~~~~ expression (iflipflop)
| ~~ operator (iflipflop)},
SINCE_2_6)
end

def test_cond_iflipflop_with_beginless_range
assert_parses(
s(:if, s(:iflipflop, nil, s(:lvar, :bar)),
nil, nil),
%q{if ..bar; end},
%q{ ~~~~~ expression (iflipflop)
| ~~ operator (iflipflop)},
SINCE_2_7)
end

def test_cond_eflipflop
assert_parses(
s(:if, s(:eflipflop, s(:lvar, :foo), s(:lvar, :bar)),
Expand All @@ -4762,6 +4798,22 @@ def test_cond_eflipflop
%q{ ~~~~~~~~~ expression (eflipflop)
| ~~~ operator (eflipflop)})

assert_parses(
s(:if, s(:eflipflop, s(:lvar, :foo), s(:nil)),
nil, nil),
%q{if foo...nil; end},
%q{ ~~~~~~~~~ expression (eflipflop)
| ~~~ operator (eflipflop)},
%w(1.8))

assert_parses(
s(:if, s(:eflipflop, s(:nil), s(:lvar, :bar)),
nil, nil),
%q{if nil...bar; end},
%q{ ~~~~~~~~~ expression (eflipflop)
| ~~~ operator (eflipflop)},
%w(1.8))

assert_parses(
s(:not, s(:begin, s(:eflipflop, s(:lvar, :foo), s(:lvar, :bar)))),
%q{!(foo...bar)},
Expand All @@ -4777,6 +4829,26 @@ def test_cond_eflipflop
SINCE_1_9)
end

def test_cond_eflipflop_with_endless_range
assert_parses(
s(:if, s(:eflipflop, s(:lvar, :foo), nil),
nil, nil),
%q{if foo...; end},
%q{ ~~~~~~ expression (eflipflop)
| ~~~ operator (eflipflop)},
SINCE_2_6)
end

def test_cond_eflipflop_with_beginless_range
assert_parses(
s(:if, s(:eflipflop, nil, s(:lvar, :bar)),
nil, nil),
%q{if ...bar; end},
%q{ ~~~~~~ expression (eflipflop)
| ~~~ operator (eflipflop)},
SINCE_2_7)
end

def test_cond_match_current_line
assert_parses(
s(:if,
Expand Down

0 comments on commit 78adf19

Please sign in to comment.