Skip to content

Commit

Permalink
Merge pull request #12120 from koic/fix_a_false_positive_for_style_sy…
Browse files Browse the repository at this point in the history
…mbol_array

Fix false positives for `Style/SymbolArray`
  • Loading branch information
koic committed Aug 15, 2023
2 parents 5ca04a2 + 89eb905 commit 9a143e6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/fix_false_positives_for_style_symbol_array.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12119](https://github.com/rubocop/rubocop/pull/12119): Fix false positives for `Style/SymbolArray` when `%i` array containing unescaped `[`, `]`, `(`, or `)`. ([@koic][])
2 changes: 2 additions & 0 deletions lib/rubocop/cop/style/symbol_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def on_array(node)

def complex_content?(node)
node.children.any? do |sym|
return false if DELIMITERS.include?(sym.source)

content = *sym
content = content.map { |c| c.is_a?(AST::Node) ? c.source : c }.join
content_without_delimiter_pairs = content.gsub(/(\[[^\s\[\]]*\])|(\([^\s\(\)]*\))/, '')
Expand Down
16 changes: 14 additions & 2 deletions spec/rubocop/cop/style/symbol_array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
end

it 'registers an offense for a %i array containing [ ]' do
it 'registers an offense for a %i array containing escaped [ ]' do
expect_offense(<<~'RUBY')
%i[one \[ \] two]
^^^^^^^^^^^^^^^^^ Use `[:one, :'[', :']', :two]` for an array of symbols.
Expand All @@ -149,6 +149,12 @@
RUBY
end

it 'does not register an offense for a %i array containing unescaped [ ]' do
expect_no_offenses(<<~RUBY)
%i(one [ ] two)
RUBY
end

it 'registers an offense for a %i array containing whitespace between brackets' do
expect_offense(<<~'RUBY')
%i[one two \[three\ four\ five\]]
Expand All @@ -171,7 +177,7 @@
RUBY
end

it 'registers an offense for a %i array containing ( )' do
it 'registers an offense for a %i array containing escaped ( )' do
expect_offense(<<~'RUBY')
%i(one \( \) two)
^^^^^^^^^^^^^^^^^ Use `[:one, :'(', :')', :two]` for an array of symbols.
Expand All @@ -182,6 +188,12 @@
RUBY
end

it 'does not register an offense for a %i array containing unescaped ( )' do
expect_no_offenses(<<~RUBY)
%i[one ( ) two]
RUBY
end

it 'registers an offense for a %i array containing parentheses between parentheses' do
expect_offense(<<~'RUBY')
%i(one two \(\(\))
Expand Down

0 comments on commit 9a143e6

Please sign in to comment.