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

Layout/EmptyLineAfterGuardClause inserts empty line outside of block, breaking method chaining #9977

Closed
afn opened this issue Aug 3, 2021 · 0 comments · Fixed by #9978
Closed
Assignees
Labels

Comments

@afn
Copy link

afn commented Aug 3, 2021

The Layout/EmptyLineAfterGuardClause cop misbehaves in a situation such as this (very contrived) example:

# frozen_string_literal: true

def weird_sum(my_array)
  my_array
    .map { |item| next unless item.positive?; item * 2 }
    .compact
    .sum
end

Expected behavior

No empty line inserted, or an empty line is inserted inside the block.

Actual behavior

An empty line is inserted before the .compact line, resulting in a syntax error.

$ rubocop --debug --auto-correct test.rb
For /home/afn/work: Default configuration from /home/afn/.rvm/gems/ruby-3.0.1/gems/rubocop-1.18.4/config/default.yml
Inspecting 1 file
Scanning /home/afn/work/test.rb
E

Offenses:

test.rb:5:19: C: [Corrected] Layout/EmptyLineAfterGuardClause: Add empty line after guard clause.
    .map { |item| next unless item.positive?; item * 2 }
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^
test.rb:7:5: E: Lint/Syntax: unexpected token tDOT
(Using Ruby 3.0 parser; configure using TargetRubyVersion parameter, under AllCops)
    .compact
    ^

1 file inspected, 2 offenses detected, 1 offense corrected
Finished in 0.11396609200164676 seconds

Resulting file:

# frozen_string_literal: true

def weird_sum(my_array)
  my_array
    .map { |item| next unless item.positive?; item * 2 }

    .compact
    .sum
end

Steps to reproduce the problem

See above.

RuboCop version

1.18.4 (using Parser 3.0.2.0, rubocop-ast 1.8.0, running on ruby 3.0.1 x86_64-linux)

@dvandersluis dvandersluis self-assigned this Aug 3, 2021
dvandersluis added a commit to dvandersluis/rubocop that referenced this issue Aug 5, 2021
…egister an offense if there is another expression following the guard clause on the same line.
@mergify mergify bot closed this as completed in #9978 Aug 5, 2021
mergify bot pushed a commit that referenced this issue Aug 5, 2021
… an offense if there is another expression following the guard clause on the same line.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants