rubocop-wait_for 0.1.1 → 0.2.1

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: '08126d86756d0151edd456f62eb9f40ff0089089d06338a6505734b91b8023a3'
4
- data.tar.gz: 2995fde28c3e4e50aa26c480a1f7f25c8079cf1cc99b4169487afbc969f07cfe
3
+ metadata.gz: bc356107f4623b869f2744a7129a6ff2b19e41d5364e6d5f1b01ac635e5015fa
4
+ data.tar.gz: c3315dc89ad9f8760b67caa07c816c04c62cabd7343c33d4aaf68f0a6163b68f
5
5
  SHA512:
6
- metadata.gz: 41b98558c352e332cae24e935987f87774e048c91229141d91b21a08f6bee5a4a0ec5193febe90d6d88b393dc7413692e5ac15ed6cf091c310984c83ca2a79c8
7
- data.tar.gz: f5909346a3e37c0f8e2391fe3fa889c3e156b32c3e03ddd35caee12359fe1039b268afd67a883ba0d638e7a75b19b566d8d491f035789ae074b9a3c83527bee7
6
+ metadata.gz: 5fd55f9eb3bfd4efb51a4d9512d64cf956257ac0242604e77afb78cf3082e07b053168c08b5ed354d3499d149d62be1009a77ac50b14e7301c0c360d2e4f93a7
7
+ data.tar.gz: 57fa72330e959a0821402ab3a30e2a2e5b6cdf7c8e6bfe649ac2bc87ecd290d1ed76b66f1bc84a62d3af001de165ae62f80e2594fad443f4cc899f36f033d96a
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.1] - 2025:08-20
4
+
5
+ - Fix `config/default.yml` configuration. [#6](https://github.com/viralpraxis/rubocop-wait_for/pull/6)
6
+
7
+ ## [0.2.0] - 2025-08-13
8
+
9
+ - Add support for statically evaluated gem version requirements. [#4](https://github.com/viralpraxis/rubocop-wait_for/pull/4)
10
+
3
11
  ## [0.1.0] - 2025-07-07
4
12
 
5
13
  - 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
data/config/default.yml CHANGED
@@ -1,6 +1,4 @@
1
- # Write it!
2
-
3
1
  WaitFor/ConditionMet:
4
- Description: 'TODO: Write a description of the cop.'
5
- Enabled: pending
6
- VersionAdded: '<<next>>'
2
+ Description: 'Checking if a condition evaluates to true.'
3
+ Enabled: true
4
+ VersionAdded: '0.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.1'
5
+ VERSION = '0.2.1'
6
6
  end
7
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.1
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yaroslav Kurbatov