From 9c2de81c498ae637aeb78796e403dc2dd8fc2699 Mon Sep 17 00:00:00 2001 From: Aleh Kanoika Date: Mon, 24 Sep 2018 08:15:11 +0300 Subject: [PATCH] Allow to exclude methods from `Metrics/MethodLength` (#6286) This makes it possible to exclude large methods about which it is known and such a dimension is permissible for them. --- CHANGELOG.md | 2 ++ config/default.yml | 1 + lib/rubocop/cop/metrics/method_length.rb | 3 ++ manual/cops_metrics.md | 1 + spec/rubocop/config_loader_spec.rb | 3 +- .../rubocop/cop/metrics/method_length_spec.rb | 31 +++++++++++++++++++ 6 files changed, 40 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f8438c93ef..f0b01444cf9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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][]) @@ -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 diff --git a/config/default.yml b/config/default.yml index 9a2b04290ad..be61caaa6a3 100644 --- a/config/default.yml +++ b/config/default.yml @@ -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? diff --git a/lib/rubocop/cop/metrics/method_length.rb b/lib/rubocop/cop/metrics/method_length.rb index 39601bae353..fb65cdb3d73 100644 --- a/lib/rubocop/cop/metrics/method_length.rb +++ b/lib/rubocop/cop/metrics/method_length.rb @@ -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 diff --git a/manual/cops_metrics.md b/manual/cops_metrics.md index 37e557f8631..7e269277f3f 100644 --- a/manual/cops_metrics.md +++ b/manual/cops_metrics.md @@ -147,6 +147,7 @@ Name | Default value | Configurable values --- | --- | --- CountComments | `false` | Boolean Max | `10` | Integer +ExcludedMethods | `[]` | Array ### References diff --git a/spec/rubocop/config_loader_spec.rb b/spec/rubocop/config_loader_spec.rb index 1bd3e30cbf1..5b0d9e265b1 100644 --- a/spec/rubocop/config_loader_spec.rb +++ b/spec/rubocop/config_loader_spec.rb @@ -467,7 +467,8 @@ class FilePath < Cop 'StyleGuide' => '#short-methods', 'Enabled' => true, 'CountComments' => false, - 'Max' => 5 + 'Max' => 5, + 'ExcludedMethods' => [] } ) expect do diff --git a/spec/rubocop/cop/metrics/method_length_spec.rb b/spec/rubocop/cop/metrics/method_length_spec.rb index 8cfb8c80751..03e24bc0d51 100644 --- a/spec/rubocop/cop/metrics/method_length_spec.rb +++ b/spec/rubocop/cop/metrics/method_length_spec.rb @@ -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