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 autocorrect for percent literal array with newlines #8481

Merged
merged 1 commit into from Aug 7, 2020
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
Expand Up @@ -8,6 +8,7 @@
* [#8466](https://github.com/rubocop-hq/rubocop/issues/8466): Fix a false positive for `Lint/UriRegexp` when using `regexp` method without receiver. ([@koic][])
* [#8478](https://github.com/rubocop-hq/rubocop/issues/8478): Relax `Lint/BinaryOperatorWithIdenticalOperands` for mathematical operations. ([@marcandre][])
* [#8480](https://github.com/rubocop-hq/rubocop/issues/8480): Tweak callback list of `Lint/MissingSuper`. ([@marcandre][])
* [#8481](https://github.com/rubocop-hq/rubocop/pull/8481): Fix autocorrect for elements with newlines in `Style/SymbolArray` and `Style/WordArray`. ([@biinari][])

## 0.89.0 (2020-08-05)

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/correctors/percent_literal_corrector.rb
Expand Up @@ -71,7 +71,7 @@ def process_multiline_words(node, escape, delimiters)
prev_line_num,
base_line_num,
index)
prev_line_num = word_node.first_line
prev_line_num = word_node.last_line
content = fix_escaped_content(word_node, escape, delimiters)
line_breaks + content
end
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/style/symbol_array_spec.rb
Expand Up @@ -39,6 +39,18 @@
expect(new_source).to eq('%i(one)')
end

it 'autocorrects arrays of symbols with embedded newlines and tabs' do
expect_offense(<<~RUBY, tab: "\t")
[:"%{tab}", :"two
^^^^{tab}^^^^^^^^ Use `%i` or `%I` for an array of symbols.
", :three]
RUBY

expect_correction(<<~'RUBY')
%I(\t two\n three)
RUBY
end

it 'autocorrects arrays of symbols with new line' do
new_source = autocorrect_source("[:one,\n:two, :three,\n:four]")
expect(new_source).to eq("%i(one\ntwo three\nfour)")
Expand Down
13 changes: 10 additions & 3 deletions spec/rubocop/cop/style/word_array_spec.rb
Expand Up @@ -58,9 +58,16 @@
RUBY
end

it 'registers an offense for strings with embedded newlines and tabs' do
inspect_source(%(["one\n", "hi\tthere"]))
expect(cop.offenses.size).to eq(1)
it 'uses %W when autocorrecting strings with embedded newlines and tabs' do
expect_offense(<<~RUBY)
["one
^^^^^ Use `%w` or `%W` for an array of words.
", "hi\tthere"]
RUBY

expect_correction(<<~'RUBY')
%W(one\n hi\tthere)
RUBY
end

it 'registers an offense for strings with newline and tab escapes' do
Expand Down