Skip to content

Commit

Permalink
Pluginfy RuboCop Performance
Browse files Browse the repository at this point in the history
This PR adds support for RuboCop's Plugin feature.

It replaces the ad-hoc `Inject` with RuboCop plugins introduced in RuboCop 1.72.

Follow-up rubocop/rubocop#13792
  • Loading branch information
koic committed Feb 15, 2025
1 parent ab7ef16 commit a473e49
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ jobs:
sed -e "/gem 'rubocop', github: 'rubocop\/rubocop'/d" \
-e "/gem 'rubocop-rspec',/d" -i Gemfile
cat << EOF > Gemfile.local
gem 'rubocop', '1.48.1' # Specify the oldest supported RuboCop version
gem 'rubocop', '1.72.0' # Specify the oldest supported RuboCop version
EOF
- name: set up Ruby
uses: ruby/setup-ruby@v1
Expand Down
7 changes: 5 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# This is the configuration used to check the rubocop source code.

inherit_from: .rubocop_todo.yml
require:
- rubocop/cop/internal_affairs

plugins:
- rubocop-internal_affairs
- rubocop-performance

require:
- rubocop-rspec

AllCops:
Expand Down
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ gem 'bump', require: false
gem 'prism'
gem 'rake'
gem 'rspec'
gem 'rubocop', github: 'rubocop/rubocop'
# FIXME: Revert to `gem 'rubocop', github: 'rubocop/rubocop'` when rubocop/rubocop#13840 is merged.
gem 'rubocop', github: 'koic/rubocop', branch: 'plugin_is_loaded_automatically_with_rubocop_rspec_support'
gem 'rubocop-rspec', '~> 3.3.0'
gem 'simplecov'
gem 'test-queue'
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,27 @@ ways to do this:
Put this into your `.rubocop.yml`.

```yaml
require: rubocop-performance
plugins: rubocop-performance
```
Alternatively, use the following array notation when specifying multiple extensions.
```yaml
require:
plugins:
- rubocop-other-extension
- rubocop-performance
```
Now you can run `rubocop` and it will automatically load the RuboCop Performance
cops together with the standard cops.

> [!NOTE]
> The plugin system is supported in RuboCop 1.72+. In earlier versions, use `require` instead of `plugins`.

### Command line

```sh
$ rubocop --require rubocop-performance
$ rubocop --plugin rubocop-performance
```

### Rake task
Expand All @@ -56,7 +59,7 @@ $ rubocop --require rubocop-performance
require 'rubocop/rake_task'
RuboCop::RakeTask.new do |task|
task.requires << 'rubocop-performance'
task.plugins << 'rubocop-performance'
end
```

Expand Down
1 change: 1 addition & 0 deletions changelog/new_pluginfy_with_lint_roller.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#490](https://github.com/rubocop/rubocop-performance/pull/490): Pluginfy RuboCop Performance. ([@koic][])
8 changes: 5 additions & 3 deletions docs/modules/ROOT/pages/usage.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,26 @@ Put this into your `.rubocop.yml`.

[source,yaml]
----
require: rubocop-performance
plugins: rubocop-performance
----

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

NOTE: The plugin system is supported in RuboCop 1.72+. In earlier versions, use `require` instead of `plugins`.

== Command line

[source,sh]
----
$ rubocop --require rubocop-performance
$ rubocop --plugin rubocop-performance
----

== Rake task

[source,ruby]
----
RuboCop::RakeTask.new do |task|
task.requires << 'rubocop-performance'
task.plugins << 'rubocop-performance'
end
----
5 changes: 1 addition & 4 deletions lib/rubocop-performance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

require_relative 'rubocop/performance'
require_relative 'rubocop/performance/version'
require_relative 'rubocop/performance/inject'

RuboCop::Performance::Inject.defaults!

require_relative 'rubocop/performance/plugin'
require_relative 'rubocop/cop/performance_cops'

RuboCop::Cop::Lint::UnusedMethodArgument.singleton_class.prepend(
Expand Down
8 changes: 1 addition & 7 deletions lib/rubocop/performance.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
# frozen_string_literal: true

module RuboCop
# RuboCop Performance project namespace
# RuboCop Performance project namespace.
module Performance
PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
CONFIG_DEFAULT = PROJECT_ROOT.join('config', 'default.yml').freeze

private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)

::RuboCop::ConfigObsoletion.files << PROJECT_ROOT.join('config', 'obsoletion.yml')
end
end
17 changes: 0 additions & 17 deletions lib/rubocop/performance/inject.rb

This file was deleted.

31 changes: 31 additions & 0 deletions lib/rubocop/performance/plugin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require 'lint_roller'

module RuboCop
module Performance
# A plugin that integrates RuboCop Performance with RuboCop's plugin system.
class Plugin < LintRoller::Plugin
def about
LintRoller::About.new(
name: 'rubocop-performance',
version: Version::STRING,
homepage: 'https://github.com/rubocop/rubocop-performance',
description: 'A collection of RuboCop cops to check for performance optimizations in Ruby code.'
)
end

def supported?(context)
context.engine == :rubocop
end

def rules(_context)
project_root = Pathname.new(__dir__).join('../../..')

ConfigObsoletion.files << project_root.join('config', 'obsoletion.yml')

LintRoller::Rules.new(type: :path, config_format: :rubocop, value: project_root.join('config', 'default.yml'))
end
end
end
end
6 changes: 4 additions & 2 deletions rubocop-performance.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ Gem::Specification.new do |s|
'source_code_uri' => 'https://github.com/rubocop/rubocop-performance/',
'documentation_uri' => "https://docs.rubocop.org/rubocop-performance/#{RuboCop::Performance::Version.document_version}/",
'bug_tracker_uri' => 'https://github.com/rubocop/rubocop-performance/issues',
'rubygems_mfa_required' => 'true'
'rubygems_mfa_required' => 'true',
'default_lint_roller_plugin' => 'RuboCop::Performance::Plugin'
}

s.add_dependency('rubocop', '>= 1.48.1', '< 2.0')
s.add_dependency('lint_roller', '~> 1.1')
s.add_dependency('rubocop', '>= 1.72.0', '< 2.0')
s.add_dependency('rubocop-ast', '>= 1.38.0', '< 2.0')
end
4 changes: 0 additions & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
end

RSpec.configure do |config|
# TODO: It can be removed when the oldest supported RuboCop version is greater than 1.71.0.
# https://github.com/rubocop/rubocop/pull/13748
config.include RuboCop::RSpec::ExpectOffense

config.shared_context_metadata_behavior = :apply_to_host_groups
config.filter_run_when_matching :focus
config.filter_run_excluding broken_on: :prism if ENV['PARSER_ENGINE'] == 'parser_prism'
Expand Down
2 changes: 1 addition & 1 deletion tasks/cops_documentation.rake
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ task update_cops_documentation: :yard_for_generate_documentation do

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

CopsDocumentationGenerator.new(departments: deps).call
end
Expand Down

0 comments on commit a473e49

Please sign in to comment.