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.



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

def initialize
  self.defined_constants = []
  self.const_stack = []
  self.aliases = {}
end

Instance Attribute Details

#aliasesObject

Returns the value of attribute aliases.



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

def aliases
  @aliases
end

#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



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubocop/helper/state.rb', line 31

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

  result = resolve_const(name, local_only: local_only)

  # If the name (or its leading segment) is an alias, resolve the alias target and
  # retry. e.g. with `Foo = Bar::Baz`, accessing `Foo::QUX` resolves `Bar::Baz::QUX`.
  if !result && (aliased = expand_alias(name))
    result = resolve_const(aliased, local_only: local_only)
  end

  return result
end

#const_assigned(const_name: nil, alias_target: nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rubocop/helper/state.rb', line 61

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

  return unless alias_target

  # Register the alias under both the fully-qualified name and the bare name so it can
  # be resolved whether referenced from inside or outside its enclosing namespace.
  self.aliases[full_name] = alias_target
  self.aliases[const_name.to_s] = alias_target
end

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



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rubocop/helper/state.rb', line 45

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



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

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



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

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



57
58
59
# File 'lib/rubocop/helper/state.rb', line 57

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