Skip to content

Commit 4920879

Browse files
committed
Remove relevance detection code
We have had problems with it: - it was slow - it is a non-standard extension of RuboCop's config - Include option exists - department-specific (e.g. FactoryBot) config was ignored
1 parent d0588f2 commit 4920879

File tree

14 files changed

+111
-107
lines changed

14 files changed

+111
-107
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* Remove deprecated class `::RuboCop::Cop::RSpec::Cop`. ([@bquorning][])
66
* Retire `RSpec/InvalidPredicateMatcher` cop. ([@pirj][])
7+
* Remove the code responsible for filtering files to inspect. ([@pirj][])
78

89
## 2.0.0.pre (2020-10-22)
910

config/default.yml

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
---
2-
AllCops:
3-
RSpec:
4-
Patterns:
5-
- _spec.rb
6-
- "(?:^|/)spec/"
7-
RSpec/FactoryBot:
8-
Patterns:
9-
- spec/factories.rb
10-
- spec/factories/**/*.rb
11-
- features/support/factories/**/*.rb
2+
RSpec:
3+
Include:
4+
- "**/*_spec.rb"
5+
- "**/spec/**/*"
126

137
RSpec/AlignLeftLetBrace:
148
Description: Checks that left braces for adjacent single line lets are aligned.
@@ -241,6 +235,9 @@ RSpec/ExpectOutput:
241235
RSpec/FilePath:
242236
Description: Checks that spec file paths are consistent and well-formed.
243237
Enabled: true
238+
Include:
239+
- "**/*_spec*rb*"
240+
- "**/spec/**/*"
244241
CustomTransform:
245242
RuboCop: rubocop
246243
RSpec: rspec
@@ -639,13 +636,23 @@ RSpec/Capybara/VisibilityMatcher:
639636
RSpec/FactoryBot/AttributeDefinedStatically:
640637
Description: Always declare attribute values as blocks.
641638
Enabled: true
639+
Include:
640+
- spec/factories.rb
641+
- spec/factories/**/*.rb
642+
- features/support/factories/**/*.rb
642643
VersionAdded: '1.28'
643644
VersionChanged: '2.0'
644645
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/FactoryBot/AttributeDefinedStatically
645646

646647
RSpec/FactoryBot/CreateList:
647648
Description: Checks for create_list usage.
648649
Enabled: true
650+
Include:
651+
- "**/*_spec.rb"
652+
- "**/spec/**/*"
653+
- spec/factories.rb
654+
- spec/factories/**/*.rb
655+
- features/support/factories/**/*.rb
649656
EnforcedStyle: create_list
650657
SupportedStyles:
651658
- create_list
@@ -657,6 +664,10 @@ RSpec/FactoryBot/CreateList:
657664
RSpec/FactoryBot/FactoryClassName:
658665
Description: Use string value when setting the class attribute explicitly.
659666
Enabled: true
667+
Include:
668+
- spec/factories.rb
669+
- spec/factories/**/*.rb
670+
- features/support/factories/**/*.rb
660671
VersionAdded: '1.37'
661672
VersionChanged: '2.0'
662673
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/FactoryBot/FactoryClassName

docs/modules/ROOT/pages/cops_rspec.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,6 +1536,10 @@ my_class_spec.rb # describe MyClass, '#method'
15361536
|===
15371537
| Name | Default value | Configurable values
15381538

1539+
| Include
1540+
| `+**/*_spec*rb*+`, `+**/spec/**/*+`
1541+
| Array
1542+
15391543
| CustomTransform
15401544
| `{"RuboCop"=>"rubocop", "RSpec"=>"rspec"}`
15411545
|

docs/modules/ROOT/pages/cops_rspec/factorybot.adoc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ count 1
3737
count { 1 }
3838
----
3939

40+
=== Configurable attributes
41+
42+
|===
43+
| Name | Default value | Configurable values
44+
45+
| Include
46+
| `spec/factories.rb`, `spec/factories/**/*.rb`, `features/support/factories/**/*.rb`
47+
| Array
48+
|===
49+
4050
=== References
4151

4252
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/FactoryBot/AttributeDefinedStatically
@@ -89,6 +99,10 @@ create_list :user, 3
8999
|===
90100
| Name | Default value | Configurable values
91101

102+
| Include
103+
| `+**/*_spec.rb+`, `+**/spec/**/*+`, `spec/factories.rb`, `spec/factories/**/*.rb`, `features/support/factories/**/*.rb`
104+
| Array
105+
92106
| EnforcedStyle
93107
| `create_list`
94108
| `create_list`, `n_times`
@@ -130,6 +144,16 @@ factory :foo, class: 'Foo' do
130144
end
131145
----
132146

147+
=== Configurable attributes
148+
149+
|===
150+
| Name | Default value | Configurable values
151+
152+
| Include
153+
| `spec/factories.rb`, `spec/factories/**/*.rb`, `features/support/factories/**/*.rb`
154+
| Array
155+
|===
156+
133157
=== References
134158

135159
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/FactoryBot/FactoryClassName

docs/modules/ROOT/pages/usage.adoc

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,20 @@ end
3636

3737
== Inspecting files that don't end with `_spec.rb`
3838

39-
By default, `rubocop-rspec` only inspects code within paths ending in `_spec.rb` or including `spec/`. You can override this setting in your config file by specifying one or more patterns:
39+
By default, `rubocop-rspec` only inspects code within paths ending in `_spec.rb` or including `spec/`. You can override this setting in your config file by setting `Include`:
4040

4141
[source,yaml]
4242
----
43-
# Inspect all files
44-
AllCops:
45-
RSpec:
46-
Patterns:
47-
- '.+'
43+
# Inspect files in `test/` directory
44+
RSpec:
45+
Include:
46+
- '**/test/**/*'
4847
----
4948

50-
5149
[source,yaml]
5250
----
5351
# Inspect only files ending with `_test.rb`
54-
AllCops:
55-
RSpec:
56-
Patterns:
57-
- '_test.rb$'
52+
RSpec:
53+
Include:
54+
- '**/*_test.rb'
5855
----

lib/rubocop/cop/rspec/base.rb

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,72 +4,16 @@ module RuboCop
44
module Cop
55
module RSpec
66
# @abstract parent class to RSpec cops
7-
#
8-
# The criteria for whether rubocop-rspec analyzes a certain ruby file
9-
# is configured via `AllCops/RSpec`. For example, if you want to
10-
# customize your project to scan all files within a `test/` directory
11-
# then you could add this to your configuration:
12-
#
13-
# @example configuring analyzed paths
14-
# # .rubocop.yml
15-
# # AllCops:
16-
# # RSpec:
17-
# # Patterns:
18-
# # - '_test.rb$'
19-
# # - '(?:^|/)test/'
207
class Base < ::RuboCop::Cop::Base
218
include RuboCop::RSpec::Language
229
include RuboCop::RSpec::Language::NodePattern
2310

24-
DEFAULT_CONFIGURATION =
25-
RuboCop::RSpec::CONFIG.fetch('AllCops').fetch('RSpec')
26-
27-
DEFAULT_PATTERN_RE = Regexp.union(
28-
DEFAULT_CONFIGURATION.fetch('Patterns')
29-
.map(&Regexp.public_method(:new))
30-
)
31-
3211
exclude_from_registry
3312

3413
# Invoke the original inherited hook so our cops are recognized
3514
def self.inherited(subclass) # rubocop:disable Lint/MissingSuper
3615
RuboCop::Cop::Base.inherited(subclass)
3716
end
38-
39-
def relevant_file?(file)
40-
relevant_rubocop_rspec_file?(file) && super
41-
end
42-
43-
private
44-
45-
def relevant_rubocop_rspec_file?(file)
46-
rspec_pattern.match?(file)
47-
end
48-
49-
def rspec_pattern
50-
if rspec_pattern_config?
51-
Regexp.union(rspec_pattern_config.map(&Regexp.public_method(:new)))
52-
else
53-
DEFAULT_PATTERN_RE
54-
end
55-
end
56-
57-
def all_cops_config
58-
config
59-
.for_all_cops
60-
end
61-
62-
def rspec_pattern_config?
63-
return unless all_cops_config.key?('RSpec')
64-
65-
all_cops_config.fetch('RSpec').key?('Patterns')
66-
end
67-
68-
def rspec_pattern_config
69-
all_cops_config
70-
.fetch('RSpec', DEFAULT_CONFIGURATION)
71-
.fetch('Patterns')
72-
end
7317
end
7418
end
7519
end

lib/rubocop/rspec/config_formatter.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module RuboCop
66
module RSpec
77
# Builds a YAML config file from two config hashes
88
class ConfigFormatter
9-
NAMESPACES = /^(RSpec|Capybara|FactoryBot|Rails)/.freeze
9+
EXTENSION_ROOT_DEPARTMENT = %r{^(RSpec/)}.freeze
1010
STYLE_GUIDE_BASE_URL = 'https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/'
1111

1212
def initialize(config, descriptions)
@@ -15,7 +15,7 @@ def initialize(config, descriptions)
1515
end
1616

1717
def dump
18-
YAML.dump(unified_config).gsub(NAMESPACES, "\n\\1")
18+
YAML.dump(unified_config).gsub(EXTENSION_ROOT_DEPARTMENT, "\n\\1")
1919
end
2020

2121
private
@@ -29,7 +29,7 @@ def unified_config
2929
end
3030

3131
def cops
32-
(descriptions.keys | config.keys).grep(NAMESPACES)
32+
(descriptions.keys | config.keys).grep(EXTENSION_ROOT_DEPARTMENT)
3333
end
3434

3535
attr_reader :config, :descriptions

spec/project/default_config_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
end
3030

3131
let(:config_keys) do
32-
cop_names + %w[AllCops]
32+
cop_names + %w[RSpec]
3333
end
3434

3535
def cop_configuration(config_key)

spec/rubocop/cop/rspec/base_spec.rb

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,8 @@
11
# frozen_string_literal: true
22

33
RSpec.describe RuboCop::Cop::RSpec::Base do
4-
subject(:cop) { RuboCop::RSpec::FakeCop.new(config) }
5-
6-
let(:config) do
7-
rubocop_config =
8-
{
9-
'AllCops' => {
10-
'RSpec' => {
11-
'Patterns' => rspec_patterns
12-
}
13-
},
14-
'RSpec/FakeCop' => {
15-
'Exclude' => %w[bar_spec.rb]
16-
}
17-
}
18-
19-
RuboCop::Config.new(rubocop_config, 'fake_cop_config.yml')
20-
end
21-
22-
let(:rspec_patterns) { ['_spec.rb$', '(?:^|/)spec/'] }
4+
let(:cop_class) { RuboCop::RSpec::FakeCop }
5+
let(:cop_config) { { 'Exclude' => %w[bar_spec.rb] } }
236

247
before do
258
stub_const('RuboCop::RSpec::FakeCop',
@@ -78,8 +61,12 @@ def on_send(node)
7861
end
7962

8063
context 'when custom patterns are specified' do
81-
let(:rspec_patterns) do
82-
['_test\.rb$']
64+
let(:other_cops) do
65+
{
66+
'RSpec' => {
67+
'Include' => ['*_test\.rb']
68+
}
69+
}
8370
end
8471

8572
it 'registers offenses when the path matches a custom specified pattern' do

spec/rubocop/cop/rspec/factory_bot/attribute_defined_statically_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# frozen_string_literal: true
22

33
RSpec.describe RuboCop::Cop::RSpec::FactoryBot::AttributeDefinedStatically do
4+
def inspected_source_filename
5+
'spec/factories.rb'
6+
end
7+
48
it 'registers an offense for offending code' do
59
expect_offense(<<-RUBY)
610
FactoryBot.define do

0 commit comments

Comments
 (0)