Rubocop: Make `Style/SymbolArray` cop to enable by default

Created on 15 Mar 2017  路  6Comments  路  Source: rubocop-hq/rubocop

Expected behavior

Style/SymbolArray:
  Enabled: true

Actual behavior

Style/SymbolArray:
  Enabled: false

I think the Style/SymbolArray cop should be enabled by default, but the cop is disabled by default.
https://github.com/bbatsov/rubocop/blob/master/config/disabled.yml#L117-L120
https://github.com/bbatsov/rubocop/commit/40cf6848ffc047358371b0d958511b28fbc67fe9
When the cop was added, TargetRubyVersion config does not exist.
However currently TargetRubyVersion exists, so we can make the cop to enable by default.

However, the cop has some problems in order to make to enable by default.

Handle TargetRubyVersion

I guess the cop does not properly handle TargetRubyVersion config.
https://github.com/bbatsov/rubocop/blob/a0f3a8f1223ba8d5b45e1bfbd950ff6f81b275f7/lib/rubocop/cop/style/symbol_array.rb#L27-L38
I think the validate_config method is unnecessarily. We should use minimum_target_ruby_version instead of validate_config.

Bug

The cop has a bug.

For example:

.rubocop.yml

Style/SymbolArray:
  Enabled: true
  EnforcedStyle: brackets

test.rb

%i(a a-b)
$ rubocop --only SmybolArray -a
Inspecting 1 file
C

Offenses:

test.rb:1:1: C: [Corrected] Use [] for an array of symbols.
%i[a a-b]
^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected

$ cat test.rb
[:a, :a-b]

The meaning of %i(a a-b) and [:a, :a-b] is different, but RuboCop add an offense for this code.

Auto correct

The cop supports auto correction. However I think the feature is not enough.

For example

FILTER_METHODS = [
  :after_filter,
  :append_after_filter,
  :append_around_filter,
  :append_before_filter,
  :around_filter,
  :before_filter,
  :prepend_after_filter,
  :prepend_around_filter,
  :prepend_before_filter,
  :skip_after_filter,
  :skip_around_filter,
  :skip_before_filter,
  :skip_filter
].freeze
FILTER_METHODS = %i(after_filter append_after_filter append_around_filter append_before_filter around_filter before_filter prepend_after_filter prepend_around_filter prepend_before_filter skip_after_filter skip_around_filter skip_before_filter skip_filter).freeze

The first code is auto-corrected to second code. The second code is not readable. We will need to add lien breaks manually to this code.

Style/WordArray supports line brakes feature, so, we should support the feature for Style/SymbolArray as well.


I'll implement the above features, and I'll make to enable by default the cop when finished.

enhancement

Most helpful comment

I just think its purely preferential and is doing nothing to add to code readability and if anything its decreasing it. This should _not_ be enabled by default.

All 6 comments

I'll implement the above features, and I'll make to enable by default the cop when finished.

Fine by me. The reason this cop is not enabled by default is that it was developed when we still supported Ruby 1.9.

For the record, I disagree. This seems like a strange thing to force on people by default given there is no inherent advantage, and it adds cognitive overload for developers.

@LunaCodeGirl I am late but agrees, as a newcomer Ruby is complex enough and I wish this was not enabled by default. Pure preference but the least amount of tokens I have to learn the better, I had to learn what was a Symbol and now rubocop introduces %i on top of it ahah :D

I just think its purely preferential and is doing nothing to add to code readability and if anything its decreasing it. This should _not_ be enabled by default.

Thanks for your feedback.
RuboCop's default rule is based on the Ruby Style Guide, so if you'd like to change the default, I recommend to open an issue to the style guide (opening an issue to this repository is also ok).
https://github.com/rubocop-hq/ruby-style-guide#i

By the way, personally I still think this cop should be enabled by default.
I agree with the cop does not improve readability.
But it improves writability by reducing typing : and , in my opinion. You can find good examples from this file. https://github.com/rubocop-hq/rubocop/blob/7e37a77d40e0cb45625bb72c7af051c6fad6932a/lib/rubocop/ast/node.rb

And, I think not all Cops improve readability directly. Readability is important, but RuboCop also focuses on consistency. We can enforce ourselves that we use %i[] or [] with this cop.

For anyone here wondering how to disable this madness, here's a snippet copy/paste ready.

in your .rubocop.yml:

Style/SymbolArray:
  Enabled: false
Was this page helpful?
0 / 5 - 0 ratings