123456789_123456789_123456789_123456789_123456789_

Module: RuboCop::AST::ConstantNode

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/rubocop/ast/node/mixin/constant_node.rb

Overview

Common functionality for nodes that deal with constants: const, casgn.

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#absolute?Boolean (readonly)

Returns:

  • (Boolean)

    if the constant starts with :: (aka s(:cbase))

[ GitHub ]

  
# File 'lib/rubocop/ast/node/mixin/constant_node.rb', line 27

def absolute?
  return false unless namespace

  each_path.first.cbase_type?
end

#class_name? (readonly)

Alias for #module_name?.

[ GitHub ]

  
# File 'lib/rubocop/ast/node/mixin/constant_node.rb', line 24

alias class_name? module_name?

#module_name?Boolean (readonly) Also known as: #class_name?

Returns:

  • (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

[ GitHub ]

  
# File 'lib/rubocop/ast/node/mixin/constant_node.rb', line 21

def module_name?
  short_name.match?(/[[:lower:]]/)
end

#relative?Boolean (readonly)

Returns:

  • (Boolean)

    if the constant does not start with :: (aka s(:cbase))

[ GitHub ]

  
# File 'lib/rubocop/ast/node/mixin/constant_node.rb', line 34

def relative?
  !absolute?
end

Instance Method Details

#each_path(&block)

Yield nodes for the namespace

For {::Foo::Bar::BAZ} => yields:
   s(:cbase), then
   s(:const, :Foo), then
   s(:const, s(:const, :Foo), :Bar)
[ GitHub ]

  
# File 'lib/rubocop/ast/node/mixin/constant_node.rb', line 44

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

#namespaceNode?

Returns:

  • (Node, nil)

    the node associated with the scope (e.g. cbase, const, …​)

[ GitHub ]

  
# File 'lib/rubocop/ast/node/mixin/constant_node.rb', line 9

def namespace
  children[0]
end

#short_nameSymbol

Returns:

  • (Symbol)

    the demodulized name of the constant: "::Foo::Bar" ⇒ :Bar

[ GitHub ]

  
# File 'lib/rubocop/ast/node/mixin/constant_node.rb', line 14

def short_name
  children[1]
end