Skip to content

Commit

Permalink
[Fix #8872] Fix an error for Metrics/ClassLength
Browse files Browse the repository at this point in the history
Fixes #8872.

This PR fixes an error for `Metrics/ClassLength`
when multiple assignments to constants.
  • Loading branch information
koic authored and bbatsov committed Oct 9, 2020
1 parent f79e1c0 commit f488821
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@
* [#8864](https://github.com/rubocop-hq/rubocop/issues/8864): Fix false positive for `Style/RedundantBegin` with a postfix `while` or `until`. ([@dvandersluis][])
* [#8869](https://github.com/rubocop-hq/rubocop/issues/8869): Fix a false positive for `Style/RedundantBegin` when using `begin` for or assignment and method call. ([@koic][])
* [#8862](https://github.com/rubocop-hq/rubocop/issues/8862): Fix an error for `Lint/AmbiguousRegexpLiteral` when using regexp without method calls in nested structure. ([@koic][])
* [#8872](https://github.com/rubocop-hq/rubocop/issues/8872): Fix an error for `Metrics/ClassLength` when multiple assignments to constants. ([@koic][])

## 0.93.0 (2020-10-08)

Expand Down
8 changes: 6 additions & 2 deletions lib/rubocop/cop/metrics/class_length.rb
Expand Up @@ -39,8 +39,12 @@ def on_class(node)
end

def on_casgn(node)
if node.parent&.assignment?
block_node = node.parent.children[1]
parent = node.parent

if parent&.assignment?
block_node = parent.children[1]
elsif parent&.parent&.masgn_type?
block_node = parent.parent.children[1]
else
_scope, _name, block_node = *node
end
Expand Down
15 changes: 15 additions & 0 deletions spec/rubocop/cop/metrics/class_length_spec.rb
Expand Up @@ -230,5 +230,20 @@ class Test
end
RUBY
end

it 'registers an offense when multiple assignments to constants' do
# `Bar` is always nil, but syntax is valid.
expect_offense(<<~RUBY)
Foo, Bar = Struct.new(:foo, :bar) do
^^^^^^^^^^^^^^^^^^^^^^^^^ Class has too many lines. [6/5]
a = 1
a = 2
a = 3
a = 4
a = 5
a = 6
end
RUBY
end
end
end

0 comments on commit f488821

Please sign in to comment.