Class: Prism::ModuleNode

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

Overview

Represents a module declaration involving the ‘module` keyword.

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

Instance Attribute Summary collapse

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.



12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
# File 'lib/prism/node.rb', line 12880

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

Instance Attribute Details

#bodyObject (readonly)

attr_reader body: StatementsNode | BeginNode | nil



12949
12950
12951
# File 'lib/prism/node.rb', line 12949

def body
  @body
end

#constant_pathObject (readonly)

attr_reader constant_path: ConstantReadNode | ConstantPathNode | MissingNode



12946
12947
12948
# File 'lib/prism/node.rb', line 12946

def constant_path
  @constant_path
end

#localsObject (readonly)

attr_reader locals: Array



12930
12931
12932
# File 'lib/prism/node.rb', line 12930

def locals
  @locals
end

#nameObject (readonly)

attr_reader name: Symbol



12965
12966
12967
# File 'lib/prism/node.rb', line 12965

def name
  @name
end

Class Method Details

.typeObject

Return a symbol representation of this node type. See ‘Node::type`.



12988
12989
12990
# File 'lib/prism/node.rb', line 12988

def self.type
  :module_node
end

Instance Method Details

#===(other) ⇒ Object

Implements case-equality for the node. This is effectively == but without comparing the value of locations. Locations are checked only for presence.



12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
# File 'lib/prism/node.rb', line 12994

def ===(other)
  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

def accept: (Visitor visitor) -> void



12894
12895
12896
# File 'lib/prism/node.rb', line 12894

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array[nil | Node]



12899
12900
12901
# File 'lib/prism/node.rb', line 12899

def child_nodes
  [constant_path, body]
end

#comment_targetsObject

def comment_targets: () -> Array[Node | Location]



12912
12913
12914
# File 'lib/prism/node.rb', line 12912

def comment_targets
  [module_keyword_loc, constant_path, *body, end_keyword_loc] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



12904
12905
12906
12907
12908
12909
# File 'lib/prism/node.rb', line 12904

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  compact << constant_path
  compact << body if body
  compact
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) ⇒ Object

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



12917
12918
12919
# File 'lib/prism/node.rb', line 12917

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) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, locals: Array, module_keyword_loc: Location, constant_path: ConstantReadNode | ConstantPathNode | MissingNode, body: StatementsNode | BeginNode | nil, end_keyword_loc: Location, name: Symbol }



12925
12926
12927
# File 'lib/prism/node.rb', line 12925

def deconstruct_keys(keys)
  { node_id: node_id, location: location, locals: locals, module_keyword_loc: module_keyword_loc, constant_path: constant_path, body: body, end_keyword_loc: end_keyword_loc, name: name }
end

#end_keywordObject

def end_keyword: () -> String



12973
12974
12975
# File 'lib/prism/node.rb', line 12973

def end_keyword
  end_keyword_loc.slice
end

#end_keyword_locObject

attr_reader end_keyword_loc: Location



12952
12953
12954
12955
12956
# File 'lib/prism/node.rb', line 12952

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

#inspectObject

def inspect -> String



12978
12979
12980
# File 'lib/prism/node.rb', line 12978

def inspect
  InspectVisitor.compose(self)
end

#module_keywordObject

def module_keyword: () -> String



12968
12969
12970
# File 'lib/prism/node.rb', line 12968

def module_keyword
  module_keyword_loc.slice
end

#module_keyword_locObject

attr_reader module_keyword_loc: Location



12933
12934
12935
12936
12937
# File 'lib/prism/node.rb', line 12933

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

#save_end_keyword_loc(repository) ⇒ Object

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



12960
12961
12962
# File 'lib/prism/node.rb', line 12960

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

#save_module_keyword_loc(repository) ⇒ Object

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



12941
12942
12943
# File 'lib/prism/node.rb', line 12941

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

#typeObject

Return a symbol representation of this node type. See ‘Node#type`.



12983
12984
12985
# File 'lib/prism/node.rb', line 12983

def type
  :module_node
end