From a2eb19a0c4e7d5d641594e93a66918e6db89a816 Mon Sep 17 00:00:00 2001 From: Jerome Dalbert Date: Sun, 23 Mar 2025 12:57:37 -0700 Subject: [PATCH] Make rubocop-packs work as a RuboCop plugin --- .rubocop.yml | 2 +- Gemfile.lock | 48 ++++++++++++++++++++---------------- README.md | 5 +++- lib/rubocop-packs.rb | 5 ++-- lib/rubocop/packs.rb | 5 ---- lib/rubocop/packs/inject.rb | 24 ------------------ lib/rubocop/packs/plugin.rb | 32 ++++++++++++++++++++++++ lib/rubocop/packs/version.rb | 8 ++++++ rubocop-packs.gemspec | 10 +++++--- spec/spec_helper.rb | 2 -- 10 files changed, 81 insertions(+), 60 deletions(-) delete mode 100644 lib/rubocop/packs/inject.rb create mode 100644 lib/rubocop/packs/plugin.rb create mode 100644 lib/rubocop/packs/version.rb diff --git a/.rubocop.yml b/.rubocop.yml index 922465b..071775d 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -14,7 +14,7 @@ AllCops: Exclude: - vendor/bundle/**/** -require: rubocop-sorbet +plugins: rubocop-sorbet Sorbet/StrictSigil: Enabled: true diff --git a/Gemfile.lock b/Gemfile.lock index 20e49b3..8ce0fdc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -5,10 +5,11 @@ PATH activesupport base64 bigdecimal + lint_roller (~> 1.1) packs-specification parse_packwerk - rubocop (~> 1.0) - rubocop-sorbet (>= 0.8.4) + rubocop (~> 1.72) + rubocop-sorbet (>= 0.9.0) sorbet-runtime GEM @@ -19,7 +20,7 @@ GEM i18n (>= 1.6, < 2) minitest (>= 5.1) tzinfo (~> 2.0) - ast (2.4.2) + ast (2.4.3) base64 (0.2.0) bigdecimal (3.1.8) concurrent-ruby (1.1.10) @@ -27,27 +28,28 @@ GEM erubi (1.13.0) i18n (1.12.0) concurrent-ruby (~> 1.0) - json (2.6.2) + json (2.10.2) + language_server-protocol (3.17.0.4) + lint_roller (1.1.0) minitest (5.16.3) netrc (0.11.0) packs-specification (0.0.10) sorbet-runtime - parallel (1.25.1) + parallel (1.26.3) parse_packwerk (0.26.0) bigdecimal sorbet-runtime - parser (3.3.4.0) + parser (3.3.7.2) ast (~> 2.4.1) racc prism (0.30.0) - racc (1.8.0) + racc (1.8.1) rainbow (3.1.1) rake (13.0.6) rbi (0.1.14) prism (>= 0.18.0, < 1.0.0) sorbet-runtime (>= 0.5.9204) - regexp_parser (2.6.0) - rexml (3.2.5) + regexp_parser (2.10.0) rspec (3.11.0) rspec-core (~> 3.11.0) rspec-expectations (~> 3.11.0) @@ -61,25 +63,27 @@ GEM diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.11.0) rspec-support (3.11.1) - rubocop (1.36.0) + rubocop (1.74.0) json (~> 2.3) + language_server-protocol (~> 3.17.0.2) + lint_roller (~> 1.1.0) parallel (~> 1.10) - parser (>= 3.1.2.1) + parser (>= 3.3.0.2) rainbow (>= 2.2.2, < 4.0) - regexp_parser (>= 1.8, < 3.0) - rexml (>= 3.2.5, < 4.0) - rubocop-ast (>= 1.20.1, < 2.0) + regexp_parser (>= 2.9.3, < 3.0) + rubocop-ast (>= 1.38.0, < 2.0) ruby-progressbar (~> 1.7) - unicode-display_width (>= 1.4.0, < 3.0) - rubocop-ast (1.21.0) - parser (>= 3.1.1.0) + unicode-display_width (>= 2.4.0, < 4.0) + rubocop-ast (1.41.0) + parser (>= 3.3.7.2) rubocop-extension-generator (0.5.1) activesupport bundler rubocop (>= 1.22.0) - rubocop-sorbet (0.8.5) + rubocop-sorbet (0.9.0) + lint_roller (~> 1.1) rubocop (>= 1) - ruby-progressbar (1.11.0) + ruby-progressbar (1.13.0) sorbet (0.5.11492) sorbet-static (= 0.5.11492) sorbet-runtime (0.5.11492) @@ -105,7 +109,9 @@ GEM thor (1.3.1) tzinfo (2.0.5) concurrent-ruby (~> 1.0) - unicode-display_width (2.3.0) + unicode-display_width (3.1.4) + unicode-emoji (~> 4.0, >= 4.0.4) + unicode-emoji (4.0.4) yard (0.9.36) yard-sorbet (0.9.0) sorbet-runtime @@ -126,4 +132,4 @@ DEPENDENCIES tapioca BUNDLED WITH - 2.2.33 + 2.2.34 diff --git a/README.md b/README.md index 27741b6..5735462 100644 --- a/README.md +++ b/README.md @@ -24,12 +24,15 @@ You need to tell RuboCop to load the Packs extension. There are three ways to do Put this into your `.rubocop.yml`: ```yaml -require: +plugins: - rubocop-packs ``` Now you can run `rubocop` and it will automatically load the RuboCop Packs cops together with the standard cops. +> [!NOTE] +> The plugin system is supported in RuboCop 1.72+. In earlier versions, use `require` instead of `plugins`. + ## The Cops All cops are located under [`lib/rubocop/cop/packs`](lib/rubocop/cop/packs), and contain examples/documentation. diff --git a/lib/rubocop-packs.rb b/lib/rubocop-packs.rb index aa67b36..965e063 100644 --- a/lib/rubocop-packs.rb +++ b/lib/rubocop-packs.rb @@ -5,12 +5,11 @@ require 'packs-specification' require_relative 'rubocop/packs' -require_relative 'rubocop/packs/inject' +require_relative 'rubocop/packs/version' +require_relative 'rubocop/packs/plugin' require_relative 'rubocop/packwerk_lite' require 'rubocop/cop/packs/root_namespace_is_pack_name' require 'rubocop/cop/packs/typed_public_apis' require 'rubocop/cop/packs/class_methods_as_public_apis' require 'rubocop/cop/packs/documented_public_apis' - -RuboCop::Packs::Inject.defaults! diff --git a/lib/rubocop/packs.rb b/lib/rubocop/packs.rb index 4cb5c7b..0b62152 100644 --- a/lib/rubocop/packs.rb +++ b/lib/rubocop/packs.rb @@ -7,11 +7,6 @@ module RuboCop module Packs extend T::Sig - PROJECT_ROOT = T.let(Pathname.new(__dir__).parent.parent.expand_path.freeze, Pathname) - CONFIG_DEFAULT = T.let(PROJECT_ROOT.join('config', 'default.yml').freeze, Pathname) - - private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT) - sig { void } def self.bust_cache! config.bust_cache! diff --git a/lib/rubocop/packs/inject.rb b/lib/rubocop/packs/inject.rb deleted file mode 100644 index 4c73520..0000000 --- a/lib/rubocop/packs/inject.rb +++ /dev/null @@ -1,24 +0,0 @@ -# typed: strict -# frozen_string_literal: true - -# The original code is from https://github.com/rubocop/rubocop-rspec/blob/master/lib/rubocop/rspec/inject.rb -# See https://github.com/rubocop/rubocop-rspec/blob/master/MIT-LICENSE.md -module RuboCop - module Packs - # Because RuboCop doesn't yet support plugins, we have to monkey patch in a - # bit of our configuration. - module Inject - extend T::Sig - - sig { void } - def self.defaults! - path = CONFIG_DEFAULT.to_s - hash = ConfigLoader.send(:load_yaml_configuration, path) - 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 diff --git a/lib/rubocop/packs/plugin.rb b/lib/rubocop/packs/plugin.rb new file mode 100644 index 0000000..7d89a1d --- /dev/null +++ b/lib/rubocop/packs/plugin.rb @@ -0,0 +1,32 @@ +# typed: false +# frozen_string_literal: true + +require 'lint_roller' + +module RuboCop + module Packs + # A plugin that integrates rubocop-packs with RuboCop's plugin system. + class Plugin < LintRoller::Plugin + def about + LintRoller::About.new( + name: 'rubocop-packs', + version: VERSION, + homepage: 'https://github.com/rubyatscale/rubocop-packs', + description: 'A collection of Rubocop rules for gradually modularizing a ruby codebase' + ) + 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 diff --git a/lib/rubocop/packs/version.rb b/lib/rubocop/packs/version.rb new file mode 100644 index 0000000..3886d27 --- /dev/null +++ b/lib/rubocop/packs/version.rb @@ -0,0 +1,8 @@ +# typed: false +# frozen_string_literal: true + +module RuboCop + module Packs + VERSION = '0.0.45' + end +end diff --git a/rubocop-packs.gemspec b/rubocop-packs.gemspec index 14819e7..454c155 100644 --- a/rubocop-packs.gemspec +++ b/rubocop-packs.gemspec @@ -1,6 +1,8 @@ +require_relative 'lib/rubocop/packs/version' + Gem::Specification.new do |spec| spec.name = 'rubocop-packs' - spec.version = '0.0.45' + spec.version = RuboCop::Packs::VERSION spec.authors = ['Gusto Engineers'] spec.email = ['dev@gusto.com'] spec.summary = 'A collection of Rubocop rules for gradually modularizing a ruby codebase' @@ -13,6 +15,7 @@ Gem::Specification.new do |spec| spec.metadata['source_code_uri'] = 'https://github.com/rubyatscale/rubocop-packs' spec.metadata['changelog_uri'] = 'https://github.com/rubyatscale/rubocop-packs/releases' spec.metadata['allowed_push_host'] = 'https://rubygems.org' + spec.metadata['default_lint_roller_plugin'] = 'RuboCop::Packs::Plugin' else raise 'RubyGems 2.0 or newer is required to protect against ' \ 'public gem pushes.' @@ -26,10 +29,11 @@ Gem::Specification.new do |spec| spec.add_dependency 'activesupport' spec.add_dependency 'base64' spec.add_dependency 'bigdecimal' + spec.add_dependency 'lint_roller', '~> 1.1' spec.add_dependency 'packs-specification' spec.add_dependency 'parse_packwerk' - spec.add_dependency 'rubocop', '~> 1.0' - spec.add_dependency 'rubocop-sorbet', '>= 0.8.4' + spec.add_dependency 'rubocop', '~> 1.72' + spec.add_dependency 'rubocop-sorbet', '>= 0.9.0' spec.add_dependency 'sorbet-runtime' spec.add_development_dependency 'bundler', '~> 2.2.16' diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8e22320..68a6468 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -17,8 +17,6 @@ c.syntax = :expect end - config.include RuboCop::RSpec::ExpectOffense - config.around do |example| ParsePackwerk.bust_cache! RuboCop::Packs.bust_cache!