Parent

Files

Class/Module Index [+]

Quicksearch

RuboCop::Cop::VariableForce::Variable

A Variable represents existance of a local variable. This holds a variable declaration node, and some states of the variable.

Attributes

assignments[R]
captured_by_block[R]
captured_by_block?[R]
declaration_node[R]
name[R]
references[R]
scope[R]

Public Class Methods

new(name, declaration_node, scope) click to toggle source
# File lib/rubocop/cop/variable_force/variable.rb, line 16
def initialize(name, declaration_node, scope)
  unless VARIABLE_DECLARATION_TYPES.include?(declaration_node.type)
    fail ArgumentError,
         "Node type must be any of #{VARIABLE_DECLARATION_TYPES}, "                   "passed #{declaration_node.type}"
  end

  @name = name.to_sym
  @declaration_node = declaration_node
  @scope = scope

  @assignments = []
  @references = []
  @captured_by_block = false
end

Public Instance Methods

argument?() click to toggle source
# File lib/rubocop/cop/variable_force/variable.rb, line 78
def argument?
  ARGUMENT_DECLARATION_TYPES.include?(@declaration_node.type)
end
assign(node) click to toggle source
# File lib/rubocop/cop/variable_force/variable.rb, line 32
def assign(node)
  @assignments << Assignment.new(node, self)
end
block_argument?() click to toggle source
# File lib/rubocop/cop/variable_force/variable.rb, line 86
def block_argument?
  argument? && @scope.node.type == :block
end
block_local_variable?() click to toggle source
# File lib/rubocop/cop/variable_force/variable.rb, line 90
def block_local_variable?
  @declaration_node.type == BLOCK_LOCAL_VARIABLE_DECLARATION_TYPE
end
capture_with_block!() click to toggle source
# File lib/rubocop/cop/variable_force/variable.rb, line 62
def capture_with_block!
  @captured_by_block = true
end
method_argument?() click to toggle source
# File lib/rubocop/cop/variable_force/variable.rb, line 82
def method_argument?
  argument? && [:def, :defs].include?(@scope.node.type)
end
reference!(node) click to toggle source
# File lib/rubocop/cop/variable_force/variable.rb, line 40
def reference!(node)
  reference = Reference.new(node, @scope)
  @references << reference
  consumed_branch_ids = Set.new

  @assignments.reverse_each do |assignment|
    next if consumed_branch_ids.include?(assignment.branch_id)

    assignment.reference!

    if assignment.inside_of_branch?
      break if assignment.branch_id == reference.branch_id

      unless assignment.reference_penetrable?
        consumed_branch_ids << assignment.branch_id
      end
    else
      break
    end
  end
end
referenced?() click to toggle source
# File lib/rubocop/cop/variable_force/variable.rb, line 36
def referenced?
  !@references.empty?
end
used?() click to toggle source

This is a convenient way to check whether the variable is used in its entire variable lifetime. For more precise usage check, refer Assignment#used?.

Once the variable is captured by a block, we have no idea when, where and how many times the block would be invoked and it means we cannot track the usage of the variable. So we consider it's used to suppress false positive offenses.

# File lib/rubocop/cop/variable_force/variable.rb, line 74
def used?
  @captured_by_block || referenced?
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.