Skip to content

Commit

Permalink
Add new CountAsOne option for code length related Metric cops
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Jul 6, 2020
1 parent c16faa7 commit e815b97
Show file tree
Hide file tree
Showing 18 changed files with 688 additions and 59 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@
* [#7458](https://github.com/rubocop-hq/rubocop/issues/7458): Add new `AsciiConstants` option for `Naming/AsciiIdentifiers`. ([@fatkodima][])
* [#7373](https://github.com/rubocop-hq/rubocop/issues/7373): Add new `Style/RedundantAssignment` cop. ([@fatkodima][])
* [#8213](https://github.com/rubocop-hq/rubocop/pull/8213): Permit to specify TargetRubyVersion 2.8 (experimental). ([@koic][])
* [#8159](https://github.com/rubocop-hq/rubocop/pull/8159): Add new `CountAsOne` option for code length related `Metric` cops. ([@fatkodima][])
* [#8164](https://github.com/rubocop-hq/rubocop/pull/8164): Support auto-correction for `Lint/InterpolationCheck`. ([@koic][])
* [#8223](https://github.com/rubocop-hq/rubocop/pull/8223): Support auto-correction for `Style/IfUnlessModifierOfIfUnless`. ([@koic][])
* [#8172](https://github.com/rubocop-hq/rubocop/pull/8172): Support auto-correction for `Lint/SafeNavigationWithEmpty`. ([@koic][])
Expand Down
10 changes: 8 additions & 2 deletions config/default.yml
Expand Up @@ -1911,9 +1911,10 @@ Metrics/BlockLength:
Description: 'Avoid long blocks with many lines.'
Enabled: true
VersionAdded: '0.44'
VersionChanged: '0.66'
VersionChanged: '0.87'
CountComments: false # count full line comments?
Max: 25
CountAsOne: []
ExcludedMethods:
# By default, exclude the `#refine` method, as it tends to have larger
# associated blocks.
Expand All @@ -1934,8 +1935,10 @@ Metrics/ClassLength:
Description: 'Avoid classes longer than 100 lines of code.'
Enabled: true
VersionAdded: '0.25'
VersionChanged: '0.87'
CountComments: false # count full line comments?
Max: 100
CountAsOne: []

# Avoid complex methods.
Metrics/CyclomaticComplexity:
Expand All @@ -1953,17 +1956,20 @@ Metrics/MethodLength:
StyleGuide: '#short-methods'
Enabled: true
VersionAdded: '0.25'
VersionChanged: '0.59.2'
VersionChanged: '0.87'
CountComments: false # count full line comments?
Max: 10
CountAsOne: []
ExcludedMethods: []

Metrics/ModuleLength:
Description: 'Avoid modules longer than 100 lines of code.'
Enabled: true
VersionAdded: '0.31'
VersionChanged: '0.87'
CountComments: false # count full line comments?
Max: 100
CountAsOne: []

Metrics/ParameterLists:
Description: 'Avoid parameter lists longer than three or four parameters.'
Expand Down
132 changes: 128 additions & 4 deletions docs/modules/ROOT/pages/cops_metrics.adoc
Expand Up @@ -45,14 +45,41 @@ and https://en.wikipedia.org/wiki/ABC_Software_Metric.
| Yes
| No
| 0.44
| 0.66
| 0.87
|===

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.
The cop can be configured to ignore blocks passed to certain methods.

You can set literals you want to fold with `CountAsOne`.
Available are: 'array', 'hash', and 'heredoc'. Each literal
will be counted as one line regardless of its actual size.

=== Examples

==== CountAsOne: ['array', 'heredoc']

[source,ruby]
----
something do
array = [ # +1
1,
2
]
hash = { # +3
key: 'value'
}
msg = <<~HEREDOC # +1
Heredoc
content.
HEREDOC
end # 5 points
----

=== Configurable attributes

|===
Expand All @@ -66,6 +93,10 @@ The cop can be configured to ignore blocks passed to certain methods.
| `25`
| Integer

| CountAsOne
| `[]`
| Array

| ExcludedMethods
| `refine`
| Array
Expand Down Expand Up @@ -123,13 +154,40 @@ The maximum level of nesting allowed is configurable.
| Yes
| No
| 0.25
| -
| 0.87
|===

This cop checks if the length a class exceeds some maximum value.
Comment lines can optionally be ignored.
The maximum allowed length is configurable.

You can set literals you want to fold with `CountAsOne`.
Available are: 'array', 'hash', and 'heredoc'. Each literal
will be counted as one line regardless of its actual size.

=== Examples

==== CountAsOne: ['array', 'heredoc']

[source,ruby]
----
class Foo
ARRAY = [ # +1
1,
2
]
HASH = { # +3
key: 'value'
}
MSG = <<~HEREDOC # +1
Heredoc
content.
HEREDOC
end # 5 points
----

=== Configurable attributes

|===
Expand All @@ -142,6 +200,10 @@ The maximum allowed length is configurable.
| Max
| `100`
| Integer

| CountAsOne
| `[]`
| Array
|===

== Metrics/CyclomaticComplexity
Expand Down Expand Up @@ -206,13 +268,40 @@ Blocks that are calls to builtin iteration methods
| Yes
| No
| 0.25
| 0.59.2
| 0.87
|===

This cop checks if the length of a method exceeds some maximum value.
Comment lines can optionally be ignored.
The maximum allowed length is configurable.

You can set literals you want to fold with `CountAsOne`.
Available are: 'array', 'hash', and 'heredoc'. Each literal
will be counted as one line regardless of its actual size.

=== Examples

==== CountAsOne: ['array', 'heredoc']

[source,ruby]
----
def m
array = [ # +1
1,
2
]
hash = { # +3
key: 'value'
}
<<~HEREDOC # +1
Heredoc
content.
HEREDOC
end # 5 points
----

=== Configurable attributes

|===
Expand All @@ -226,6 +315,10 @@ The maximum allowed length is configurable.
| `10`
| Integer

| CountAsOne
| `[]`
| Array

| ExcludedMethods
| `[]`
| Array
Expand All @@ -244,13 +337,40 @@ The maximum allowed length is configurable.
| Yes
| No
| 0.31
| -
| 0.87
|===

This cop checks if the length a module exceeds some maximum value.
Comment lines can optionally be ignored.
The maximum allowed length is configurable.

You can set literals you want to fold with `CountAsOne`.
Available are: 'array', 'hash', and 'heredoc'. Each literal
will be counted as one line regardless of its actual size.

=== Examples

==== CountAsOne: ['array', 'heredoc']

[source,ruby]
----
module M
ARRAY = [ # +1
1,
2
]
HASH = { # +3
key: 'value'
}
MSG = <<~HEREDOC # +1
Heredoc
content.
HEREDOC
end # 5 points
----

=== Configurable attributes

|===
Expand All @@ -263,6 +383,10 @@ The maximum allowed length is configurable.
| Max
| `100`
| Integer

| CountAsOne
| `[]`
| Array
|===

== Metrics/ParameterLists
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop.rb
Expand Up @@ -59,7 +59,6 @@
require_relative 'rubocop/cop/mixin/check_line_breakable'
require_relative 'rubocop/cop/mixin/configurable_max'
require_relative 'rubocop/cop/mixin/code_length' # relies on configurable_max
require_relative 'rubocop/cop/mixin/classish_length' # relies on code_length
require_relative 'rubocop/cop/mixin/configurable_enforced_style'
require_relative 'rubocop/cop/mixin/configurable_formatting'
require_relative 'rubocop/cop/mixin/configurable_naming'
Expand Down Expand Up @@ -329,6 +328,7 @@
require_relative 'rubocop/cop/metrics/cyclomatic_complexity'
# relies on cyclomatic_complexity
require_relative 'rubocop/cop/metrics/utils/abc_size_calculator'
require_relative 'rubocop/cop/metrics/utils/code_length_calculator'
require_relative 'rubocop/cop/metrics/abc_size'
require_relative 'rubocop/cop/metrics/block_length'
require_relative 'rubocop/cop/metrics/block_nesting'
Expand Down
22 changes: 22 additions & 0 deletions lib/rubocop/cop/metrics/block_length.rb
Expand Up @@ -7,6 +7,28 @@ module Metrics
# Comment lines can optionally be ignored.
# The maximum allowed length is configurable.
# The cop can be configured to ignore blocks passed to certain methods.
#
# You can set literals you want to fold with `CountAsOne`.
# Available are: 'array', 'hash', and 'heredoc'. Each literal
# will be counted as one line regardless of its actual size.
#
# @example CountAsOne: ['array', 'heredoc']
#
# something do
# array = [ # +1
# 1,
# 2
# ]
#
# hash = { # +3
# key: 'value'
# }
#
# msg = <<~HEREDOC # +1
# Heredoc
# content.
# HEREDOC
# end # 5 points
class BlockLength < Cop
include TooManyLines

Expand Down
25 changes: 24 additions & 1 deletion lib/rubocop/cop/metrics/class_length.rb
Expand Up @@ -6,8 +6,31 @@ module Metrics
# This cop checks if the length a class exceeds some maximum value.
# Comment lines can optionally be ignored.
# The maximum allowed length is configurable.
#
# You can set literals you want to fold with `CountAsOne`.
# Available are: 'array', 'hash', and 'heredoc'. Each literal
# will be counted as one line regardless of its actual size.
#
# @example CountAsOne: ['array', 'heredoc']
#
# class Foo
# ARRAY = [ # +1
# 1,
# 2
# ]
#
# HASH = { # +3
# key: 'value'
# }
#
# MSG = <<~HEREDOC # +1
# Heredoc
# content.
# HEREDOC
# end # 5 points
#
class ClassLength < Cop
include ClassishLength
include TooManyLines

def on_class(node)
check_code_length(node)
Expand Down
23 changes: 23 additions & 0 deletions lib/rubocop/cop/metrics/method_length.rb
Expand Up @@ -6,6 +6,29 @@ module Metrics
# This cop checks if the length of a method exceeds some maximum value.
# Comment lines can optionally be ignored.
# The maximum allowed length is configurable.
#
# You can set literals you want to fold with `CountAsOne`.
# Available are: 'array', 'hash', and 'heredoc'. Each literal
# will be counted as one line regardless of its actual size.
#
# @example CountAsOne: ['array', 'heredoc']
#
# def m
# array = [ # +1
# 1,
# 2
# ]
#
# hash = { # +3
# key: 'value'
# }
#
# <<~HEREDOC # +1
# Heredoc
# content.
# HEREDOC
# end # 5 points
#
class MethodLength < Cop
include TooManyLines

Expand Down

0 comments on commit e815b97

Please sign in to comment.