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 #8582] Add new Layout/BeginEndAlignment cop #8628

Merged
merged 1 commit into from Sep 6, 2020

Commits on Sep 6, 2020

  1. [Fix rubocop#8582] Add new Layout/BeginEndAlignment cop

    Fixes rubocop#8582.
    
    This PR adds `Layout/BeginEndAlignment` cop to solve the issue.
    
    The following is an example:
    
    ```ruby
    % cat example.rb
    foo = bar rescue "{}"
    ```
    
    ## Before
    
    The alignment was misaligned because `begin` was not awake.
    
    ```ruby
    % rubocop -a example.rb
    (snip)
    
    % cat example.rb
    foo =
      begin
             bar
      rescue StandardError
        '{}'
           end
    ```
    
    ## After
    
    Awakened by `begin`, so aligned.
    
    ```ruby
    % rubocop -a example.rb
    (snip)
    
    % cat example.rb
    foo =
      begin
        bar
      rescue StandardError
        '{}'
      end
    ```
    
    I was first planning to add `begin` processing to `Layout/EndAlignment` cop.
    However, `Layout/EndAlignment` cop with `EnforcedStyleAlignWith: keyword (default)`
    was not preferred code in the following:
    
    ```ruby
    longlonglonglonglonglonglonglonglong ||= begin
                                               do_something
                                             end
    ```
    
    This style has been found to have very long line length with RuboCop's own code.
    
    Therefore, since this PR would like to use `EnforcedStyleAlignWith: start_of_line`
    by default against `foo ||= begin`, this PR added the new cop.
    
    ```ruby
    # EnforcedStyleAlignWith: start_of_line (default)
    longlonglonglonglonglonglonglonglong ||= begin
      do_something
    end
    ```
    
    This solution allows configuration with minimal impact on existing code.
    
    It is configurable if user want to align to `begin`.
    
    ```ruby
    # EnforcedStyleAlignWith: begin
    longlonglonglonglonglonglonglonglong ||= begin
                                               do_something
                                             end
    ```
    
    This PR also makes `Layout/RescueEnsureAlignment` cop aware of new
    `Layout/BeginEndAlignment` cop's config to align `rescue` indents.
    koic committed Sep 6, 2020
    Configuration menu
    Copy the full SHA
    1b21b76 View commit details
    Browse the repository at this point in the history