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 an incorrect auto-correct for Style/MultilineTernaryOperator #8661

Commits on Sep 13, 2020

  1. Fix an incorrect auto-correct for Style/MultilineTernaryOperator

    This PR fixes the following incorrect auto-correct for
    `Style/MultilineTernaryOperator` when returning
    a multiline ternary operator expression.
    
    ```console
    % cat example.rb
    return distance_in_minutes == 0 ?
           locale.t(:less_than_x_minutes, count: 1) :
           locale.t(:x_minutes, count: distance_in_minutes)
    
    % bundle exec rubocop -a --only Style/MultilineTernaryOperator
    (snip)
    
    Inspecting 2 files
    C.
    
    Offenses:
    
    example.rb:1:8: C: [Corrected] Style/MultilineTernaryOperator: Avoid
    multi-line ternary operators, use if or unless instead.
    return distance_in_minutes == 0 ? ...
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
    
    2 files inspected, 1 offense detected, 1 offense corrected
    
    % cat example.rb
    return if distance_in_minutes == 0
      locale.t(:less_than_x_minutes, count: 1)
    else
      locale.t(:x_minutes, count: distance_in_minutes)
    end
    
    % ruby -c example.rb
    example.rb:3: syntax error, unexpected `else', expecting end-of-input
    ```
    
    I found it with the following code.
    https://github.com/rails/rails/blob/v6.0.3.2/actionview/lib/action_view/helpers/date_helper.rb#L109-L111
    koic committed Sep 13, 2020
    Copy the full SHA
    8a1776f View commit details
    Browse the repository at this point in the history