Skip to content

Commit

Permalink
Include line continuations in whitespace for NestedParenthesizedCalls
Browse files Browse the repository at this point in the history
Include backslash followed by newline as whitespace when replacing with
parentheses in Style/NestedParenthesizedCalls cop.

Fix existing spec for:

```ruby
puts(nex \
       5)
```

to auto-correct to:

```ruby
puts(nex(5))
```

Originally the spec was written such that the backslash newline gets
interpreted when parsing the heredoc as:

```ruby
"puts(nex        5)\n"
```
  • Loading branch information
biinari authored and bbatsov committed Aug 6, 2020
1 parent dfbbdd0 commit 3065500
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -53,6 +53,7 @@
* [#8226](https://github.com/rubocop-hq/rubocop/issues/8226): Fix `Style/IfUnlessModifier` to add parentheses when converting if-end condition inside an array or a hash to a single-line form. ([@dsavochkin][])
* [#8443](https://github.com/rubocop-hq/rubocop/pull/8443): Fix an incorrect auto-correct for `Style/StructInheritance` when there is a comment before class declaration. ([@koic][])
* [#8444](https://github.com/rubocop-hq/rubocop/issues/8444): Fix an error for `Layout/FirstMethodArgumentLineBreak` when using kwargs in `super`. ([@koic][])
* [#8448](https://github.com/rubocop-hq/rubocop/pull/8448): Fix `Style/NestedParenthesizedCalls` to include line continuations in whitespace for auto-correct. ([@biinari][])

### Changes

Expand Down
22 changes: 18 additions & 4 deletions lib/rubocop/cop/mixin/range_help.rb
Expand Up @@ -46,7 +46,8 @@ def range_with_surrounding_comma(range, side = :both)
end

def range_with_surrounding_space(range:, side: :both,
newlines: true, whitespace: false)
newlines: true, whitespace: false,
continuations: false)
buffer = @processed_source.buffer
src = buffer.source

Expand All @@ -55,10 +56,13 @@ def range_with_surrounding_space(range:, side: :both,
begin_pos = range.begin_pos
if go_left
begin_pos =
final_pos(src, begin_pos, -1, newlines, whitespace)
final_pos(src, begin_pos, -1, continuations, newlines, whitespace)
end
end_pos = range.end_pos
end_pos = final_pos(src, end_pos, 1, newlines, whitespace) if go_right
if go_right
end_pos =
final_pos(src, end_pos, 1, continuations, newlines, whitespace)
end
Parser::Source::Range.new(buffer, begin_pos, end_pos)
end

Expand Down Expand Up @@ -101,17 +105,27 @@ def directions(side)
end
end

def final_pos(src, pos, increment, newlines, whitespace)
# rubocop:disable Metrics/ParameterLists
def final_pos(src, pos, increment, continuations, newlines, whitespace)
pos = move_pos(src, pos, increment, true, /[ \t]/)
pos = move_pos_str(src, pos, increment, continuations, "\\\n")
pos = move_pos(src, pos, increment, newlines, /\n/)
move_pos(src, pos, increment, whitespace, /\s/)
end
# rubocop:enable Metrics/ParameterLists

def move_pos(src, pos, step, condition, regexp)
offset = step == -1 ? -1 : 0
pos += step while condition && regexp.match?(src[pos + offset])
pos.negative? ? 0 : pos
end

def move_pos_str(src, pos, step, condition, needle)
size = needle.length
offset = step == -1 ? -size : 0
pos += size * step while condition && src[pos + offset, size] == needle
pos.negative? ? 0 : pos
end
end
end
end
4 changes: 3 additions & 1 deletion lib/rubocop/cop/style/nested_parenthesized_calls.rb
Expand Up @@ -37,7 +37,9 @@ def autocorrect(nested)

leading_space =
range_with_surrounding_space(range: first_arg.begin,
side: :left)
side: :left,
whitespace: true,
continuations: true)

lambda do |corrector|
corrector.replace(leading_space, '(')
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cop/style/nested_parenthesized_calls_spec.rb
Expand Up @@ -103,7 +103,7 @@

context 'backslash newline in method call' do
let(:source) do
<<~RUBY
<<~'RUBY'
puts(nex \
5)
RUBY
Expand Down

0 comments on commit 3065500

Please sign in to comment.