Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information