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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/rubocop/cop/metrics/class_length.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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).

block_node = node.parent.children[1]
else
_scope, _name, block_node = *node
end

check_code_length(block_node) if block_node.class_definition?
end

private
Expand Down
20 changes: 17 additions & 3 deletions spec/rubocop/cop/metrics/class_length_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class Test
it 'registers an offense' do
expect_offense(<<~RUBY)
Foo = Class.new do
^^^ Class has too many lines. [6/5]
^^^^^^^^^^^^ Class has too many lines. [6/5]
a = 1
a = 2
a = 3
Expand All @@ -190,7 +190,7 @@ class Test
it 'registers an offense' do
expect_offense(<<~RUBY)
Foo = ::Class.new do
^^^ Class has too many lines. [6/5]
^^^^^^^^^^^^^^ Class has too many lines. [6/5]
a = 1
a = 2
a = 3
Expand All @@ -206,7 +206,21 @@ class Test
it 'registers an offense' do
expect_offense(<<~RUBY)
Foo = Struct.new(:foo, :bar) do
^^^ Class has too many lines. [6/5]
^^^^^^^^^^^^^^^^^^^^^^^^^ Class has too many lines. [6/5]
a = 1
a = 2
a = 3
a = 4
a = 5
a = 6
end
RUBY
end

it 'registers an offense when inspecting or equals (`||=`) for consntant' do
expect_offense(<<~RUBY)
Foo ||= Struct.new(:foo, :bar) do
^^^^^^^^^^^^^^^^^^^^^^^^^ Class has too many lines. [6/5]
a = 1
a = 2
a = 3
Expand Down