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

Style/HashAsLastArrayItem copy with style "no_braces" can result in invalid code #8323

Closed
aried3r opened this issue Jul 13, 2020 · 0 comments · Fixed by #8333
Closed

Style/HashAsLastArrayItem copy with style "no_braces" can result in invalid code #8323

aried3r opened this issue Jul 13, 2020 · 0 comments · Fixed by #8333
Labels

Comments

@aried3r
Copy link

aried3r commented Jul 13, 2020

Expected behavior

Using Style/HashAsLastArrayItem with EnforcedStyle: no_braces should yield valid Ruby code.

I was planning to use braces but wanted to see which parts of our code base might use the no_braces style currently and wanted to see what would change with either of the two styles.

Actual behavior

The cop is not safe and results in invalid Ruby code.

Steps to reproduce the problem

# .rubocop.yml
AllCops:
  NewCops: enable

Style/HashAsLastArrayItem:
  EnforcedStyle: no_braces
# hash_as_last_array_item.rb before
# frozen-string-literal: true

a = [
  {
    foo: :bar
  },
  {
    foo: :baz
  }
]

pp a
$ ruby hash_as_last_array_item.rb
[{:foo=>:bar}, {:foo=>:baz}]
$ rubocop -a hash_as_last_array_item.rb
Inspecting 1 file
E

Offenses:

hash_as_last_array_item.rb:4:3: C: [Corrected] Style/HashAsLastArrayItem: Omit the braces around the hash.
  { ...
  ^
hash_as_last_array_item.rb:6:3: E: Lint/Syntax: unexpected token tCOMMA
(Using Ruby 2.4 parser; configure using TargetRubyVersion parameter, under AllCops)
  ,
  ^
hash_as_last_array_item.rb:7:3: C: [Corrected] Style/HashAsLastArrayItem: Omit the braces around the hash.
  { ...
  ^

1 file inspected, 3 offenses detected, 2 offenses corrected
# hash_as_last_array_item.rb after
# frozen-string-literal: true

a = [
  
    foo: :bar
  ,
  
    foo: :baz
  
]

pp a
$ ruby hash_as_last_array_item.rb
hash_as_last_array_item.rb:6: syntax error, unexpected ',', expecting ']'
hash_as_last_array_item.rb:10: syntax error, unexpected ']', expecting end-of-input

RuboCop version

$ rubocop -V
0.88.0 (using Parser 2.7.1.4, rubocop-ast 0.1.0, running on ruby 2.7.1 x86_64-darwin19)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants