Skip to content

Commit

Permalink
Fix Style/WordArray for subarrays
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima authored and bbatsov committed Jan 1, 2023
1 parent d5c303a commit 3cce8c8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/rubocop/cop/style/word_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ def on_array(node)
def within_2d_array_of_complex_content?(node)
return false unless (parent = node.parent)

parent.array_type? && parent.values.any? { |subarray| complex_content?(subarray.values) }
parent.array_type? &&
parent.values.all?(&:array_type?) &&
parent.values.any? { |subarray| complex_content?(subarray.values) }
end

def complex_content?(strings, complex_regex: word_regex)
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/style/word_array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,17 @@
]
RUBY
end

it 'registers an offense and corrects for nested arrays' do
expect_offense(<<~RUBY)
[['one', 'One'], 2, 3]
^^^^^^^^^^^^^^ Use `%w` or `%W` for an array of words.
RUBY

expect_correction(<<~RUBY)
[%w(one One), 2, 3]
RUBY
end
end

context 'when EnforcedStyle is array' do
Expand Down

0 comments on commit 3cce8c8

Please sign in to comment.