From 4e1eca3c6b0e108d9d66241092add4f3e4365bbd Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Sun, 3 Sep 2023 22:17:59 -0400 Subject: [PATCH] Use `requires_gem` API in `TargetRailsVersion` ... and fix invalid Gemfile.lock examples --- .../change_migrate_to_requires_gem_api.md | 1 + lib/rubocop/cop/mixin/target_rails_version.rb | 31 +++++++++- spec/rubocop/config_spec.rb | 62 +++++++++---------- 3 files changed, 61 insertions(+), 33 deletions(-) create mode 100644 changelog/change_migrate_to_requires_gem_api.md diff --git a/changelog/change_migrate_to_requires_gem_api.md b/changelog/change_migrate_to_requires_gem_api.md new file mode 100644 index 0000000000..5723b0479e --- /dev/null +++ b/changelog/change_migrate_to_requires_gem_api.md @@ -0,0 +1 @@ +* [#1137](https://github.com/rubocop/rubocop-rails/pull/1137): Migrate to `TargetRailsVersion` the new [`requires_gem` API](https://github.com/rubocop/rubocop/pull/12186). ([@amomchilov][]) diff --git a/lib/rubocop/cop/mixin/target_rails_version.rb b/lib/rubocop/cop/mixin/target_rails_version.rb index 6f9021407f..4dca5074ae 100644 --- a/lib/rubocop/cop/mixin/target_rails_version.rb +++ b/lib/rubocop/cop/mixin/target_rails_version.rb @@ -4,13 +4,40 @@ module RuboCop module Cop # Common functionality for checking target rails version. module TargetRailsVersion + # Informs the base RuboCop gem that it the Rails version is checked via `requires_gem` API, + # without needing to call this `#support_target_rails_version` method. + USES_REQUIRES_GEM_API = true + def minimum_target_rails_version(version) - @minimum_target_rails_version = version + if respond_to?(:requires_gem) + case version + when Integer, Float then requires_gem(TARGET_GEM_NAME, ">= #{version}") + when String then requires_gem(TARGET_GEM_NAME, version) + end + else + # Fallback path for previous versions of RuboCop which don't support the `requires_gem` API yet. + @minimum_target_rails_version = version + end end def support_target_rails_version?(version) - @minimum_target_rails_version <= version + if respond_to?(:requires_gem) + return false unless gem_requirements + + gem_requirement = gem_requirements[TARGET_GEM_NAME] + return true unless gem_requirement # If we have no requirement, then we support all versions + + gem_requirement.satisfied_by?(Gem::Version.new(version)) + else + # Fallback path for previous versions of RuboCop which don't support the `requires_gem` API yet. + @minimum_target_rails_version <= version + end end + + # Look for `railties` instead of `rails`, to support apps that only use a subset of `rails` + # See https://github.com/rubocop/rubocop/pull/11289 + TARGET_GEM_NAME = 'railties' + private_constant :TARGET_GEM_NAME end end end diff --git a/spec/rubocop/config_spec.rb b/spec/rubocop/config_spec.rb index dbb727d529..d8b8103acc 100644 --- a/spec/rubocop/config_spec.rb +++ b/spec/rubocop/config_spec.rb @@ -61,20 +61,20 @@ remote: https://rubygems.org/ specs: actionmailer (4.1.0) - actionpack (= 4.1.0) - actionview (= 4.1.0) - mail (~> 2.5.4) - rails (4.1.0) - actionmailer (= 4.1.0) - actionpack (= 4.1.0) - actionview (= 4.1.0) - activemodel (= 4.1.0) - activerecord (= 4.1.0) - activesupport (= 4.1.0) - bundler (>= 1.3.0, < 2.0) - railties (= 4.1.0) - sprockets-rails (~> 2.0) - railties (4.1.0) + actionpack (4.1.0) + actionview (4.1.0) + mail (2.5.4) + rails (4.1.0) + actionmailer (= 4.1.0) + actionpack (= 4.1.0) + actionview (= 4.1.0) + activemodel (= 4.1.0) + activerecord (= 4.1.0) + activesupport (= 4.1.0) + bundler (>= 1.3.0, < 2.0) + railties (= 4.1.0) + sprockets-rails (~> 2.0) + railties (4.1.0) PLATFORMS ruby @@ -96,20 +96,20 @@ remote: https://rubygems.org/ specs: actionmailer (4.1.0) - actionpack (= 4.1.0) - actionview (= 4.1.0) - mail (~> 2.5.4) - rails (400.33.22) - actionmailer (= 4.1.0) - actionpack (= 4.1.0) - actionview (= 4.1.0) - activemodel (= 4.1.0) - activerecord (= 4.1.0) - activesupport (= 4.1.0) - bundler (>= 1.3.0, < 2.0) - railties (= 4.1.0) - sprockets-rails (~> 2.0) - railties (400.33.22) + actionpack (4.1.0) + actionview (4.1.0) + mail (2.5.4) + rails (400.33.22) + actionmailer (= 4.1.0) + actionpack (= 4.1.0) + actionview (= 4.1.0) + activemodel (= 4.1.0) + activerecord (= 4.1.0) + activesupport (= 4.1.0) + bundler (>= 1.3.0, < 2.0) + railties (= 4.1.0) + sprockets-rails (~> 2.0) + railties (400.33.22) PLATFORMS ruby @@ -131,9 +131,9 @@ remote: https://rubygems.org/ specs: actionmailer (4.1.0) - actionpack (= 4.1.0) - actionview (= 4.1.0) - mail (~> 2.5.4) + actionpack (4.1.0) + actionview (4.1.0) + mail (2.5.4) PLATFORMS ruby