rubocop-wait_for 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2838e7a28daeb2b42c69f22aa9a355d4c1b8121a20418a34f0c4e32eb0f92c0f
4
- data.tar.gz: 49c4575ac16e5ac806c04ec6d7233dde8e1b714afa4a4cc7f1244b35fddbcdd2
3
+ metadata.gz: 23ddd9507b68778bdaa0eeb610b0329012cec58a31f09daed27299b9c12d516a
4
+ data.tar.gz: ef6c94d9f0610586aab0c989905d8605fef4213a2fd4c66085a793003f65739c
5
5
  SHA512:
6
- metadata.gz: fc900f5b918bf09c8677f3536bba3afd463477439976779622cb9924fb51189871648e0d834c9b30962e56323683ed54d513dd8810abb0fac58d51e563c11ff1
7
- data.tar.gz: 3481d3386f03e303c75c443995af1d9ad025f4016b8a3e48e110dd0b3c03475bd075f62a03e0e20813ec81c79022a022f682144ba71b4362eb9bcb7900c03379
6
+ metadata.gz: 7b1fbbc872062f81acae711cc4a9eb0509c7b905e0062c261b236dc990b447ca4b94df8dc3fef148dba1e7b36ee9f384d39617f7dea50cb33b45bece2da46b37
7
+ data.tar.gz: c0534dcae8cb5d69d78de13a42b820137641d532d7931a79d7d2d11444834258c08b45238038a4bd951a459a903d51053cbc2281e6d570c055cbb77b1f45828f
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2025-08-13
4
+
5
+ - Add support for statically evaluated gem version requirements. [#4](https://github.com/viralpraxis/rubocop-wait_for/pull/4)
6
+
3
7
  ## [0.1.0] - 2025-07-07
4
8
 
5
9
  - Initial release.
data/README.md CHANGED
@@ -60,7 +60,7 @@ some_code_to_remove_once_ruby34_is_in_use()
60
60
 
61
61
  > [!NOTE]
62
62
  >
63
- > Use can alias use an alias `rubocop-wait-for <code>`
63
+ > Use can use an alias `# rubocop-wait-for <code>`
64
64
 
65
65
  If the condition inside the comment evaluates to `true` at the time of linting, RuboCop registers an offense.
66
66
 
@@ -75,6 +75,24 @@ This enables workflows like:
75
75
  > [!WARNING]
76
76
  > All the code found after the directive is passed directly to `Kernel.eval`. Use with caution.
77
77
 
78
+ Another situation this cop might be useful in is checking gem version.
79
+
80
+ You can achieve it by using a special form of the magic comment:
81
+
82
+ ```ruby
83
+ # wait-for gem-version rails '>= 8.1'
84
+ ```
85
+
86
+ This condition evaluates to `true` when the detected Rails version is at least `8.1`.
87
+
88
+ Note that gem versions are determined statically using RuboCop’s [built-in feature](https://docs.rubocop.org/rubocop/development.html#limit-by-ruby-or-gem-versions).
89
+
90
+ You can also use multiple version requirements:
91
+
92
+ ```ruby
93
+ # wait-for gem-version rails '>= 8.1' '< 8.4'
94
+ ```
95
+
78
96
  ### Caveats
79
97
 
80
98
  1. Missing dependencies
@@ -82,8 +100,8 @@ This enables workflows like:
82
100
  RuboCop does not automatically load project dependencies from your `Gemfile.lock`. If your condition relies on gems like Rails, you may need to require them manually:
83
101
 
84
102
  ```ruby
85
- # wait-for require 'rails/gem_version'; Rails.gem_version >= Gem::Version.new('9.0.0')
86
- some_code_to_remove_once_rails90_is_in_use()
103
+ # wait-for require 'rails'; defined?(::Rails.some_new_feature) != nil
104
+ some_code_to_remove_once_new_rails_feature_is_available()
87
105
  ```
88
106
 
89
107
  2. Caching
@@ -24,7 +24,7 @@ module RuboCop
24
24
  DIRECTIVE_PATTERN = /#\s*(?:rubocop[:\-])?wait-for\s+(.+)/.freeze
25
25
  private_constant(*constants(false))
26
26
 
27
- MSG = 'condition has been met.'
27
+ MSG = 'Condition has been met.'
28
28
 
29
29
  def external_dependency_checksum
30
30
  Time.now.to_i.to_s if ENV['RUBOCOP_WAIT_FOR_CHECK_ALL'] == '1'
@@ -45,11 +45,38 @@ module RuboCop
45
45
  private
46
46
 
47
47
  def evaluate_condition(condition)
48
- Kernel.eval(condition) # rubocop:disable Security/Eval
49
- rescue Exception => e # rubocop:disable Lint/RescueException
50
- Kernel.warn "#{self.class.name}: Encountered exception during evaluating condition: #{e.message}"
48
+ if (gem_condition = gem_version_condition(condition))
49
+ gem_version_requirement_met?(gem_condition)
50
+ else
51
+ begin
52
+ Kernel.eval(condition) # rubocop:disable Security/Eval
53
+ rescue Exception => e # rubocop:disable Lint/RescueException
54
+ Kernel.warn "#{self.class.name}: Encountered exception during evaluating condition: #{e.message}"
51
55
 
52
- false
56
+ false
57
+ end
58
+ end
59
+ end
60
+
61
+ def gem_version_condition(condition)
62
+ unless (match_data = /\Agem-version\s+([a-zA-Z0-9_-]+)\s+((?:['"][^'"]+['"]\s*)+)\z/i.match(condition.strip))
63
+ return
64
+ end
65
+
66
+ {
67
+ gem_name: match_data[1],
68
+ requirements: Gem::Requirement.new(match_data[2].scan(/['"]([^'"]+)['"]/).flatten)
69
+ }
70
+ end
71
+
72
+ def gem_version_requirement_met?(gem_version_requirement)
73
+ all_gem_versions_in_target = @config.gem_versions_in_target
74
+ return false unless all_gem_versions_in_target
75
+
76
+ gem_version_in_target = all_gem_versions_in_target[gem_version_requirement.fetch(:gem_name)]
77
+ return false unless gem_version_in_target
78
+
79
+ gem_version_requirement.fetch(:requirements).satisfied_by?(gem_version_in_target)
53
80
  end
54
81
  end
55
82
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module WaitFor
5
- VERSION = '0.1.0'
5
+ VERSION = '0.2.0'
6
6
  end
7
7
  end
@@ -3,8 +3,5 @@
3
3
  require_relative 'wait_for/version'
4
4
 
5
5
  module RuboCop
6
- module WaitFor
7
- class Error < StandardError; end
8
- # Your code goes here...
9
- end
6
+ module WaitFor; end # rubocop:disable Style/Documentation
10
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-wait_for
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yaroslav Kurbatov