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 error for Metrics/ClassLength #8824

Merged
merged 1 commit into from
Oct 2, 2020

Commits on Oct 1, 2020

  1. Fix an error for Metrics/ClassLength

    Follow rubocop#8122.
    
    This PR fixes the following error for `Metrics/ClassLength`
    when using or assignment (`||=`).
    
    ```console
    % cat example.rb
    Constant ||= Struct.new {}
    
    % bundle exec rubocop --only Layout/ClassLength -d example.rb
    (snip)
    
    Inspecting 1 file
    Scanning /Users/koic/src/github.com/koic/rubocop-issues/8122/example.rb
    An error occurred while Metrics/ClassLength cop was inspecting
    /Users/koic/src/github.com/koic/rubocop-issues/8122/example.rb:1:0.
    undefined method `class_definition?' for nil:NilClass
    /Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/metrics/class_length.rb:43:in
    `on_casgn'
    /Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/commissioner.rb:99:in
    `block (2 levels) in trigger_responding_cops'
    /Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/commissioner.rb:152:in
    `with_cop_error_handling'
    /Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/commissioner.rb:98:in
    `block in trigger_responding_cops'
    /Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/commissioner.rb:97:in
    `each'
    /Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/commissioner.rb:97:in
    `trigger_responding_cops'
    /Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/commissioner.rb:70:in
    `on_casgn'
    /Users/koic/src/github.com/rubocop-hq/rubocop-ast/lib/rubocop/ast/traversal.rb:60:in
    `block in on_or_asgn'
    /Users/koic/src/github.com/rubocop-hq/rubocop-ast/lib/rubocop/ast/traversal.rb:60:in
    `each'
    /Users/koic/src/github.com/rubocop-hq/rubocop-ast/lib/rubocop/ast/traversal.rb:60:in
    `on_or_asgn'
    /Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/commissioner.rb:72:in
    `on_or_asgn'
    /Users/koic/src/github.com/rubocop-hq/rubocop-ast/lib/rubocop/ast/traversal.rb:14:in
    `walk'
    /Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/commissioner.rb:85:in
    `investigate'
    ```
    
    This PR have also tweaked the highlight area of offense to more eligible
    location for count the class block length.
    
    Before:
    
    ```ruby
    Foo = Class.new do
    ^^^ Class has too many lines. [6/5]
    ```
    
    After:
    
    ```ruby
    Foo = Class.new do
          ^^^^^^^^^^^^ Class has too many lines. [6/5]
    ```
    
    I haven't added the fixing to the changelog because rubocop#8122 hasn't been released yet.
    koic committed Oct 1, 2020
    Configuration menu
    Copy the full SHA
    109ac27 View commit details
    Browse the repository at this point in the history