Skip to content

Commit

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

It replaces the ad-hoc `Inject` with RuboCop plugins introduced in RuboCop 1.72.
Additionally, since RuboCop already requires Ruby 2.7 or higher as its runtime,
the `required_ruby_version` will be updated to 2.7.

Follow-up rubocop/rubocop#13792
  • Loading branch information
koic committed Feb 14, 2025
1 parent bd44ddb commit e7da37a
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 19 deletions.
6 changes: 4 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
inherit_from: .rubocop_todo.yml

require:
- rubocop/cop/internal_affairs
plugins:
- rubocop-internal_affairs
- rubocop-rake

require:
- rubocop-rspec

AllCops:
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ gemspec

gem "rake", "~> 13.0"
gem 'rspec'
gem 'rubocop', '>= 0.76'
gem 'rubocop', github: 'rubocop/rubocop'
gem 'rubocop-rspec'
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,27 @@ ways to do this:
Put this into your `.rubocop.yml`.

```yaml
require: rubocop-rake
plugins: rubocop-rake
```
Alternatively, use the following array notation when specifying multiple extensions.
```yaml
require:
plugins:
- rubocop-other-extension
- rubocop-rake
```
Now you can run `rubocop` and it will automatically load the RuboCop Rake
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

```bash
rubocop --require rubocop-rake
rubocop --plugin rubocop-rake
```

### Rake task
Expand All @@ -57,7 +60,7 @@ rubocop --require rubocop-rake
require 'rubocop/rake_task'
RuboCop::RakeTask.new do |task|
task.requires << 'rubocop-rake'
task.plugins << 'rubocop-rake'
end
```

Expand All @@ -71,4 +74,3 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/rubocop/rubocop-rake.

4 changes: 1 addition & 3 deletions lib/rubocop-rake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

require_relative 'rubocop/rake'
require_relative 'rubocop/rake/version'
require_relative 'rubocop/rake/inject'

RuboCop::Rake::Inject.defaults!
require_relative 'rubocop/rake/plugin'

require_relative 'rubocop/cop/rake/helper/class_definition'
require_relative 'rubocop/cop/rake/helper/on_task'
Expand Down
6 changes: 0 additions & 6 deletions lib/rubocop/rake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,5 @@ module RuboCop
# :nodoc:
module Rake
class Error < StandardError; end

PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
CONFIG_DEFAULT = PROJECT_ROOT.join('config', 'default.yml').freeze
CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze

private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
end
end
31 changes: 31 additions & 0 deletions lib/rubocop/rake/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 Rake
# A plugin that integrates RuboCop Rake with RuboCop's plugin system.
class Plugin < LintRoller::Plugin
def about
LintRoller::About.new(
name: 'rubocop-rake',
version: Version::STRING,
homepage: 'https://github.com/rubocop/rubocop-rake',
description: 'A RuboCop plugin for Rake.',
)
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
6 changes: 4 additions & 2 deletions rubocop-rake.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Gem::Specification.new do |spec|
'homepage_uri' => spec.homepage,
'source_code_uri' => spec.homepage,
'changelog_uri' => "https://github.com/rubocop/rubocop-rake/blob/master/CHANGELOG.md",
'rubygems_mfa_required' => 'true'
'rubygems_mfa_required' => 'true',
'default_lint_roller_plugin' => 'RuboCop::Rake::Plugin'
}

# Specify which files should be added to the gem when it is released.
Expand All @@ -31,5 +32,6 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency 'rubocop', '~> 1.0'
spec.add_dependency 'lint_roller', '~> 1.1'
spec.add_dependency 'rubocop', '>= 1.72.0'
end

0 comments on commit e7da37a

Please sign in to comment.