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/NestedTernaryOperator autocorrection raises when ternaries have an if statement between them #12237

Closed
stevegeek opened this issue Oct 2, 2023 · 0 comments · Fixed by #12238
Labels

Comments

@stevegeek
Copy link

If a nested ternary has a conditional between it and it's parent ternary, the cop raises NoMethodError

For example

a ? (
  if b
    c ? 1 : 2
  end
) : 3

I have a fix ready, I will open a PR shortly


Expected behavior

The cop should autocorrect the example to

if a
  if b
    c ? 1 : 2
  end
else
  3
end

Actual behavior

The cop raises:

Failure/Error: replace_loc_and_whitespace(corrector, if_node.loc.question, "\n")
     
     NoMethodError:
       undefined method `question' for #<Parser::Source::Map::Condition:0x00007f3e94bb4430 @keyword=#<Parser::Source::Range (string) 8...10>, @end=#<Parser::Source::Range (string) 29...32>, @else=nil, @begin=nil, 
...

Steps to reproduce the problem

The example provided above will cause the issue to happen when running rubocop and autocorrect.

RuboCop version

$ rubocop -V
1.52.1 (using Parser 3.2.2.3, rubocop-ast 1.29.0, running on ruby 3.1.3) [arm64-darwin22]
@koic koic added the bug label Oct 2, 2023
koic added a commit to koic/rubocop that referenced this issue Oct 2, 2023
Fixes rubocop#12237.

This PR fixes an error for `Style/NestedTernaryOperator`
when a ternary operator has a nested ternary operator within an `if`.
stevegeek added a commit to stevegeek/rubocop that referenced this issue Oct 2, 2023
Fix autocorrection to correctly support conditionals between nested ternaries.
koic added a commit that referenced this issue Oct 2, 2023
…nary_operator

[Fix #12237] Fix an error for `Style/NestedTernaryOperator`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants