Skip to content

Commit e734b08

Browse files
committed
Make RuboCop RSpecRails work as a RuboCop plugin
This pull request adds support for RuboCop's plugin feature, added in rubocop/rubocop#13792, released in v1.72. It replaces the ad-hoc `inject_defaults!` with RuboCop plugins. Some Rake tasks may still need to use `inject_defaults!`.
1 parent 2d257f2 commit e734b08

File tree

10 files changed

+61
-20
lines changed

10 files changed

+61
-20
lines changed

.rubocop.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
inherit_from: .rubocop_todo.yml
22

3-
require:
3+
plugins:
44
- rubocop-performance
55
- rubocop-rake
66
- rubocop-rspec
7-
- rubocop/cop/internal_affairs
7+
- rubocop-internal_affairs
88

99
AllCops:
1010
DisplayCopNames: true

CHANGELOG.md

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

55
- Handle unknown HTTP status codes for `RSpecRails/HttpStatus` cop. ([@viralpraxis])
66
- Fix a false negative for `RSpecRails/TravelAround` cop when passed as a proc to a travel method. ([@ydah])
7+
- Make RuboCop RSpecRails work as a RuboCop plugin. ([@bquorning])
78

89
## 2.30.0 (2024-06-12)
910

Gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ gem 'bump'
88
gem 'rack'
99
gem 'rake'
1010
gem 'rspec', '~> 3.11'
11-
gem 'rubocop-performance', '~> 1.7'
12-
gem 'rubocop-rake', '~> 0.6'
11+
gem 'rubocop-performance', '~> 1.24'
12+
gem 'rubocop-rake', '~> 0.7'
1313
gem 'simplecov', '>= 0.19'
1414
gem 'yard'
1515

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,34 @@ ways to do this:
3333
Put this into your `.rubocop.yml`.
3434

3535
```yaml
36-
require: rubocop-rspec_rails
36+
plugins: rubocop-rspec_rails
3737
```
3838
3939
Alternatively, use the following array notation when specifying multiple extensions.
4040
4141
```yaml
42-
require:
42+
plugins:
4343
- rubocop-rspec
4444
- rubocop-rspec_rails
4545
```
4646
4747
Now you can run `rubocop` and it will automatically load the RuboCop RSpec Rails
4848
cops together with the standard cops.
4949

50+
> [!NOTE]
51+
> The plugin system is supported in RuboCop 1.72+. In earlier versions, use `require` instead of `plugins`.
52+
5053
### Command line
5154

5255
```bash
53-
rubocop --require rubocop-rspec_rails
56+
rubocop --plugin rubocop-rspec_rails
5457
```
5558

5659
### Rake task
5760

5861
```ruby
5962
RuboCop::RakeTask.new do |task|
60-
task.requires << 'rubocop-rspec_rails'
63+
task.plugins << 'rubocop-rspec_rails'
6164
end
6265
```
6366

docs/modules/ROOT/pages/usage.adoc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,35 @@ There are three ways to do this:
88
Put this into your `.rubocop.yml`:
99

1010
----
11-
require: rubocop-rspec_rails
11+
plugins: rubocop-rspec_rails
1212
----
1313

1414
or, if you are using several extensions:
1515

1616
----
17-
require:
17+
plugins:
1818
- rubocop-rspec
1919
- rubocop-rspec_rails
2020
----
2121

2222
Now you can run `rubocop` and it will automatically load the RuboCop RSpec Rails
2323
cops together with the standard cops.
2424

25+
NOTE: The plugin system is supported in RuboCop 1.72+. In earlier versions, use `require` instead of `plugins`.
26+
2527
== Command line
2628

2729
[source,bash]
2830
----
29-
$ rubocop --require rubocop-rspec_rails
31+
$ rubocop --plugin rubocop-rspec_rails
3032
----
3133

3234
== Rake task
3335

3436
[source,ruby]
3537
----
3638
RuboCop::RakeTask.new do |task|
37-
task.requires << 'rubocop-rspec_rails'
39+
task.plugins << 'rubocop-rspec_rails'
3840
end
3941
----
4042

lib/rubocop-rspec_rails.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
require 'rubocop'
77
require 'rubocop/rspec/language'
88

9+
require_relative 'rubocop/rspec_rails/plugin'
910
require_relative 'rubocop/rspec_rails/version'
1011

1112
require 'rubocop/cop/rspec/base'
1213
require_relative 'rubocop/cop/rspec_rails_cops'
13-
14-
project_root = File.join(__dir__, '..')
15-
RuboCop::ConfigLoader.inject_defaults!(project_root)

lib/rubocop/rspec_rails/plugin.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
require 'lint_roller'
4+
5+
module RuboCop
6+
module RSpecRails
7+
# A plugin that integrates RuboCop RSpecRails with RuboCop's plugin system.
8+
class Plugin < LintRoller::Plugin
9+
# :nocov:
10+
def about
11+
LintRoller::About.new(
12+
name: 'rubocop-rspec_rails',
13+
version: Version::STRING,
14+
homepage: 'https://github.com/rubocop/rubocop-rspec_rails',
15+
description: 'Code style checking for RSpec Rails files.'
16+
)
17+
end
18+
# :nocov:
19+
20+
def supported?(context)
21+
context.engine == :rubocop
22+
end
23+
24+
def rules(_context)
25+
project_root = Pathname.new(__dir__).join('../../..')
26+
27+
LintRoller::Rules.new(
28+
type: :path,
29+
config_format: :rubocop,
30+
value: project_root.join('config/default.yml')
31+
)
32+
end
33+
end
34+
end
35+
end

rubocop-rspec_rails.gemspec

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ Gem::Specification.new do |spec|
3131
spec.metadata = {
3232
'changelog_uri' => 'https://github.com/rubocop/rubocop-rspec_rails/blob/master/CHANGELOG.md',
3333
'documentation_uri' => 'https://docs.rubocop.org/rubocop-rspec_rails/',
34-
'rubygems_mfa_required' => 'true'
34+
'rubygems_mfa_required' => 'true',
35+
'default_lint_roller_plugin' => 'RuboCop::RSpecRails::Plugin'
3536
}
3637

37-
spec.add_runtime_dependency 'rubocop', '~> 1.61'
38-
spec.add_runtime_dependency 'rubocop-rspec', '~> 3', '>= 3.0.1'
38+
spec.add_dependency 'lint_roller', '~> 1.1'
39+
spec.add_dependency 'rubocop', '~> 1.72', '>= 1.72.1'
40+
spec.add_runtime_dependency 'rubocop-rspec', '~> 3.5'
3941
end

spec/spec_helper.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ module SpecHelper
4242
# We should take their advice!
4343
config.raise_on_warning = true
4444

45-
config.include(ExpectOffense)
46-
4745
config.include_context 'with default RSpec/Language config', :config
4846
config.include_context 'smoke test', type: :cop_spec
4947
end

tasks/cops_documentation.rake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ end
1212

1313
desc 'Generate docs of all cops departments'
1414
task generate_cops_documentation: :yard_for_generate_documentation do
15+
RuboCop::ConfigLoader.inject_defaults!("#{__dir__}/../config/default.yml")
16+
1517
generator = CopsDocumentationGenerator.new(
1618
departments: %w[RSpecRails]
1719
)

0 commit comments

Comments
 (0)