Skip to content

Commit

Permalink
[Fix #10559] Fix crash in code length calculation
Browse files Browse the repository at this point in the history
Closes #10559
  • Loading branch information
tejasbubane committed Apr 21, 2022
1 parent 16c6f74 commit 08e2021
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_crash_in_codelength.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#10559](https://github.com/rubocop/rubocop/issues/10559): Fix crash in code length calculation. ([@tejasbubane][])
7 changes: 6 additions & 1 deletion lib/rubocop/cop/metrics/utils/code_length_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def count_comments?

def omit_length(descendant)
parent = descendant.parent
return 0 if another_args?(parent)
return 0 if another_args?(parent) || pos_missing?(parent)

[
parent.loc.begin.end_pos != descendant.loc.expression.begin_pos,
Expand All @@ -167,6 +167,11 @@ def omit_length(descendant)
def another_args?(node)
node.call_type? && node.arguments.count > 1
end

def pos_missing?(node)
loc = node.loc
!(loc.begin && loc.end)
end
end
end
end
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 @@ -158,6 +158,17 @@ def test
expect(length).to eq(1)
end

it 'counts single line correctly if asked folding without parens' 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 'counts single line hash with line breaks correctly if asked folding' do
source = parse_source(<<~RUBY)
def test
Expand Down

0 comments on commit 08e2021

Please sign in to comment.