module RuboCop::Cop::Style::MutableConstant::ShareableConstantValue

Handles magic comment shareable_constant_value with O(n ^ 2) complexity n - number of lines in the source Iterates over all lines before a CONSTANT until it reaches shareable_constant_value

Public Instance Methods

magic_comment_in_scope(node) click to toggle source

Identifies the most recent magic comment with valid shareable constant values that’s in scope for this node

# File lib/rubocop/cop/style/mutable_constant.rb, line 102
def magic_comment_in_scope(node)
  processed_source_till_node(node).reverse_each.find do |line|
    MagicComment.parse(line).valid_shareable_constant_value?
  end
end
recent_shareable_value?(node) click to toggle source
# File lib/rubocop/cop/style/mutable_constant.rb, line 91
def recent_shareable_value?(node)
  shareable_constant_comment = magic_comment_in_scope node
  return false if shareable_constant_comment.nil?

  shareable_constant_value = MagicComment.parse(shareable_constant_comment)
                                         .shareable_constant_value
  shareable_constant_value_enabled? shareable_constant_value
end

Private Instance Methods

processed_source_till_node(node) click to toggle source
# File lib/rubocop/cop/style/mutable_constant.rb, line 110
def processed_source_till_node(node)
  processed_source.lines[0..(node.last_line - 1)]
end
shareable_constant_value_enabled?(value) click to toggle source
# File lib/rubocop/cop/style/mutable_constant.rb, line 114
def shareable_constant_value_enabled?(value)
  %w[literal experimental_everything experimental_copy].include? value
end