Rubocop: Style/SymbolArray auto-correct creating invalid syntax

Created on 8 Jun 2017  路  3Comments  路  Source: rubocop-hq/rubocop

This seems to be more of surprising behavior with the Ruby parser than Rubocop, but nonetheless it is still creating errors.

# original, valid syntax, pre-Rubocop
prepend_before_filter only: [ :create, :destroy ] { request.env["devise.skip_timeout"] = true }

# invalid (Ruby 2.2.4) syntax created by Rubocop
prepend_before_filter only: %i[create destroy] { request.env["devise.skip_timeout"] = true }

The Ruby error:

app/controllers/users/sessions_controller.rb:4: syntax error, unexpected '{', expecting keyword_end
...tion only: %i[create destroy] { request.env['devise.skip_tim...
...                               ^
app/controllers/users/sessions_controller.rb:4: syntax error, unexpected '}', expecting keyword_end

Expected behavior

When running rubocop -a, it produces valid Ruby syntax.

Actual behavior

It creates invalid syntax.

Steps to reproduce the problem

  1. Given a file:

    # thing.rb
    class Thing
      do_anything only: [:word, :two] { 5 }
    end
    
  2. When you run Rubocop with auto-correct rubocop -a thing.rb

  3. It produces an error:

    Inspecting 1 file
    E
    
    Offenses:
    
    thing.rb:2:21: C: [Corrected] Use %i or %I for an array of symbols.
    do_anything only: [:word, :two] { 5 }
                      ^^^^^^^^^^^^^
    thing.rb:2:34: E: unexpected token tLCURLY
    (Using Ruby 2.1 parser; configure using TargetRubyVersion parameter, under AllCops)
    do_anything only: %i[word two] { 5 }
                                   ^
    
    1 file inspected, 2 offenses detected, 1 offense corrected
    

RuboCop version

0.48.1 (using Parser 2.4.0.0, running on ruby 2.2.4 x86_64-darwin16)

bug

Most helpful comment

As a note, I have opened an upstream issue for this in Redmine.

All 3 comments

Interesting. Looks like parser follows MRI and throws a parsing error as well. Good catch!

The above code has same problem...

# not error
foo [:foo, :bar] { something }

# Syntax error
foo %i[foo bar] { something }

Ref:

As a note, I have opened an upstream issue for this in Redmine.

Was this page helpful?
0 / 5 - 0 ratings