forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This cop checks there aren't too many lines in blocks, much like `Metric/MethodLength` checks for too many lines in methods. Also removed the splatted additional args from CodeLength#check_code_length, I don't think they've been necessary since the old `check` method was refactored into that method.
- Loading branch information
Showing
7 changed files
with
145 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Metrics | ||
# This cop checks if the length of a block exceeds some maximum value. | ||
# Comment lines can optionally be ignored. | ||
# The maximum allowed length is configurable. | ||
class BlockLength < Cop | ||
include CodeLength | ||
|
||
def on_block(node) | ||
check_code_length(node) | ||
end | ||
|
||
private | ||
|
||
def message(length, max_length) | ||
format('Block has too many lines. [%d/%d]', length, max_length) | ||
end | ||
|
||
def code_length(node) | ||
lines = node.source.lines.to_a[1..-2] || [] | ||
|
||
lines.count { |line| !irrelevant_line(line) } | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe RuboCop::Cop::Metrics::BlockLength, :config do | ||
subject(:cop) { described_class.new(config) } | ||
let(:cop_config) { { 'Max' => 2, 'CountComments' => false } } | ||
|
||
it 'rejects a block with more than 5 lines' do | ||
inspect_source(cop, ['something do', | ||
' a = 1', | ||
' a = 2', | ||
' a = 3', | ||
'end']) | ||
expect(cop.offenses.size).to eq(1) | ||
expect(cop.offenses.map(&:line).sort).to eq([1]) | ||
expect(cop.config_to_allow_offenses).to eq('Max' => 3) | ||
end | ||
|
||
it 'reports the correct beginning and end lines' do | ||
inspect_source(cop, ['something do', | ||
' a = 1', | ||
' a = 2', | ||
' a = 3', | ||
'end']) | ||
offense = cop.offenses.first | ||
expect(offense.location.first_line).to eq(1) | ||
expect(offense.location.last_line).to eq(5) | ||
end | ||
|
||
it 'accepts a block with less than 3 lines' do | ||
inspect_source(cop, ['something do', | ||
' a = 1', | ||
' a = 2', | ||
'end']) | ||
expect(cop.offenses).to be_empty | ||
end | ||
|
||
it 'does not count blank lines' do | ||
inspect_source(cop, ['something do', | ||
' a = 1', | ||
'', | ||
'', | ||
' a = 4', | ||
'end']) | ||
expect(cop.offenses).to be_empty | ||
end | ||
|
||
it 'accepts empty blocks' do | ||
inspect_source(cop, ['something do', | ||
'end']) | ||
expect(cop.offenses).to be_empty | ||
end | ||
|
||
it 'rejects brace blocks too' do | ||
inspect_source(cop, ['something {', | ||
' a = 1', | ||
' a = 2', | ||
' a = 3', | ||
'}']) | ||
expect(cop.offenses.size).to eq(1) | ||
end | ||
|
||
it 'properly counts nested blocks' do | ||
inspect_source(cop, ['something do', | ||
' something do', | ||
' a = 2', | ||
' a = 3', | ||
' a = 4', | ||
' a = 5', | ||
' end', | ||
'end']) | ||
expect(cop.offenses.size).to eq(2) | ||
expect(cop.offenses.map(&:line).sort).to eq([1, 2]) | ||
end | ||
|
||
it 'does not count commented lines by default' do | ||
inspect_source(cop, ['something do', | ||
' a = 1', | ||
' #a = 2', | ||
' #a = 3', | ||
' a = 4', | ||
'end']) | ||
expect(cop.offenses).to be_empty | ||
end | ||
|
||
context 'when CountComments is enabled' do | ||
before { cop_config['CountComments'] = true } | ||
|
||
it 'also counts commented lines' do | ||
inspect_source(cop, ['something do', | ||
' a = 1', | ||
' #a = 2', | ||
' a = 3', | ||
'end']) | ||
expect(cop.offenses.size).to eq(1) | ||
expect(cop.offenses.map(&:line).sort).to eq([1]) | ||
end | ||
end | ||
end |