Skip to content

Commit

Permalink
[Fix rubocop#3063] Don't auto-correct a + \\ into a + \\\\ (ruboc…
Browse files Browse the repository at this point in the history
…op#3098)

Take care not to create a double backslash (a syntax error)
when auto-correcting a + used for string concatenation into
a backslash.

The reporting of the offense remains the same, i.e., no
special message for "you already have a backslash, just
remove the +".
  • Loading branch information
jonas054 authored and bbatsov committed May 1, 2016
1 parent 4d5176e commit 0bca541
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
* [#3061](https://github.com/bbatsov/rubocop/pull/3061): Custom cops now show up in --show-cops. ([@ptarjan][])
* [#3088](https://github.com/bbatsov/rubocop/pull/3088): Ignore offenses that involve conflicting HEREDOCs in the `Style/Multiline*BraceLayout` cops. ([@panthomakos][])
* [#3083](https://github.com/bbatsov/rubocop/issues/3083): Do not register an offense for splat block args in `Style/SymbolProc`. ([@rrosenblum][])
* [#3063](https://github.com/bbatsov/rubocop/issues/3063): Don't auto-correct `a + \` into `a + \\` in `Style/LineEndConcatenation`. ([@jonas054][])

### Changes

Expand Down
5 changes: 5 additions & 0 deletions lib/rubocop/cop/style/line_end_concatenation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ def autocorrect(operator_range)
operator_range = range_with_surrounding_space(operator_range,
:right,
!:with_newline)
one_more_char = operator_range.resize(operator_range.size + 1)
# Don't create a double backslash at the end of the line, in case
# there already was a backslash after the concatenation operator.
operator_range = one_more_char if one_more_char.source.end_with?('\\')

->(corrector) { corrector.replace(operator_range, '\\') }
end

Expand Down
7 changes: 7 additions & 0 deletions spec/rubocop/cop/style/line_end_concatenation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@
expect(corrected).to eq ['top = "test" \\', '"top"'].join("\n")
end

it 'autocorrects a + with \\ to just \\' do
corrected = autocorrect_source(cop,
['top = "test" + \\',
'"top"'])
expect(corrected).to eq ['top = "test" \\', '"top"'].join("\n")
end

it 'autocorrects for chained concatenations and << calls' do
corrected = autocorrect_source(cop,
['top = "test#{x}" <<',
Expand Down

0 comments on commit 0bca541

Please sign in to comment.