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 #289

Open
wants to merge 4 commits into
base: main
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
3 changes: 2 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
PATH
remote: .
specs:
rubocop-sorbet (0.8.9)
rubocop-sorbet (0.9.0)
lint_roller (~> 1.1)
rubocop (>= 1)

GEM
Expand Down
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,33 @@ You need to tell RuboCop to load the Sorbet extension. There are three ways to d
Put this into your `.rubocop.yml`:

```yaml
require: rubocop-sorbet
plugins: rubocop-sorbet
```

Alternatively, use the following array notation when specifying multiple extensions:

```yaml
require:
- rubocop-other-extension
plugins:
- rubocop-sorbet
- rubocop-other-extension
```

Now you can run `rubocop` and it will automatically load the RuboCop Sorbet 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-sorbet
rubocop --plugin rubocop-sorbet
```

### Rake task

```ruby
RuboCop::RakeTask.new do |task|
task.requires << 'rubocop-sorbet'
task.plugins << 'rubocop-sorbet'
end
```

Expand Down
7 changes: 5 additions & 2 deletions lib/rubocop-sorbet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

require_relative "rubocop/sorbet"
require_relative "rubocop/sorbet/version"
require_relative "rubocop/sorbet/inject"
require_relative "rubocop/sorbet/plugin"

RuboCop::Sorbet::Inject.defaults!
unless RuboCop::Sorbet::Plugin::SUPPORTED
require_relative "rubocop/sorbet/inject"
RuboCop::Sorbet::Inject.defaults!
end

require_relative "rubocop/cop/sorbet_cops"
15 changes: 10 additions & 5 deletions lib/rubocop/sorbet.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
# frozen_string_literal: true

require "rubocop"
require "rubocop/sorbet/version"
require "rubocop/sorbet/plugin"
require "pathname"
require "yaml"

module RuboCop
module Sorbet
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
unless Plugin::SUPPORTED
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)
private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)

::RuboCop::ConfigObsoletion.files << PROJECT_ROOT.join("config", "obsoletion.yml")
::RuboCop::ConfigObsoletion.files << PROJECT_ROOT.join("config", "obsoletion.yml")
end
end
end
36 changes: 36 additions & 0 deletions lib/rubocop/sorbet/plugin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require "rubocop"
require "lint_roller"
require "pathname"

module RuboCop
module Sorbet
# A plugin that integrates RuboCop Sorbet with RuboCop's plugin system.
class Plugin < LintRoller::Plugin
RUBOCOP_MIN_VERSION = "1.72.1"
SUPPORTED = Gem::Version.create(RuboCop::Version.version) < RUBOCOP_MIN_VERSION

def about
LintRoller::About.new(
name: "rubocop-sorbet",
version: VERSION,
homepage: "https://github.com/Shopify/rubocop-sorbet",
description: "A collection of Rubocop rules for Sorbet.",
)
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
2 changes: 1 addition & 1 deletion lib/rubocop/sorbet/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module RuboCop
module Sorbet
VERSION = "0.8.9"
VERSION = "0.9.0"
end
end
2 changes: 2 additions & 0 deletions rubocop-sorbet.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Gem::Specification.new do |spec|
spec.metadata["allowed_push_host"] = "https://rubygems.org"
spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "https://github.com/shopify/rubocop-sorbet"
spec.metadata["default_lint_roller_plugin"] = "RuboCop::Sorbet::Plugin"

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
Expand All @@ -27,5 +28,6 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_runtime_dependency("lint_roller", "~> 1.1")
spec.add_runtime_dependency("rubocop", ">= 1")
end