123456789_123456789_123456789_123456789_123456789_

Module: RuboCop::Cop::Style::MutableConstant::ShareableConstantValue

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/rubocop/cop/style/mutable_constant.rb

Overview

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

Class Method Summary

Class Method Details

.magic_comment_in_scope(node) (mod_func)

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

[ GitHub ]

  
# 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

.processed_source_till_node(node) (private, mod_func)

[ GitHub ]

  
# 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

.recent_shareable_value?(node) ⇒ Boolean (mod_func)

[ GitHub ]

  
# 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

.shareable_constant_value_enabled?(value) ⇒ Boolean (private, mod_func)

[ GitHub ]

  
# 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