Skip to content

Style/NestedTernaryOperator autocorrection raises when ternaries have an if statement between them #12237

@stevegeek

Description

@stevegeek

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]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy