Class: Prism::ModuleNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
sig/generated/prism/node.rbs,
ext/prism/api_node.c

Overview

Represents a module declaration involving the module keyword.

module Foo end
^^^^^^^^^^^^^^

Explore code sample

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, locals, module_keyword_loc, constant_path, body, end_keyword_loc, name) ⇒ ModuleNode

Initialize a new ModuleNode node.

: (Source source, Integer node_id, Location location, Integer flags, Array locals, Location module_keyword_loc, (ConstantReadNode | ConstantPathNode) constant_path, (StatementsNode | BeginNode)? body, Location end_keyword_loc, Symbol name) -> void

Parameters:



23578
23579
23580
23581
23582
23583
23584
23585
23586
23587
23588
23589
# File 'lib/prism/node.rb', line 23578

def initialize(source, node_id, location, flags, locals, module_keyword_loc, constant_path, body, end_keyword_loc, name)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @locals = locals
  @module_keyword_loc = module_keyword_loc
  @constant_path = constant_path
  @body = body
  @end_keyword_loc = end_keyword_loc
  @name = name
end

Class Method Details

.type ⇒ :module_node

See Node.type.

: () -> :module_node

Returns:

  • (:module_node)


23679
23680
23681
# File 'lib/prism/node.rb', line 23679

def self.type
  :module_node
end

Instance Method Details

#===(other) ⇒ boolish

: (untyped other) -> boolish

Parameters:

  • other (Object)

Returns:

  • (boolish)


23799
23800
23801
23802
23803
23804
23805
23806
23807
23808
# File 'lib/prism/node.rb', line 23799

def ===(other) # :nodoc:
  other.is_a?(ModuleNode) &&
    (locals.length == other.locals.length) &&
    locals.zip(other.locals).all? { |left, right| left === right } &&
    (module_keyword_loc.nil? == other.module_keyword_loc.nil?) &&
    (constant_path === other.constant_path) &&
    (body === other.body) &&
    (end_keyword_loc.nil? == other.end_keyword_loc.nil?) &&
    (name === other.name)
end

#accept(visitor) ⇒ Object

See Node.accept.

: (_Visitor visitor) -> untyped

Parameters:

Returns:

  • (Object)


23605
23606
23607
# File 'lib/prism/node.rb', line 23605

def accept(visitor)
  visitor.visit_module_node(self)
end

#body ⇒ StatementsNode, ...

:call-seq:

body -> StatementsNode | BeginNode | nil

Returns the body attribute.

: () -> (StatementsNode | BeginNode)?

Returns:



23738
23739
23740
# File 'lib/prism/node.rb', line 23738

def body
  @body
end

#child_nodes ⇒ Array[node?] Also known as: deconstruct

See Node.child_nodes.

: () -> Array

Returns:

  • (Array[node?])


23612
23613
23614
# File 'lib/prism/node.rb', line 23612

def child_nodes
  [constant_path, body]
end

#compact_child_nodes ⇒ Array[node]

See Node.compact_child_nodes.

: () -> Array

Returns:

  • (Array[node])


23630
23631
23632
23633
23634
23635
# File 'lib/prism/node.rb', line 23630

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  compact << constant_path
  if (body = self.body); compact << body; end
  compact
end

#constant_path ⇒ ConstantReadNode, ConstantPathNode

:call-seq:

constant_path -> ConstantReadNode | ConstantPathNode

Returns the constant_path attribute.

: () -> (ConstantReadNode | ConstantPathNode)



23728
23729
23730
# File 'lib/prism/node.rb', line 23728

def constant_path
  @constant_path
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, locals: self.locals, module_keyword_loc: self.module_keyword_loc, constant_path: self.constant_path, body: self.body, end_keyword_loc: self.end_keyword_loc, name: self.name) ⇒ ModuleNode

:call-seq:

copy(**fields) -> ModuleNode

Creates a copy of self with the given fields, using self as the template.

: (?node_id: Integer, ?location: Location, ?flags: Integer, ?locals: Array, ?module_keyword_loc: Location, ?constant_path: (ConstantReadNode | ConstantPathNode), ?body: (StatementsNode | BeginNode)?, ?end_keyword_loc: Location, ?name: Symbol) -> ModuleNode

Parameters:

  • node_id: (Integer) (defaults to: self.node_id)
  • location: (Location) (defaults to: self.location)
  • flags: (Integer) (defaults to: self.flags)
  • locals: (Array[Symbol]) (defaults to: self.locals)
  • module_keyword_loc: (Location) (defaults to: self.module_keyword_loc)
  • constant_path: (ConstantReadNode, ConstantPathNode) (defaults to: self.constant_path)
  • body: (StatementsNode, BeginNode, nil) (defaults to: self.body)
  • end_keyword_loc: (Location) (defaults to: self.end_keyword_loc)
  • name: (Symbol) (defaults to: self.name)

Returns:



23643
23644
23645
# File 'lib/prism/node.rb', line 23643

def copy(node_id: self.node_id, location: self.location, flags: self.flags, locals: self.locals, module_keyword_loc: self.module_keyword_loc, constant_path: self.constant_path, body: self.body, end_keyword_loc: self.end_keyword_loc, name: self.name)
  ModuleNode.new(source, node_id, location, flags, locals, module_keyword_loc, constant_path, body, end_keyword_loc, name)
end

#deconstruct_keys(keys) ⇒ Hash[Symbol, untyped]

: (Array? keys) -> Hash[Symbol, untyped]

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


23650
23651
23652
23653
23654
23655
23656
23657
23658
23659
23660
23661
23662
23663
23664
23665
23666
23667
# File 'lib/prism/node.rb', line 23650

def deconstruct_keys(keys) # :nodoc:
  (keys || i[node_id location locals module_keyword_loc constant_path body end_keyword_loc name module_keyword end_keyword]).each_with_object(
    {} #: Hash[Symbol, untyped]
  ) do |key, deconstructed|
    case key
    when :node_id                 then deconstructed[:node_id] = self.node_id
    when :location                then deconstructed[:location] = self.location
    when :locals                  then deconstructed[:locals] = self.locals
    when :module_keyword_loc      then deconstructed[:module_keyword_loc] = self.module_keyword_loc
    when :constant_path           then deconstructed[:constant_path] = self.constant_path
    when :body                    then deconstructed[:body] = self.body
    when :end_keyword_loc         then deconstructed[:end_keyword_loc] = self.end_keyword_loc
    when :name                    then deconstructed[:name] = self.name
    when :module_keyword          then deconstructed[:module_keyword] = self.module_keyword
    when :end_keyword             then deconstructed[:end_keyword] = self.end_keyword
    end
  end
end

#each_child_node ⇒ Enumerator[node, void] #each_child_node ⇒ void

See Node.each_child_node.

: () -> Enumerator[node, void] : () { (node) -> void } -> void

Overloads:

  • #each_child_node ⇒ Enumerator[node, void]

    Returns:

    • (Enumerator[node, void])
  • #each_child_node ⇒ void

    This method returns an undefined value.

Yields:

Yield Parameters:

  • arg0 (node)

Yield Returns:

  • (void)


23620
23621
23622
23623
23624
23625
# File 'lib/prism/node.rb', line 23620

def each_child_node(&blk)
  return to_enum(:each_child_node) unless block_given?

  yield constant_path
  if (body = self.body); yield body; end
end

#end_keyword ⇒ String

:call-seq:

end_keyword -> String

Slice the location of end_keyword_loc from the source.

: () -> String

Returns:

  • (String)


23792
23793
23794
# File 'lib/prism/node.rb', line 23792

def end_keyword
  end_keyword_loc.slice
end

#end_keyword_loc ⇒ Location

:category: Locations :call-seq:

end_keyword_loc -> Location

Returns the Location represented by end_keyword_loc.

: () -> Location

Returns:



23749
23750
23751
23752
23753
# File 'lib/prism/node.rb', line 23749

def end_keyword_loc
  location = @end_keyword_loc
  return location if location.is_a?(Location)
  @end_keyword_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#inspect ⇒ String

: () -> String

Returns:

  • (String)


23684
23685
23686
# File 'lib/prism/node.rb', line 23684

def inspect # :nodoc:
  InspectVisitor.compose(self)
end

#locals ⇒ Array[Symbol]

:call-seq:

locals -> Array[Symbol]

Returns the locals attribute.

: () -> Array

Returns:

  • (Array[Symbol])


23696
23697
23698
# File 'lib/prism/node.rb', line 23696

def locals
  @locals
end

#module_keyword ⇒ String

:call-seq:

module_keyword -> String

Slice the location of module_keyword_loc from the source.

: () -> String

Returns:

  • (String)


23782
23783
23784
# File 'lib/prism/node.rb', line 23782

def module_keyword
  module_keyword_loc.slice
end

#module_keyword_loc ⇒ Location

:category: Locations :call-seq:

module_keyword_loc -> Location

Returns the Location represented by module_keyword_loc.

: () -> Location

Returns:



23707
23708
23709
23710
23711
# File 'lib/prism/node.rb', line 23707

def module_keyword_loc
  location = @module_keyword_loc
  return location if location.is_a?(Location)
  @module_keyword_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#name ⇒ Symbol

:call-seq:

name -> Symbol

Returns the name attribute.

: () -> Symbol

Returns:

  • (Symbol)


23770
23771
23772
# File 'lib/prism/node.rb', line 23770

def name
  @name
end

#save_end_keyword_loc(repository) ⇒ Relocation::Entry

:category: Repository Save the end_keyword_loc location using the given saved source so that it can be retrieved later.

: (_Repository repository) -> Relocation::Entry

Parameters:

Returns:



23760
23761
23762
# File 'lib/prism/node.rb', line 23760

def save_end_keyword_loc(repository)
  repository.enter(node_id, :end_keyword_loc)
end

#save_module_keyword_loc(repository) ⇒ Relocation::Entry

:category: Repository Save the module_keyword_loc location using the given saved source so that it can be retrieved later.

: (_Repository repository) -> Relocation::Entry

Parameters:

Returns:



23718
23719
23720
# File 'lib/prism/node.rb', line 23718

def save_module_keyword_loc(repository)
  repository.enter(node_id, :module_keyword_loc)
end

#type ⇒ :module_node

See Node#type.

: () -> :module_node

Returns:

  • (:module_node)


23672
23673
23674
# File 'lib/prism/node.rb', line 23672

def type
  :module_node
end