diff --git a/CHANGELOG.md b/CHANGELOG.md index a047b943c2d..39bcd1d94c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/rubocop/cop/correctors/percent_literal_corrector.rb b/lib/rubocop/cop/correctors/percent_literal_corrector.rb index 41428b0e7ec..d2b1a0d0db9 100644 --- a/lib/rubocop/cop/correctors/percent_literal_corrector.rb +++ b/lib/rubocop/cop/correctors/percent_literal_corrector.rb @@ -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 diff --git a/spec/rubocop/cop/style/symbol_array_spec.rb b/spec/rubocop/cop/style/symbol_array_spec.rb index 824ab84ce83..8e2848ac505 100644 --- a/spec/rubocop/cop/style/symbol_array_spec.rb +++ b/spec/rubocop/cop/style/symbol_array_spec.rb @@ -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)") diff --git a/spec/rubocop/cop/style/word_array_spec.rb b/spec/rubocop/cop/style/word_array_spec.rb index 843dea3ddb8..a5e3191ec45 100644 --- a/spec/rubocop/cop/style/word_array_spec.rb +++ b/spec/rubocop/cop/style/word_array_spec.rb @@ -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