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

Conversation

koic
Copy link
Member

@koic koic commented Oct 1, 2020

Follow #8122.

This PR fixes the following error for Metrics/ClassLength when using or assignment (||=).

% 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:

Foo = Class.new do
^^^ Class has too many lines. [6/5]

After:

Foo = Class.new do
      ^^^^^^^^^^^^ Class has too many lines. [6/5]

I haven't added the fixing to the changelog because #8122 hasn't been released yet.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

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.
@@ -39,8 +39,13 @@ def on_class(node)
end

def on_casgn(node)
_scope, _name, block_node = *node
check_code_length(node) if block_node.class_definition?
if node.parent&.assignment?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right!
It's an interesting pattern, I wonder if it is common?
Maybe it should be abstracted (later) in rubocop-ast, so that for any "assignment" we can get the assignment block with one method call, wdyt?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I'm not sure yet, but I think we can investigate a similar case and consider abstracting it to RuboCop AST (later).

@koic koic merged commit f3af4e2 into rubocop:master Oct 2, 2020
@koic koic deleted the fix_an_error_for_layout_class_length branch October 2, 2020 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants