Navigation Menu

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

[Style/SoleNestedConditional] Syntax error when autocorrecting nested if conditions with a block #10881

Closed
tom-kuca opened this issue Aug 8, 2022 · 1 comment · Fixed by #10886
Assignees
Labels

Comments

@tom-kuca
Copy link

tom-kuca commented Aug 8, 2022

If there are nested if statesments, where the second if condition is a function with an arugment and a block, autocorrecting causes a syntax error. Given snippet test.rb

def foo
  if cond
    if validate :strict do
        "hello"
      end
    end
  end
end

and running

rubocop test.rb --force-default-config --only Style/SoleNestedConditional -a

there is a syntax error reported by ruby (see below). Prior to autocorrect, there was no syntax error.


Expected behavior

ruby test.rb do not report syntax error after autocorrect. Proposed solution is to add parenthesis around arguments.

def foo
  if cond && validate(:strict) do
        "hello"
      end
    end
end

Actual behavior

The source code is autocorrected to

def foo
  if cond && validate :strict do
        puts "hello"
      end
    end
end

ruby test.rb reports syntax error:

/tmp/test.rb:2: syntax error, unexpected symbol literal, expecting `do' or '{' or '('
  if cond && validate :strict do
/tmp/test.rb:5: syntax error, unexpected `end', expecting end-of-input
    end

RuboCop version

$ [bundle exec] rubocop -V
1.33.0 (using Parser 3.1.2.0, rubocop-ast 1.19.1, running on ruby 2.7.4 x86_64-linux)
@koic koic added the bug label Aug 8, 2022
@dvandersluis dvandersluis self-assigned this Aug 8, 2022
dvandersluis added a commit to dvandersluis/rubocop that referenced this issue Aug 9, 2022
…p `block` and `csend` nodes when necessary.
koic added a commit that referenced this issue Aug 9, 2022
[Fix #10881] Fix `Style/SoleNestedConditional` to properly wrap `block` and `csend` nodes when necessary
@tom-kuca
Copy link
Author

tom-kuca commented Aug 9, 2022

Thanks for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants