class RuboCop::AST::ConstNode

A node extension for `const` nodes.

Public Instance Methods

absolute?() click to toggle source

@return [Boolean] if the constant starts with `::` (aka s(:cbase))

# File lib/rubocop/ast/node/const_node.rb, line 26
def absolute?
  return false unless namespace

  each_path.first.cbase_type?
end
class_name?()
Alias for: module_name?
each_path(&block) click to toggle source

Yield nodes for the namespace

For `::Foo::Bar::BAZ` => yields:
   s(:cbase), then
   s(:const, :Foo), then
   s(:const, s(:const, :Foo), :Bar)
# File lib/rubocop/ast/node/const_node.rb, line 43
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
module_name?() click to toggle source

@return [Boolean] if the constant is a Module / Class, according to the standard convention.

Note: some classes might have uppercase in which case this method
      returns false
# File lib/rubocop/ast/node/const_node.rb, line 20
def module_name?
  short_name.match?(/[[:lower:]]/)
end
Also aliased as: class_name?
namespace() click to toggle source

@return [Node, nil] the node associated with the scope (e.g. cbase, const, …)

# File lib/rubocop/ast/node/const_node.rb, line 8
def namespace
  children[0]
end
relative?() click to toggle source

@return [Boolean] if the constant does not start with `::` (aka s(:cbase))

# File lib/rubocop/ast/node/const_node.rb, line 33
def relative?
  !absolute?
end
short_name() click to toggle source

@return [Symbol] the demodulized name of the constant: “::Foo::Bar” => :Bar

# File lib/rubocop/ast/node/const_node.rb, line 13
def short_name
  children[1]
end