Skip to content

Commit b83339d

Browse files
committed
Pluginfy RuboCop Performance
This PR adds support for RuboCop's Plugin feature: rubocop/rubocop#13792 It replaces the ad-hoc `Inject` with RuboCop plugins introduced in RuboCop 1.72. NOTE: Version 1.72.1 or later is specified, as it includes rubocop/rubocop#13837, which removes the `Inject` from spec_helper.rb. While 1.72.0 is sufficient for runtime, but specifying the patch version 1.72.1 keeps things simple.
1 parent ab7ef16 commit b83339d

File tree

12 files changed

+57
-45
lines changed

12 files changed

+57
-45
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ jobs:
9292
sed -e "/gem 'rubocop', github: 'rubocop\/rubocop'/d" \
9393
-e "/gem 'rubocop-rspec',/d" -i Gemfile
9494
cat << EOF > Gemfile.local
95-
gem 'rubocop', '1.48.1' # Specify the oldest supported RuboCop version
95+
gem 'rubocop', '1.72.1' # Specify the oldest supported RuboCop version
9696
EOF
9797
- name: set up Ruby
9898
uses: ruby/setup-ruby@v1

.rubocop.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# This is the configuration used to check the rubocop source code.
22

33
inherit_from: .rubocop_todo.yml
4-
require:
5-
- rubocop/cop/internal_affairs
4+
5+
plugins:
6+
- rubocop-internal_affairs
67
- rubocop-performance
8+
9+
require:
710
- rubocop-rspec
811

912
AllCops:

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,27 @@ ways to do this:
3030
Put this into your `.rubocop.yml`.
3131

3232
```yaml
33-
require: rubocop-performance
33+
plugins: rubocop-performance
3434
```
3535
3636
Alternatively, use the following array notation when specifying multiple extensions.
3737
3838
```yaml
39-
require:
39+
plugins:
4040
- rubocop-other-extension
4141
- rubocop-performance
4242
```
4343
4444
Now you can run `rubocop` and it will automatically load the RuboCop Performance
4545
cops together with the standard cops.
4646

47+
> [!NOTE]
48+
> The plugin system is supported in RuboCop 1.72+. In earlier versions, use `require` instead of `plugins`.
49+
4750
### Command line
4851

4952
```sh
50-
$ rubocop --require rubocop-performance
53+
$ rubocop --plugin rubocop-performance
5154
```
5255

5356
### Rake task
@@ -56,7 +59,7 @@ $ rubocop --require rubocop-performance
5659
require 'rubocop/rake_task'
5760
5861
RuboCop::RakeTask.new do |task|
59-
task.requires << 'rubocop-performance'
62+
task.plugins << 'rubocop-performance'
6063
end
6164
```
6265

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#490](https://github.com/rubocop/rubocop-performance/pull/490): Pluginfy RuboCop Performance. ([@koic][])

docs/modules/ROOT/pages/usage.adoc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,26 @@ Put this into your `.rubocop.yml`.
99

1010
[source,yaml]
1111
----
12-
require: rubocop-performance
12+
plugins: rubocop-performance
1313
----
1414

1515
Now you can run `rubocop` and it will automatically load the RuboCop Performance
1616
cops together with the standard cops.
1717

18+
NOTE: The plugin system is supported in RuboCop 1.72+. In earlier versions, use `require` instead of `plugins`.
19+
1820
== Command line
1921

2022
[source,sh]
2123
----
22-
$ rubocop --require rubocop-performance
24+
$ rubocop --plugin rubocop-performance
2325
----
2426

2527
== Rake task
2628

2729
[source,ruby]
2830
----
2931
RuboCop::RakeTask.new do |task|
30-
task.requires << 'rubocop-performance'
32+
task.plugins << 'rubocop-performance'
3133
end
3234
----

lib/rubocop-performance.rb

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

55
require_relative 'rubocop/performance'
66
require_relative 'rubocop/performance/version'
7-
require_relative 'rubocop/performance/inject'
8-
9-
RuboCop::Performance::Inject.defaults!
10-
7+
require_relative 'rubocop/performance/plugin'
118
require_relative 'rubocop/cop/performance_cops'
129

1310
RuboCop::Cop::Lint::UnusedMethodArgument.singleton_class.prepend(

lib/rubocop/performance.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
# frozen_string_literal: true
22

33
module RuboCop
4-
# RuboCop Performance project namespace
4+
# RuboCop Performance project namespace.
55
module Performance
6-
PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
7-
CONFIG_DEFAULT = PROJECT_ROOT.join('config', 'default.yml').freeze
8-
9-
private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
10-
11-
::RuboCop::ConfigObsoletion.files << PROJECT_ROOT.join('config', 'obsoletion.yml')
126
end
137
end

lib/rubocop/performance/inject.rb

Lines changed: 0 additions & 17 deletions
This file was deleted.

lib/rubocop/performance/plugin.rb

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

rubocop-performance.gemspec

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ Gem::Specification.new do |s|
2727
'source_code_uri' => 'https://github.com/rubocop/rubocop-performance/',
2828
'documentation_uri' => "https://docs.rubocop.org/rubocop-performance/#{RuboCop::Performance::Version.document_version}/",
2929
'bug_tracker_uri' => 'https://github.com/rubocop/rubocop-performance/issues',
30-
'rubygems_mfa_required' => 'true'
30+
'rubygems_mfa_required' => 'true',
31+
'default_lint_roller_plugin' => 'RuboCop::Performance::Plugin'
3132
}
3233

33-
s.add_dependency('rubocop', '>= 1.48.1', '< 2.0')
34+
s.add_dependency('lint_roller', '~> 1.1')
35+
s.add_dependency('rubocop', '>= 1.72.1', '< 2.0')
3436
s.add_dependency('rubocop-ast', '>= 1.38.0', '< 2.0')
3537
end

spec/spec_helper.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
end
1010

1111
RSpec.configure do |config|
12-
# TODO: It can be removed when the oldest supported RuboCop version is greater than 1.71.0.
13-
# https://github.com/rubocop/rubocop/pull/13748
14-
config.include RuboCop::RSpec::ExpectOffense
15-
1612
config.shared_context_metadata_behavior = :apply_to_host_groups
1713
config.filter_run_when_matching :focus
1814
config.filter_run_excluding broken_on: :prism if ENV['PARSER_ENGINE'] == 'parser_prism'

tasks/cops_documentation.rake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ task update_cops_documentation: :yard_for_generate_documentation do
1515

1616
# NOTE: Update `<<next>>` version for docs/modules/ROOT/pages/cops_performance.adoc
1717
# when running release tasks.
18-
RuboCop::Performance::Inject.defaults!
18+
RuboCop::ConfigLoader.inject_defaults!("#{__dir__}/../config/default.yml")
1919

2020
CopsDocumentationGenerator.new(departments: deps).call
2121
end

0 commit comments

Comments
 (0)