Skip to content

Commit

Permalink
Merge pull request #1434 from koic/pluginfy_with_lint_roller
Browse files Browse the repository at this point in the history
Pluginfy RuboCop Rails
  • Loading branch information
koic authored Feb 15, 2025
2 parents 67066d7 + aeb3cac commit 897b22b
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ jobs:
-e "/gem 'rubocop-performance',/d" \
-e "/gem 'rubocop-rspec',/d" -i Gemfile
cat << EOF > Gemfile.local
gem 'rubocop', '1.52.0' # Specify the oldest supported RuboCop version
gem 'rubocop', '1.72.1'
EOF
- uses: ruby/setup-ruby@v1
with:
Expand Down
6 changes: 4 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# This is the configuration used to check the rubocop source code.

inherit_from: .rubocop_todo.yml
plugins:
- rubocop-internal_affairs
- rubocop-rails

require:
- rubocop/cop/internal_affairs
- rubocop-performance
- rubocop-rails
- rubocop-rspec

AllCops:
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,36 @@ ways to do this:
Put this into your `.rubocop.yml`.

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

Note: `--rails` option is required while `rubocop` command supports `--rails` option.

### Rake task

```ruby
require 'rubocop/rake_task'
RuboCop::RakeTask.new do |task|
task.requires << 'rubocop-rails'
task.plugins << 'rubocop-rails'
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 @@
* [#1434](https://github.com/rubocop/rubocop-rails/pull/1434): Pluginfy RuboCop Rails. ([@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,7 +9,7 @@ Put this into your `.rubocop.yml`.

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

Now you can run `rubocop` and it will automatically load the RuboCop Rails
Expand All @@ -19,18 +19,20 @@ cops together with the standard cops.

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

== Rake task

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

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

== RuboCop Rails configuration

The following settings specific to RuboCop Rails can be configured in `.rubocop.yml`.
Expand Down
5 changes: 1 addition & 4 deletions lib/rubocop-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@

require_relative 'rubocop/rails'
require_relative 'rubocop/rails/version'
require_relative 'rubocop/rails/inject'
require_relative 'rubocop/rails/schema_loader'
require_relative 'rubocop/rails/schema_loader/schema'

RuboCop::Rails::Inject.defaults!

require_relative 'rubocop/rails/plugin'
require_relative 'rubocop/cop/rails_cops'

require_relative 'rubocop/rails/migration_file_skippable'
Expand Down
8 changes: 1 addition & 7 deletions lib/rubocop/rails.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
# frozen_string_literal: true

module RuboCop
# RuboCop Rails project namespace
# RuboCop Rails project namespace.
module Rails
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/rails/inject.rb

This file was deleted.

31 changes: 31 additions & 0 deletions lib/rubocop/rails/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 Rails
# A plugin that integrates RuboCop Rails with RuboCop's plugin system.
class Plugin < LintRoller::Plugin
def about
LintRoller::About.new(
name: 'rubocop-rails',
version: Version::STRING,
homepage: 'https://github.com/rubocop/rubocop-rails',
description: 'A RuboCop extension focused on enforcing Rails best practices and coding conventions.'
)
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-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ Gem::Specification.new do |s|
'source_code_uri' => 'https://github.com/rubocop/rubocop-rails/',
'documentation_uri' => "https://docs.rubocop.org/rubocop-rails/#{RuboCop::Rails::Version.document_version}/",
'bug_tracker_uri' => 'https://github.com/rubocop/rubocop-rails/issues',
'rubygems_mfa_required' => 'true'
'rubygems_mfa_required' => 'true',
'default_lint_roller_plugin' => 'RuboCop::Rails::Plugin'
}

s.add_dependency 'activesupport', '>= 4.2.0'
# Rack::Utils::SYMBOL_TO_STATUS_CODE, which is used by HttpStatus cop, was
# introduced in rack 1.1
s.add_dependency 'lint_roller', '~> 1.1'
s.add_dependency 'rack', '>= 1.1'
s.add_dependency 'rubocop', '>= 1.52.0', '< 2.0'
s.add_dependency 'rubocop', '>= 1.72.1', '< 2.0'
s.add_dependency 'rubocop-ast', '>= 1.38.0', '< 2.0'
end
16 changes: 0 additions & 16 deletions spec/rubocop/rails/inject_spec.rb

This file was deleted.

4 changes: 0 additions & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,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 @@ -22,7 +22,7 @@ task update_cops_documentation: :yard_for_generate_documentation do

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

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

0 comments on commit 897b22b

Please sign in to comment.