Skip to content

Commit

Permalink
Merge pull request #8598 from owst/fix_line_continuation_redundant_re…
Browse files Browse the repository at this point in the history
…gexp_escape

[Fix #8583] Allow line continuations in `Style/RedundantRegexpEscape`
  • Loading branch information
koic committed Aug 26, 2020
2 parents 8d461ad + b8707ef commit 362df66
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -30,6 +30,7 @@
* [#7705](https://github.com/rubocop-hq/rubocop/issues/7705): Fix `Style/OneLineConditional` cop to handle if/then/elsif/then/else/end cases. Add `AlwaysCorrectToMultiline` config option to this cop to always convert offenses to the multi-line form (false by default). ([@Lykos][], [@dsavochkin][])
* [#8590](https://github.com/rubocop-hq/rubocop/issues/8590): Fix an error when auto-correcting encoding mismatch file. ([@koic][])
* [#8321](https://github.com/rubocop-hq/rubocop/issues/8321): Enable auto-correction for `Layout/{Def}EndAlignment`, `Lint/EmptyEnsure`. ([@marcandre][])
* [#8583](https://github.com/rubocop-hq/rubocop/issues/8583): Fix `Style/RedundantRegexpEscape` false positive for line continuations. ([@owst][])

### Changes

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/redundant_regexp_escape.rb
Expand Up @@ -38,7 +38,7 @@ class RedundantRegexpEscape < Cop

MSG_REDUNDANT_ESCAPE = 'Redundant escape inside regexp literal'

ALLOWED_ALWAYS_ESCAPES = ' []^\\#'.chars.freeze
ALLOWED_ALWAYS_ESCAPES = " \n[]^\\#".chars.freeze
ALLOWED_WITHIN_CHAR_CLASS_METACHAR_ESCAPES = '-'.chars.freeze
ALLOWED_OUTSIDE_CHAR_CLASS_METACHAR_ESCAPES = '.*+?{}()|$'.chars.freeze

Expand Down
15 changes: 15 additions & 0 deletions spec/rubocop/cop/style/redundant_regexp_escape_spec.rb
Expand Up @@ -16,6 +16,18 @@
end
end

context 'with a line continuation' do
it 'does not register an offense' do
expect_no_offenses("foo = /a\\\nb/")
end
end

context 'with a line continuation within a character class' do
it 'does not register an offense' do
expect_no_offenses("foo = /[a\\\nb]/")
end
end

[
('a'..'z').to_a - %w[c n p u x],
('A'..'Z').to_a - %w[C M P],
Expand Down Expand Up @@ -277,6 +289,9 @@
end
end

# Avoid an empty character class
next if char == "\n"

context "with an escaped '#{char}' inside a character class" do
it 'does not register an offense' do
expect_no_offenses("foo = /[\\#{char}]/")
Expand Down

0 comments on commit 362df66

Please sign in to comment.