Skip to content

Commit

Permalink
Allow to exclude methods from Metrics/MethodLength (#6286)
Browse files Browse the repository at this point in the history
This makes it possible to exclude large methods about which it is known and such a dimension is permissible for them.
  • Loading branch information
akanoi authored and bbatsov committed Sep 24, 2018
1 parent 841cf1c commit 9c2de81
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -20,6 +20,7 @@

### Bug fixes

* [#6286](https://github.com/rubocop-hq/rubocop/pull/6286): Allow exclusion of certain methods for `Metrics/MethodLength`. ([@akanoi][])
* [#6267](https://github.com/rubocop-hq/rubocop/pull/6267): Fix undefined method 'method_name' for `Rails/FindEach`. ([@Knack][])
* [#6278](https://github.com/rubocop-hq/rubocop/pull/6278): Fix false positive for `Naming/FileName` when investigating gemspecs. ([@kddeisz][])
* [#6256](https://github.com/rubocop-hq/rubocop/pull/6256): Fix false positive for `Naming/FileName` when investigating dotfiles. ([@sinsoku][])
Expand Down Expand Up @@ -3585,6 +3586,7 @@
[@schneems]: https://github.com/schneems
[@ShockwaveNN]: https://github.com/ShockwaveNN
[@Knack]: https://github.com/Knack
[@akanoi]: https://github.com/akanoi
[@yensaki]: https://github.com/yensaki
[@ryanhageman]: https://github.com/ryanhageman
[@autopp]: https://github.com/autopp
Expand Down
1 change: 1 addition & 0 deletions config/default.yml
Expand Up @@ -1597,6 +1597,7 @@ Metrics/LineLength:
Metrics/MethodLength:
CountComments: false # count full line comments?
Max: 10
ExcludedMethods: []

Metrics/ModuleLength:
CountComments: false # count full line comments?
Expand Down
3 changes: 3 additions & 0 deletions lib/rubocop/cop/metrics/method_length.rb
Expand Up @@ -12,6 +12,9 @@ class MethodLength < Cop
LABEL = 'Method'.freeze

def on_def(node)
excluded_methods = cop_config['ExcludedMethods']
return if excluded_methods.include?(String(node.method_name))

check_code_length(node)
end
alias on_defs on_def
Expand Down
1 change: 1 addition & 0 deletions manual/cops_metrics.md
Expand Up @@ -147,6 +147,7 @@ Name | Default value | Configurable values
--- | --- | ---
CountComments | `false` | Boolean
Max | `10` | Integer
ExcludedMethods | `[]` | Array

### References

Expand Down
3 changes: 2 additions & 1 deletion spec/rubocop/config_loader_spec.rb
Expand Up @@ -467,7 +467,8 @@ class FilePath < Cop
'StyleGuide' => '#short-methods',
'Enabled' => true,
'CountComments' => false,
'Max' => 5
'Max' => 5,
'ExcludedMethods' => []
}
)
expect do
Expand Down
31 changes: 31 additions & 0 deletions spec/rubocop/cop/metrics/method_length_spec.rb
Expand Up @@ -189,4 +189,35 @@ def m
RUBY
end
end

context 'when method is defined in `ExcludedMethods`' do
before { cop_config['ExcludedMethods'] = ['foo'] }

it 'still rejects other methods with more than 5 lines' do
expect_offense(<<-RUBY.strip_indent)
def m
^^^^^^ Method has too many lines. [6/5]
a = 1
a = 2
a = 3
a = 4
a = 5
a = 6
end
RUBY
end

it 'accepts the foo method with more than 5 lines' do
expect_no_offenses(<<-RUBY.strip_indent)
def foo
a = 1
a = 2
a = 3
a = 4
a = 5
a = 6
end
RUBY
end
end
end

0 comments on commit 9c2de81

Please sign in to comment.