Skip to content

Commit

Permalink
[Fix rubocop#4781] Prevent Style/UnneededPercentQ from breaking on …
Browse files Browse the repository at this point in the history
…strings that is concated with backslash

`Style/UnneededPercentQ` raises an error when it is auto-correcting the following code.

```ruby
puts %q(foo bar baz) \
  'boogers'
```

This change will fix this problem.
  • Loading branch information
pocke committed Nov 11, 2017
1 parent bf3edd8 commit adc0362
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* [#4885](https://github.com/bbatsov/rubocop/issues/4885): Fix false offense detected by `Style/MixinUsage` cop. ([@koic][])
* [#3363](https://github.com/bbatsov/rubocop/pull/3363): Fix `Style/EmptyElse` autocorrection removes comments from branches. ([@dpostorivo][])
* [#5025](https://github.com/bbatsov/rubocop/issues/5025): Fix error with Layout/MultilineMethodCallIndentation cop and lambda.(...). ([@dpostorivo][])
* [#4781](https://github.com/bbatsov/rubocop/issues/4781): Prevent `Style/UnneededPercentQ` from breaking on strings that are concated with backslash. ([@pocke][])

### Changes

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/style/unneeded_percent_q.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class UnneededPercentQ < Cop
ESCAPED_NON_BACKSLASH = /\\[^\\]/

def on_dstr(node)
return unless string_literal?(node)
check(node)
end

Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/cop/style/unneeded_percent_q_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@

expect(new_source).to eq("'hi'")
end

it 'auto-corrects for strings that is concated with backslash' do
new_source = autocorrect_source(<<-RUBY.strip_indent)
%q(foo bar baz) \
'boogers'
RUBY
expect(new_source).to eq(<<-RUBY.strip_indent)
'foo bar baz' \
'boogers'
RUBY
end
end
end

Expand Down Expand Up @@ -128,6 +139,17 @@

expect(new_source).to eq(%("hi\#{4}"))
end

it 'auto-corrects for strings that is concated with backslash' do
new_source = autocorrect_source(<<-RUBY.strip_indent)
%Q(foo bar baz) \
'boogers'
RUBY
expect(new_source).to eq(<<-RUBY.strip_indent)
"foo bar baz" \
'boogers'
RUBY
end
end
end

Expand Down

0 comments on commit adc0362

Please sign in to comment.