Skip to content

Commit

Permalink
[Fix rubocop#3578] Fix safe navigation counting in Metrics/AbcSize
Browse files Browse the repository at this point in the history
This cop wasn't correctly counting method calls that happen with the safe navigation operator (`x&.y`), it has been changed so that they are treated the same as other method calls (like `x.y`).

Adds a test which checks the score of a method with the safe navigation operator. The test would fail without the change in the cop.
  • Loading branch information
savef committed Oct 7, 2016
1 parent 024bb49 commit 5008ed6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* [#3334](https://github.com/bbatsov/rubocop/issues/3334): Do not register an offense for a literal space (`\s`) in `Style/UnneededCapitalW`. ([@rrosenblum][])
* [#3390](https://github.com/bbatsov/rubocop/issues/3390): Fix SaveBang cop for multiple conditional. ([@tejasbubane][])
* [#3577](https://github.com/bbatsov/rubocop/issues/3577): Fix `Style/RaiseArgs` not allowing compact raise with splatted args. ([@savef][])
* [#3578](https://github.com/bbatsov/rubocop/issues/3578): Fix safe navigation method call counting in `Metrics/AbcSize`. ([@savef][])

### Changes

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/metrics/abc_size.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AbcSize < Cop

MSG = 'Assignment Branch Condition size for %s is too high. ' \
'[%.4g/%.4g]'.freeze
BRANCH_NODES = [:send].freeze
BRANCH_NODES = [:send, :csend].freeze
CONDITION_NODES = CyclomaticComplexity::COUNTED_NODES.freeze

private
Expand Down
14 changes: 13 additions & 1 deletion spec/rubocop/cop/metrics/abc_size_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,19 @@
'end'])
expect(cop.messages)
.to eq(['Assignment Branch Condition size for method_name is too ' \
'high. [6.4/0]']) # square root of 1*1 + 5*5 + 2*2 => 6.4
'high. [6.4/0]']) # sqrt(1*1 + 5*5 + 2*2) => 6.4
end

context 'target_ruby_version >= 2.3', :ruby23 do
it 'treats safe navigation method calls like regular method calls' do
inspect_source(cop,
['def method_name',
' object&.do_something',
'end'])
expect(cop.messages)
.to eq(['Assignment Branch Condition size for method_name is too ' \
'high. [2/0]']) # sqrt(0 + 2*2 + 0) => 2
end
end
end

Expand Down

0 comments on commit 5008ed6

Please sign in to comment.