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.

Since RuboCop Performance still supports some older versions of RuboCop,
a conditional check using `RuboCop.const_defined?(:Plugin)` has been added for compatibility.

Once only versions of RuboCop that support the Plugin feature are supported,
the compatibility code and the `RuboCop::Performance::Inject` module will be removed.

This means that the legacy style will gradually become deprecated in the near future.
  • Loading branch information
koic committed Feb 4, 2025
1 parent ab7ef16 commit a45b18d
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 14 deletions.
5 changes: 4 additions & 1 deletion .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:

plugins:
- rubocop/cop/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 this once rubocop/rubocop#13792 is merged and released.
gem 'rubocop', github: 'koic/rubocop', branch: 'support_rubocop_extension_plugin'
gem 'rubocop-rspec', '~> 3.3.0'
gem 'simplecov'
gem 'test-queue'
Expand Down
49 changes: 45 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,51 @@ gem 'rubocop-performance', require: false

## Usage

You need to tell RuboCop to load the Performance extension. There are three
You need to tell RuboCop to load the Performance extension. There are some
ways to do this:

### RuboCop configuration file
### RuboCop configuration file (Recommended Style)

Put this into your `.rubocop.yml`.

```yaml
plugins: rubocop-performance
```
Alternatively, use the following array notation when specifying multiple extensions.
```yaml
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+.

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

### Command line

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

### Rake task

```ruby
require 'rubocop/rake_task'
RuboCop::RakeTask.new do |task|
task.plugins << 'rubocop-performance'
end
```

### RuboCop configuration file (Legacy Style)

Put this into your `.rubocop.yml`.

Expand All @@ -44,13 +85,13 @@ require:
Now you can run `rubocop` and it will automatically load the RuboCop Performance
cops together with the standard cops.

### Command line
### Command line (Legacy Style)

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

### Rake task
### Rake task (Legacy Style)

```ruby
require 'rubocop/rake_task'
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][])
38 changes: 34 additions & 4 deletions docs/modules/ROOT/pages/usage.adoc
Original file line number Diff line number Diff line change
@@ -1,29 +1,59 @@
= Usage

You need to tell RuboCop to load the Performance extension. There are three
You need to tell RuboCop to load the Performance extension. There are some
ways to do this:

== RuboCop configuration file
== RuboCop configuration file (Recommended Style)

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+.

== Command line

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

== Rake task

[source,ruby]
----
RuboCop::RakeTask.new do |task|
task.plugins << 'rubocop-performance'
end
----

== RuboCop configuration file (Legacy Style)

Put this into your `.rubocop.yml`.

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

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

== Command line (Legacy Style)

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

== Rake task (Legacy Style)

[source,ruby]
----
RuboCop::RakeTask.new do |task|
Expand Down
12 changes: 10 additions & 2 deletions lib/rubocop-performance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@

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

RuboCop::Performance::Inject.defaults!
# FIXME: When RuboCop Rails requires RuboCop 1.72.0+ only, the following compatibility code can be removed.
if RuboCop.const_defined?(:Plugin)
require_relative 'rubocop/performance/plugin'
else
# NOTE: Until the plugin stabilizes, an option to use the older version of RuboCop is provided.
# The plugin will be unified in the future.
require_relative 'rubocop/performance/inject'

RuboCop::Performance::Inject.defaults!
end

require_relative 'rubocop/cop/performance_cops'

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/performance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ module Performance

private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)

::RuboCop::ConfigObsoletion.files << PROJECT_ROOT.join('config', 'obsoletion.yml')
ConfigObsoletion.files << Pathname("#{__dir__}/../../config/obsoletion.yml")
end
end
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)
LintRoller::Rules.new(
type: :path,
config_format: :rubocop,
value: Pathname.new(__dir__).join('../../../config/default.yml')
)
end
end
end
end
3 changes: 3 additions & 0 deletions rubocop-performance.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Gem::Specification.new do |s|
'rubygems_mfa_required' => 'true'
}

s.metadata['default_lint_roller_plugin'] = 'RuboCop::Performance::Plugin'

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

# FIXME: Once only RuboCop versions that support plugins remain, please remove the `if` condition.
RuboCop::ConfigLoader.inject_defaults!("#{__dir__}/../config/default.yml") if RuboCop.const_defined?(:Plugin)

if ENV.fetch('COVERAGE', nil) == 'true'
require 'simplecov'
SimpleCov.start
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 a45b18d

Please sign in to comment.