Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #12873] Fix an error for Metrics/BlockLength #12874

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_metrics_block_length.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12873](https://github.com/rubocop/rubocop/issues/12873): Fix an error for `Metrics/BlockLength` when the `CountAsOne` config is invalid. ([@koic][])
4 changes: 2 additions & 2 deletions lib/rubocop/cop/metrics/utils/code_length_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def build_foldable_checks(types) # rubocop:disable Metrics/MethodLength
when :method_call
->(node) { node.call_type? }
else
raise ArgumentError, "Unknown foldable type: #{type.inspect}. " \
"Valid foldable types are: #{FOLDABLE_TYPES.join(', ')}."
raise Warning, "Unknown foldable type: #{type.inspect}. " \
"Valid foldable types are: #{FOLDABLE_TYPES.join(', ')}."
end
end
end
Expand Down
19 changes: 19 additions & 0 deletions spec/rubocop/cop/metrics/block_length_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,25 @@
RUBY
end

context 'when the `CountAsOne` config is invalid' do
before do
cop_config['CountAsOne'] = 'config'
end

let(:source) { <<~RUBY }
something do
^^^^^^^^^^^^ Block has too many lines. [3/2]
a = 1
a = 2
a = 3
end
RUBY

it 'raises `RuboCop::Warning`' do
expect { expect_offense(source) }.to raise_error(RuboCop::Warning)
end
end

context 'when using numbered parameter', :ruby27 do
it 'rejects a block with more than 5 lines' do
expect_offense(<<~RUBY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ def test

expect do
described_class.new(source.ast, source, foldable_types: %i[unknown]).calculate
end.to raise_error(ArgumentError, /Unknown foldable type/)
end.to raise_error(RuboCop::Warning, /Unknown foldable type/)
end
end
end