Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an error for flip-flop with beginless or endless ranges #946

Conversation

koic
Copy link
Collaborator

@koic koic commented Oct 4, 2023

This PR resolves rubocop/rubocop#12198.

$ 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

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

eflipflop with explicitly nil

$ ruby-parse -e 'if foo...nil ; end'
(if
  (eflipflop
    (send nil :foo)
    (nil)) nil nil)
$ 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.

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.
@iliabylich iliabylich merged commit 555ecec into whitequark:master Oct 4, 2023
9 checks passed
@iliabylich
Copy link
Collaborator

Thanks. Do you need a release?

@koic koic deleted the fix_flip_flop_with_beginless_or_endless_ranges branch October 4, 2023 10:07
@koic
Copy link
Collaborator Author

koic commented Oct 4, 2023

Thank you! If you could release it, one issue of RuboCop would be resolved :-)

@iliabylich
Copy link
Collaborator

Sure, 3.2.2.4 has been released.

koic added a commit to koic/rubocop that referenced this pull request Oct 4, 2023
…ess ranges

Fixes rubocop#12198.

Parser 3.2.2.4 includes whitequark/parser#946.

This PR requires Parser 3.2.2.4+ to fix an error for flip-flop with beginless or endless ranges.
bbatsov pushed a commit to rubocop/rubocop that referenced this pull request Oct 4, 2023
Fixes #12198.

Parser 3.2.2.4 includes whitequark/parser#946.

This PR requires Parser 3.2.2.4+ to fix an error for flip-flop with beginless or endless ranges.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

undefined method type for nil:NilClass
2 participants