Skip to content

Commit

Permalink
[Fix rubocop#10008] Tweak offense message for Style/WordArray and `…
Browse files Browse the repository at this point in the history
…Style/SymbolArray` cops

Fixes rubocop#10007 and rubocop#10008.

This PR tweaks offense message for `Style/WordArray` and `Style/SymbolArray` cops.
It is a clear message suggested by rubocop#10008.
  • Loading branch information
koic authored and bbatsov committed Aug 16, 2021
1 parent b51c6f7 commit dcc4df5
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 18 deletions.
7 changes: 5 additions & 2 deletions lib/rubocop/cop/mixin/percent_array.rb
Expand Up @@ -38,8 +38,11 @@ def check_percent_array(node)

return unless style == :brackets || invalid_percent_array_contents?(node)

add_offense(node, message: self.class::ARRAY_MSG) do |corrector|
correct_bracketed(corrector, node)
bracketed_array = build_bracketed_array(node)
message = format(self.class::ARRAY_MSG, prefer: bracketed_array)

add_offense(node, message: message) do |corrector|
corrector.replace(node, bracketed_array)
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/rubocop/cop/style/symbol_array.rb
Expand Up @@ -35,7 +35,7 @@ class SymbolArray < Base
extend AutoCorrector

PERCENT_MSG = 'Use `%i` or `%I` for an array of symbols.'
ARRAY_MSG = 'Use `[]` for an array of symbols.'
ARRAY_MSG = 'Use `%<prefer>s` for an array of symbols.'

class << self
attr_accessor :largest_brackets
Expand All @@ -60,7 +60,7 @@ def symbols_contain_spaces?(node)
end
end

def correct_bracketed(corrector, node)
def build_bracketed_array(node)
syms = node.children.map do |c|
if c.dsym_type?
string_literal = to_string_literal(c.source)
Expand All @@ -71,7 +71,7 @@ def correct_bracketed(corrector, node)
end
end

corrector.replace(node, "[#{syms.join(', ')}]")
"[#{syms.join(', ')}]"
end

def to_symbol_literal(string)
Expand Down
6 changes: 3 additions & 3 deletions lib/rubocop/cop/style/word_array.rb
Expand Up @@ -44,7 +44,7 @@ class WordArray < Base
extend AutoCorrector

PERCENT_MSG = 'Use `%w` or `%W` for an array of words.'
ARRAY_MSG = 'Use `[]` for an array of words.'
ARRAY_MSG = 'Use `%<prefer>s` for an array of words.'

class << self
attr_accessor :largest_brackets
Expand Down Expand Up @@ -82,7 +82,7 @@ def word_regex
Regexp.new(cop_config['WordRegex'])
end

def correct_bracketed(corrector, node)
def build_bracketed_array(node)
words = node.children.map do |word|
if word.dstr_type?
string_literal = to_string_literal(word.source)
Expand All @@ -93,7 +93,7 @@ def correct_bracketed(corrector, node)
end
end

corrector.replace(node, "[#{words.join(', ')}]")
"[#{words.join(', ')}]"
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/rubocop/cop/style/symbol_array_spec.rb
Expand Up @@ -201,7 +201,7 @@
it 'registers an offense for array starting with %i' do
expect_offense(<<~RUBY)
%i(one two three)
^^^^^^^^^^^^^^^^^ Use `[]` for an array of symbols.
^^^^^^^^^^^^^^^^^ Use `[:one, :two, :three]` for an array of symbols.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -212,7 +212,7 @@
it 'autocorrects an array starting with %i' do
expect_offense(<<~RUBY)
%i(one @two $three four-five)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `[]` for an array of symbols.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `[:one, :@two, :$three, :'four-five']` for an array of symbols.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -223,7 +223,7 @@
it 'autocorrects an array has interpolations' do
expect_offense(<<~'RUBY')
%I(#{foo} #{foo}bar foo#{bar} foo)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `[]` for an array of symbols.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `[:"#{foo}", :"#{foo}bar", :"foo#{bar}", :foo]` for an array of symbols.
RUBY

expect_correction(<<~'RUBY')
Expand Down
14 changes: 7 additions & 7 deletions spec/rubocop/cop/style/word_array_spec.rb
Expand Up @@ -321,7 +321,7 @@
it 'registers an offense for a %w() array containing spaces' do
expect_offense(<<~'RUBY')
%w(one\ two three\ four)
^^^^^^^^^^^^^^^^^^^^^^^^ Use `[]` for an array of words.
^^^^^^^^^^^^^^^^^^^^^^^^ Use `['one two', 'three four']` for an array of words.
RUBY

expect_correction(<<~RUBY)
Expand Down Expand Up @@ -373,7 +373,7 @@
it 'registers an offense for a %w() array' do
expect_offense(<<~RUBY)
%w(one two three)
^^^^^^^^^^^^^^^^^ Use `[]` for an array of words.
^^^^^^^^^^^^^^^^^ Use `['one', 'two', 'three']` for an array of words.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -384,7 +384,7 @@
it 'autocorrects a %w() array which uses single quotes' do
expect_offense(<<~RUBY)
%w(one's two's three's)
^^^^^^^^^^^^^^^^^^^^^^^ Use `[]` for an array of words.
^^^^^^^^^^^^^^^^^^^^^^^ Use `["one's", "two's", "three's"]` for an array of words.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -395,7 +395,7 @@
it 'autocorrects a %W() array which uses escapes' do
expect_offense(<<~'RUBY')
%W(\n \t \b \v \f)
^^^^^^^^^^^^^^^^^^ Use `[]` for an array of words.
^^^^^^^^^^^^^^^^^^ Use `["\n", "\t", "\b", "\v", "\f"]` for an array of words.
RUBY

expect_correction(<<~'RUBY')
Expand All @@ -406,7 +406,7 @@
it 'autocorrects a %w() array which uses string with hyphen' do
expect_offense(<<~RUBY)
%w(foo bar foo-bar)
^^^^^^^^^^^^^^^^^^^ Use `[]` for an array of words.
^^^^^^^^^^^^^^^^^^^ Use `['foo', 'bar', 'foo-bar']` for an array of words.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -417,7 +417,7 @@
it 'autocorrects a %W() array which uses string with hyphen' do
expect_offense(<<~'RUBY')
%W(foo bar #{foo}-bar)
^^^^^^^^^^^^^^^^^^^^^^ Use `[]` for an array of words.
^^^^^^^^^^^^^^^^^^^^^^ Use `['foo', 'bar', "#{foo}-bar"]` for an array of words.
RUBY

expect_correction(<<~'RUBY')
Expand All @@ -428,7 +428,7 @@
it 'autocorrects a %W() array which uses string interpolation' do
expect_offense(<<~'RUBY')
%W(#{foo}bar baz)
^^^^^^^^^^^^^^^^^ Use `[]` for an array of words.
^^^^^^^^^^^^^^^^^ Use `["#{foo}bar", 'baz']` for an array of words.
RUBY

expect_correction(<<~'RUBY')
Expand Down

0 comments on commit dcc4df5

Please sign in to comment.