Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix auto-correction for Style/SymbolArray with array contains interpolation #6802

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* [#6699](https://github.com/rubocop-hq/rubocop/issues/6699): Fix infinite loop for `Layout/IndentationWidth` and `Layout/IndentationConsistency` when bad modifier indentation before good method definition. ([@koic][])
* [#6777](https://github.com/rubocop-hq/rubocop/issues/6777): Fix a false positive for `Style/TrivialAccessors` when using trivial reader/writer methods at the top level. ([@koic][])
* [#6799](https://github.com/rubocop-hq/rubocop/pull/6799): Fix errors for `Style/ConditionalAssignment`, `Style/IdenticalConditionalBranches`, `Lint/ElseLayout`, and `Layout/IndentationWidth` with empty braces. ([@pocke][])
* [#6802](https://github.com/rubocop-hq/rubocop/pull/6802): Fix auto-correction for `Style/SymbolArray` with array contains interpolation when `EnforcedStyle` is `brackets`. ([@pocke][])

### Changes

Expand Down
10 changes: 9 additions & 1 deletion lib/rubocop/cop/style/symbol_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,15 @@ def symbols_contain_spaces?(node)
end

def correct_bracketed(node)
syms = node.children.map { |c| to_symbol_literal(c.value.to_s) }
syms = node.children.map do |c|
if c.dsym_type?
string_literal = to_string_literal(c.source)

':' + trim_string_interporation_escape_character(string_literal)
else
to_symbol_literal(c.value.to_s)
end
end

lambda do |corrector|
corrector.replace(node.source_range, "[#{syms.join(', ')}]")
Expand Down
4 changes: 0 additions & 4 deletions lib/rubocop/cop/style/word_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ def correct_bracketed(node)
corrector.replace(node.source_range, "[#{words.join(', ')}]")
end
end

def trim_string_interporation_escape_character(str)
str.gsub(/\\\#{(.*?)\}/) { "\#{#{Regexp.last_match(1)}}" }
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/cop/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ def to_string_literal(string)
end
end

def trim_string_interporation_escape_character(str)
str.gsub(/\\\#{(.*?)\}/) { "\#{#{Regexp.last_match(1)}}" }
end

def interpret_string_escapes(string)
StringInterpreter.interpret(string)
end
Expand Down
7 changes: 6 additions & 1 deletion spec/rubocop/cop/style/symbol_array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
end
end

context 'when EnforcedStyle is array' do
context 'when EnforcedStyle is brackets' do
let(:cop_config) { { 'EnforcedStyle' => 'brackets', 'MinSize' => 0 } }

it 'does not register an offense for arrays of symbols' do
Expand All @@ -167,6 +167,11 @@
new_source = autocorrect_source('%i(one @two $three four-five)')
expect(new_source).to eq("[:one, :@two, :$three, :'four-five']")
end

it 'autocorrects an array has interpolations' do
new_source = autocorrect_source('%I(#{foo} #{foo}bar foo#{bar} foo)')
expect(new_source).to eq('[:"#{foo}", :"#{foo}bar", :"foo#{bar}", :foo]')
end
end

context 'with non-default MinSize' do
Expand Down