Skip to content

Commit

Permalink
Fix false positives for Style/SymbolArray
Browse files Browse the repository at this point in the history
This PR fixes false positives for `Style/SymbolArray`
when `%i` array containing unescaped `[`, `]`, `(`, or `)`.

I noticed the following false positive in the RuboCop AST repository:

```console
$ cd path/to/rubocop-ast
$ bundle exec rubocop --only Style/SymbolArray
(snip)

spec/rubocop/ast/node_pattern/lexer_spec.rb:36:9: C: [Correctable] Style/SymbolArray:
Use [:'(', :array, :sym, :'$', :int, :+, :x, :')'] for an array of symbols.
        %i[( array sym $ int + x )]
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^

163 files inspected, 1 offense detected, 1 offense autocorrectable
```
  • Loading branch information
koic committed Aug 14, 2023
1 parent ff8c5ff commit 89eb905
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 89eb905

Please sign in to comment.