Skip to content

Commit

Permalink
Fix autocorrect for percent literal array with newlines
Browse files Browse the repository at this point in the history
Fix line number when array element contains newline so that extra newlines
are not added to the result

Use expect_correction for embedded newline in percent literal array
  • Loading branch information
biinari committed Aug 7, 2020
1 parent 0eae367 commit 86b1bd5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
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

0 comments on commit 86b1bd5

Please sign in to comment.