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 Rails #1434

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
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.0' # Specify the oldest supported RuboCop version
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.0', '< 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: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
require_relative 'support/file_helper'
require_relative 'support/shared_contexts'

project_root = Pathname.new(__dir__).join('..')
RuboCop::ConfigLoader.inject_defaults!(project_root.join('config', 'default.yml'))
RuboCop::ConfigObsoletion.files << project_root.join('config', 'obsoletion.yml')

# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].sort.each { |f| require f }
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