Skip to content

Commit

Permalink
[Fix rubocop#10559] Fix crash on CodeLengthCalculator if method call …
Browse files Browse the repository at this point in the history
…is not parenthesized
  • Loading branch information
nobuyo committed Apr 21, 2022
1 parent 7fcb272 commit 8caa98b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/fix_crash_on_codelengthcalculator_if.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#10559](https://github.com/rubocop/rubocop/issues/10559): Fix crash on CodeLengthCalculator if method call is not parenthesized. ([@nobuyo][])
5 changes: 5 additions & 0 deletions lib/rubocop/cop/metrics/utils/code_length_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,18 @@ def count_comments?
def omit_length(descendant)
parent = descendant.parent
return 0 if another_args?(parent)
return 0 unless parenthesized?(parent)

[
parent.loc.begin.end_pos != descendant.loc.expression.begin_pos,
parent.loc.end.begin_pos != descendant.loc.expression.end_pos
].count(true)
end

def parenthesized?(node)
node.call_type? && node.parenthesized?
end

def another_args?(node)
node.call_type? && node.arguments.count > 1
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 without parentheses 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 'counts single line hash with line breaks correctly if asked folding' do
source = parse_source(<<~RUBY)
def test
Expand Down

0 comments on commit 8caa98b

Please sign in to comment.