Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix line continuations in spec examples #8448

Merged
merged 2 commits into from
Aug 6, 2020

Conversation

biinari
Copy link
Contributor

@biinari biinari commented Aug 4, 2020

When describing the code that should be detected / auto-corrected, some specs do not escape line continuations properly.

For example:

<<~RUBY
  something \
    continues
RUBY

results in the string "something continues\n" instead of the desired "something \\\n continues\n"

Correct specs escape the backslash or use single quoted heredocs to keep the line continuation in the source under test, like:

# single quoted heredoc (looks more like the source would)
<<~'RUBY'
  something \
    continues
RUBY

# or escape the backslash (allows interpolation and other escapes where needed)
<<~RUBY
  something \\
    continues
RUBY

In correcting the specs, it became apparent that Style/NestedParenthesizedCalls cop did not behave as intended. RangeHelp mixin has been adapted to optionally include line continuations (backslash newline sequence).

Feedback is welcome on whether this behaviour on replacing whitespace with parentheses matches with your expectations.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

Fix backslash before newline.
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"
```
@biinari biinari force-pushed the fix/spec_line_continuations branch from 6b1e23e to b67bf30 Compare August 4, 2020 05:14
@biinari biinari marked this pull request as ready for review August 4, 2020 05:14
@@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine this method is not meant to be used directly? I really need to finish #8278 ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, there's a comment above that says these helpers are not supposed to be used directly. I agree it would be better to mark them individually as @api private.

@bbatsov bbatsov merged commit 3065500 into rubocop:master Aug 6, 2020
@bbatsov
Copy link
Collaborator

bbatsov commented Aug 6, 2020

Nice catch! Thanks!

@biinari biinari deleted the fix/spec_line_continuations branch August 6, 2020 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants