Class: RuboCop::Cop::WaitFor::ConditionMet

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/wait_for/condition_met.rb

Overview

Evaluates annotated comments that specify a runtime condition and registers an offense once the condition evaluates to true.

It is designed to support use cases such as removing temporary code once a feature flag is enabled, a deprecation deadline is reached, or some environment-dependent behavior becomes active.

Examples:

# bad
# Assuming RUBY_VERSION is '3.4.4'
# wait-for Gem::Version.new(RUBY_VERSION) >= '3.4.0'
some_code_to_update_upon_upgrading_to_ruby34

# good
# Assuming RUBY_VERSION is '3.3.8'
# wait-for Gem::Version.new(RUBY_VERSION) >= '3.4.0'
some_code_to_update_upon_upgrading_to_ruby34

Constant Summary collapse

DIRECTIVE_PATTERN =
/#\s*(?:rubocop[:\-])?wait-for\s+(.+)/.freeze
MSG =
'Condition has been met.'

Instance Method Summary collapse

Instance Method Details

#external_dependency_checksumObject



29
30
31
# File 'lib/rubocop/cop/wait_for/condition_met.rb', line 29

def external_dependency_checksum
  Time.now.to_i.to_s if ENV['RUBOCOP_WAIT_FOR_CHECK_ALL'] == '1'
end

#on_new_investigationObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubocop/cop/wait_for/condition_met.rb', line 33

def on_new_investigation
  return if processed_source.buffer.source.empty?

  processed_source.comments.each do |comment|
    next unless (condition = DIRECTIVE_PATTERN.match(comment.text))

    result = evaluate_condition(condition.captures[0])

    add_offense(comment) if result == true
  end
end