Skip to content

Commit

Permalink
Merge pull request #12088 from jasondoc3/jason/fix-false-positive-sym…
Browse files Browse the repository at this point in the history
…bol-array

[Fix #12071] Fix `Style/SymbolArray` false positives when using square brackets or interpolation in a symbol literal within a percent style array
  • Loading branch information
koic committed Aug 1, 2023
2 parents 4a75995 + c5cefae commit d8646b4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog/fix_style_symbol_array_false_positives.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12071](https://github.com/rubocop/rubocop/issues/12071): Fix `Style/SymbolArray` false positives when using square brackets or interpolation in a symbol literal in a percent style array. ([@jasondoc3][])
6 changes: 3 additions & 3 deletions lib/rubocop/cop/style/symbol_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ def on_array(node)

def complex_content?(node)
node.children.any? do |sym|
content, = *sym
content = content.to_s
content_without_delimiter_pairs = content.gsub(/(\[\])|(\(\))/, '')
content = *sym
content = content.map { |c| c.is_a?(AST::Node) ? c.source : c }.join
content_without_delimiter_pairs = content.gsub(/(\[[^\s\[\]]*\])|(\([^\s\(\)]*\))/, '')

content.include?(' ') || DELIMITERS.any? do |delimiter|
content_without_delimiter_pairs.include?(delimiter)
Expand Down
50 changes: 49 additions & 1 deletion spec/rubocop/cop/style/symbol_array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@
end

it 'does not register an offense for array containing delimiters without spaces' do
expect_no_offenses('%i[one two [] ()]')
expect_no_offenses('%i[zero (one) [two] three[4] five[six] seven(8) nine(ten) ([]) [] ()]')
end

it 'does not register an offense for a percent array with interpolations' do
expect_no_offenses('%I[one_#{two} three #{four}_five six#{seven}eight [nine_#{ten}]]')
end

it 'does not register an offense if symbol contains whitespace' do
Expand Down Expand Up @@ -145,6 +149,28 @@
RUBY
end

it 'registers an offense for a %i array containing whitespace between brackets' do
expect_offense(<<~'RUBY')
%i[one two \[three\ four\ five\]]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `[:one, :two, :'[three four five]']` for an array of symbols.
RUBY

expect_correction(<<~RUBY)
[:one, :two, :'[three four five]']
RUBY
end

it 'registers an offense for a %i array containing brackets between brackets' do
expect_offense(<<~'RUBY')
%i[one two \[\[\]]
^^^^^^^^^^^^^^^^^^ Use `[:one, :two, :'[[]']` for an array of symbols.
RUBY

expect_correction(<<~RUBY)
[:one, :two, :'[[]']
RUBY
end

it 'registers an offense for a %i array containing ( )' do
expect_offense(<<~'RUBY')
%i(one \( \) two)
Expand All @@ -156,6 +182,28 @@
RUBY
end

it 'registers an offense for a %i array containing parentheses between parentheses' do
expect_offense(<<~'RUBY')
%i(one two \(\(\))
^^^^^^^^^^^^^^^^^^ Use `[:one, :two, :'(()']` for an array of symbols.
RUBY

expect_correction(<<~RUBY)
[:one, :two, :'(()']
RUBY
end

it 'registers an offense for a %i array containing whitespace between parentheses' do
expect_offense(<<~'RUBY')
%i(one two \(three\ four\ five\))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `[:one, :two, :'(three four five)']` for an array of symbols.
RUBY

expect_correction(<<~RUBY)
[:one, :two, :'(three four five)']
RUBY
end

context 'when PreferredDelimiters is specified' do
let(:other_cops) do
{
Expand Down

0 comments on commit d8646b4

Please sign in to comment.