Skip to content

Commit 05bccb6

Browse files
luke-hillpirj
authored andcommitted
Exclude all spec blocks from Metrics/BlockLength
This was something that was originally discussed in #988
1 parent 2f46bf1 commit 05bccb6

File tree

5 files changed

+16
-3
lines changed

5 files changed

+16
-3
lines changed

.rubocop.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ Metrics/BlockLength:
5151
- rubocop-rspec.gemspec
5252
- Rakefile
5353
- '**/*.rake'
54-
- spec/**/*.rb
5554

5655
Naming/FileName:
5756
Exclude:

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Drop Ruby 2.5 support. ([@ydah][])
66
* Add new `RSpec/ChangeByZero` cop. ([@ydah][])
77
* Improve `RSpec/ExpectChange` to detect namespaced and top-level constants. ([@M-Yamashita01][])
8+
* Introduce an amendment to `Metrics/BlockLength` to exclude spec files. ([@luke-hill][])
89

910
## 2.10.0 (2022-04-19)
1011

@@ -686,3 +687,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
686687
[@ydah]: https://github.com/ydah
687688
[@t3h2mas]: https://github.com/t3h2mas
688689
[@M-Yamashita01]: https://github.com/M-Yamashita01
690+
[@luke-hill]: https://github.com/luke-hill

config/default.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ RSpec:
110110
- subject
111111
- subject!
112112

113+
Metrics/BlockLength:
114+
inherit_mode:
115+
merge:
116+
- Exclude
117+
Exclude:
118+
- "**/*_spec.rb"
119+
- "**/spec/**/*"
120+
113121
RSpec/AlignLeftLetBrace:
114122
Description: Checks that left braces for adjacent single line lets are aligned.
115123
Enabled: false

lib/rubocop/rspec/config_formatter.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module RSpec
88
class ConfigFormatter
99
EXTENSION_ROOT_DEPARTMENT = %r{^(RSpec/)}.freeze
1010
SUBDEPARTMENTS = %(RSpec/Capybara RSpec/FactoryBot RSpec/Rails)
11+
AMENDMENTS = %(Metrics/BlockLength)
1112
COP_DOC_BASE_URL = 'https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/'
1213

1314
def initialize(config, descriptions)
@@ -18,6 +19,7 @@ def initialize(config, descriptions)
1819
def dump
1920
YAML.dump(unified_config)
2021
.gsub(EXTENSION_ROOT_DEPARTMENT, "\n\\1")
22+
.gsub(*AMENDMENTS, "\n\\0")
2123
.gsub(/^(\s+)- /, '\1 - ')
2224
end
2325

@@ -26,6 +28,7 @@ def dump
2628
def unified_config
2729
cops.each_with_object(config.dup) do |cop, unified|
2830
next if SUBDEPARTMENTS.include?(cop)
31+
next if AMENDMENTS.include?(cop)
2932

3033
unified[cop].merge!(descriptions.fetch(cop))
3134
unified[cop]['Reference'] = COP_DOC_BASE_URL + cop.sub('RSpec/', '')

spec/project/default_config_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ def cop_configuration(config_key)
4242
end
4343
end
4444

45-
it 'has configuration for all cops' do
46-
expect(default_config.keys).to match_array(config_keys)
45+
it 'has configuration for all cops and amendments' do
46+
expect(default_config.keys)
47+
.to match_array([*config_keys, 'Metrics/BlockLength'])
4748
end
4849

4950
it 'sorts configuration keys alphabetically', :pending do

0 commit comments

Comments
 (0)