Class: RuboCop::RequireTools::State

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/helper/state.rb

Overview

Contains current state of an inspected file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeState

Returns a new instance of State.



8
9
10
11
# File 'lib/rubocop/helper/state.rb', line 8

def initialize
  self.defined_constants = []
  self.const_stack = []
end

Instance Attribute Details

#const_stackObject

Returns the value of attribute const_stack.



6
7
8
# File 'lib/rubocop/helper/state.rb', line 6

def const_stack
  @const_stack
end

#defined_constantsObject

Returns the value of attribute defined_constants.



5
6
7
# File 'lib/rubocop/helper/state.rb', line 5

def defined_constants
  @defined_constants
end

Instance Method Details

#access_const(const_name: nil, local_only: false) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rubocop/helper/state.rb', line 29

def access_const(const_name: nil, local_only: false)
  name = const_name.to_s.sub(/^:*/, '').sub(/:*$/, '') # Strip leading/trailing ::

  # If const_stack is ["A", "B", "C"] all of A, A::B, A::B::C are valid lookup combinations
  prefixes = self.const_stack.reduce([]) { |a, c| a << [a.last, c].compact.join('::') }

  # I use const_get here because in testing const_get and const_defined? have yielded different results
  unless local_only
    result = Object.const_get(name) rescue nil                                                   # Defined elsewhere, top-level
    result ||= self.defined_constants.find { |c| Object.const_get("#{c}::#{name}") rescue nil }  # Defined elsewhere, nested
  end

  result ||= self.defined_constants.find { |c| name == c }                                       # Defined in this file, other module/class
  prefixes.each do |prefix|
    result ||= self.defined_constants.find { |c| [name, "#{prefix}::#{name}"].include? c }       # Defined in this file, other module/class
    result ||= prefix == name                                                                    # Defined in this file, in current module/class
  end

  return result
end

#const_assigned(const_name: nil) ⇒ Object



66
67
68
69
70
# File 'lib/rubocop/helper/state.rb', line 66

def const_assigned(const_name: nil)
  full_name = (self.const_stack + [const_name]).join('::')
  self.defined_constants << full_name
  self.defined_constants.uniq!
end

#define_const(const_name: nil, is_part_of_stack: true) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rubocop/helper/state.rb', line 50

def define_const(const_name: nil, is_part_of_stack: true)
  new = []
  self.defined_constants.each do |c|
    found = Object.const_get("#{c}::#{const_name}") rescue nil
    new << found.to_s if found
  end
  self.defined_constants.push(*new)
  self.const_stack.push(const_name) if is_part_of_stack
  self.defined_constants.push(const_name.to_s, self.const_stack.join('::'))
  self.defined_constants.uniq!
end

#require(file: nil) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/rubocop/helper/state.rb', line 13

def require(file: nil)
  Kernel.require(file)
rescue NameError, LoadError => ex
  puts "Note: Could not load #{file}:"
  puts ex.message
  puts 'Check your dependencies, they could be circular'
end

#require_relative(relative_path: nil) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/rubocop/helper/state.rb', line 21

def require_relative(relative_path: nil)
  Kernel.require_relative(relative_path)
rescue NameError, LoadError => ex
  puts "Note: Could not load relative file #{relative_path}:"
  puts ex.message
  puts 'Check your dependencies, they could be circular'
end

#undefine_const(const_name: nil) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



62
63
64
# File 'lib/rubocop/helper/state.rb', line 62

def undefine_const(const_name: nil) # rubocop:disable Lint/UnusedMethodArgument
  self.const_stack.pop
end