Skip to content

Commit

Permalink
Merge pull request #10520 from nobuyo/fix-code-length-calculator-bug
Browse files Browse the repository at this point in the history
[Fix #10469] Fix code length calculation when kwargs written in single line
  • Loading branch information
koic committed Apr 14, 2022
2 parents 709c452 + e0686d4 commit a4821e2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#10469](https://github.com/rubocop/rubocop/issues/10469): Fix code length calculation when kwargs written in single line. ([@nobuyo][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/metrics/utils/code_length_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def calculate
descendant_length = code_length(descendant)
length = length - descendant_length + 1
# Subtract 2 length of opening and closing brace if method argument omits hash braces.
length -= 2 if descendant.hash_type? && !descendant.braces?
length -= 2 if descendant.hash_type? && !descendant.braces? && descendant.multiline?
end

length
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/metrics/utils/code_length_calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ def test
expect(length).to eq(2)
end

it 'counts single line correctly if asked folding' do
source = parse_source(<<~RUBY)
def test
foo(foo: :bar, baz: :quux)
end
RUBY

length = described_class.new(source.ast, source, foldable_types: %i[hash]).calculate
expect(length).to eq(1)
end

it 'folds hashes as method kwargs if asked' do
source = parse_source(<<~RUBY)
def test
Expand Down

0 comments on commit a4821e2

Please sign in to comment.