Skip to content

Commit

Permalink
Restore injection for old rubocop versions
Browse files Browse the repository at this point in the history
  • Loading branch information
ngan committed Mar 7, 2025
1 parent efa5470 commit 901477e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ PATH
specs:
rubocop-sorbet (0.9.0)
lint_roller (~> 1.1)
rubocop (>= 1.72.1)
rubocop (>= 1)

GEM
remote: https://rubygems.org/
Expand Down
5 changes: 5 additions & 0 deletions lib/rubocop-sorbet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@
require_relative "rubocop/sorbet/version"
require_relative "rubocop/sorbet/plugin"

if Gem::Version.create(RuboCop::Version.version) < RuboCop::Sorbet::Plugin::RUBOCOP_MIN_VERSION
require_relative "rubocop/sorbet/inject"
RuboCop::Sorbet::Inject.defaults!
end

require_relative "rubocop/cop/sorbet_cops"
13 changes: 13 additions & 0 deletions lib/rubocop/sorbet.rb
Original file line number Diff line number Diff line change
@@ -1,10 +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

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)

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

# The original code is from https://github.com/rubocop-hq/rubocop-rspec/blob/master/lib/rubocop/rspec/inject.rb
# See https://github.com/rubocop-hq/rubocop-rspec/blob/master/MIT-LICENSE.md
module RuboCop
module Sorbet
# Because RuboCop doesn't yet support plugins, we have to monkey patch in a
# bit of our configuration.
module Inject
class << self
def defaults!
path = CONFIG_DEFAULT.to_s
hash = ConfigLoader.send(:load_yaml_configuration, path)
if Gem::Version.new(RuboCop::Version::STRING) >= Gem::Version.new("1.66")
# We use markdown for cop documentation. Unconditionally setting
# the base url will produce incorrect urls on older RuboCop versions,
# so we do that for fully supported version only here.
hash["Sorbet"] ||= {}
hash["Sorbet"]["DocumentationBaseURL"] = "https://github.com/Shopify/rubocop-sorbet/blob/main/manual"
hash["Sorbet"]["DocumentationExtension"] = ".md"
end
config = Config.new(hash, path).tap(&:make_excludes_absolute)
puts "configuration from #{path}" if ConfigLoader.debug?
config = ConfigLoader.merge_with_default(config, path)
ConfigLoader.instance_variable_set(:@default_configuration, config)
end
end
end
end
end
5 changes: 5 additions & 0 deletions lib/rubocop/sorbet/plugin.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# 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',
Expand Down
2 changes: 1 addition & 1 deletion rubocop-sorbet.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +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("rubocop", ">= 1.72.1")
spec.add_runtime_dependency("rubocop", ">= 1")
spec.add_runtime_dependency("lint_roller", "~> 1.1")
end

0 comments on commit 901477e

Please sign in to comment.