Rubocop: SpaceAroundOperators raised when the previous aligned line has brackets

Created on 31 Jul 2017  路  8Comments  路  Source: rubocop-hq/rubocop

Here is the example snippet to reproduce the error

mamamie = ''
test    = [
  1, 2, 3, 4, 5, 6, 7
]
hello   = [
  2, 2, 4, 5
]

mamamie + test[0].to_s + hello[2].to_s

Expected behavior

Rubocop should pass

Actual behavior

rubocop raises an Operator = should be surrounded by a single space. error.

Steps to reproduce the problem

run rubocop against

mamamie = ''
test    = [
  1, 2, 3, 4, 5, 6, 7
]
hello   = [
  2, 2, 4, 5
]

mamamie + test[0].to_s + hello[2].to_s

Or some other example
https://github.com/wikimedia/mediawiki-ruby-api/blob/master/mediawiki_api.gemspec

RuboCop version

Include the output of rubocop -V. Here's an example:

$ rubocop -V
0.49.1 (using Parser 2.4.0.0, running on ruby 2.4.1 x86_64-darwin16)
bug stale

All 8 comments

From what I see in specs to that Cop, it's expected behavior:

    it 'registers an offense for assignment with many spaces on either side' do
      expect_offense(<<-RUBY.strip_indent)
        x   = 0
            ^ Operator `=` should be surrounded by a single space.
        y +=   0
          ^^ Operator `+=` should be surrounded by a single space.
        z[0]  =  0
              ^ Operator `=` should be surrounded by a single space.
      RUBY
    end

Probably it's a good idea to be able to disable that check for assignment.
But to fix this offense you can change code to:

test = [
  1, 2, 3, 4, 5, 6, 7
]

@smakagon
I don't think it is expected
https://github.com/bbatsov/rubocop/blob/master/lib/rubocop/cop/layout/space_around_operators.rb#L115
!aligned_with_operator?(op) i do believe it means, if it aligned with other lines, until non empty line, it should not raise an error

Here is my current findings:
In specs I see that AllowForAlignment is set to true but still specs expect this to add an offense:

x     = 0

I'm not sure if it's correct behavior with that config.

Speaking about this method:

        def excess_leading_space?(op, with_space)
          with_space.source =~ /^  / &&
            (!allow_for_alignment? || !aligned_with_operator?(op))
        end

For described case, this code aligned_with_operator?(op) returns false so this: !aligned_with_operator?(op) returns true. It makes this cop to log offense.

Seems like specs should be fixed and usage/logic of aligned_with_operator?(op) should be refined.

Btw, from source code and documentation I'm not sure I understand how AllowForAlignment supposed to work. For which operators AllowForAlignment should allow more than one space? If that should be applied just to assignments I could provide fix.

@Drenmi @bbatsov WDYT?

My opinion is that it's a bug. The code in preceding_following_alignment.rb looks like we want to support the above case.

Also seen in:

OPEN_OBJECT = {
  'str1'      => {
    'str2'      => 'str3',
    'str4_long' => 'str5'
  },
  'str6'      => {},
  'str7_long' => { 'str8' => 'str9' }
}.freeze

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding!

This issues been automatically closed due to lack of activity. Feel free to re-open it if you ever come back to it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cabello picture cabello  路  3Comments

tedPen picture tedPen  路  3Comments

mikegee picture mikegee  路  3Comments

printercu picture printercu  路  3Comments

AndreiMotinga picture AndreiMotinga  路  3Comments