class RuboCop::AST::ConstNode

A node extension for ‘const` nodes.

def absolute?

Returns:
  • (Boolean) - if the constant starts with `::` (aka s(:cbase))
def absolute?
  return false unless namespace
  each_path.first.cbase_type?
end

def each_path(&block)

s(:const, s(:const, :Foo), :Bar)
s(:const, :Foo), then
s(:cbase), then
For `::Foo::Bar::BAZ` => yields:

Yield nodes for the namespace
def each_path(&block)
  return to_enum(__method__) unless block
  descendants = []
  last = self
  loop do
    last = last.children.first
    break if last.nil?
    descendants << last
    break unless last.const_type?
  end
  descendants.reverse_each(&block)
  self
end

def module_name?

Returns:
  • (Boolean) - if the constant is a Module / Class, according to the standard convention.
def module_name?
  short_name.match?(/[[:lower:]]/)
end

def namespace

Returns:
  • (Node, nil) - the node associated with the scope (e.g. cbase, const, ...)
def namespace
  children[0]
end

def relative?

Returns:
  • (Boolean) - if the constant does not start with `::` (aka s(:cbase))
def relative?
  !absolute?
end

def short_name

Returns:
  • (Symbol) - the demodulized name of the constant: "::Foo::Bar" => :Bar
def short_name
  children[1]
end