Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pluginfy RuboCop Performance #490

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading