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.



13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
# File 'lib/prism/node.rb', line 13839

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



13916
13917
13918
# File 'lib/prism/node.rb', line 13916

def body
  @body
end

#constant_pathObject (readonly)

attr_reader constant_path: ConstantReadNode | ConstantPathNode | MissingNode



13913
13914
13915
# File 'lib/prism/node.rb', line 13913

def constant_path
  @constant_path
end

#localsObject (readonly)

attr_reader locals: Array



13897
13898
13899
# File 'lib/prism/node.rb', line 13897

def locals
  @locals
end

#nameObject (readonly)

attr_reader name: Symbol



13932
13933
13934
# File 'lib/prism/node.rb', line 13932

def name
  @name
end

Class Method Details

.typeObject

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



13955
13956
13957
# File 'lib/prism/node.rb', line 13955

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.



13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
# File 'lib/prism/node.rb', line 13961

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



13853
13854
13855
# File 'lib/prism/node.rb', line 13853

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



13858
13859
13860
# File 'lib/prism/node.rb', line 13858

def child_nodes
  [constant_path, body]
end

#comment_targetsObject

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



13879
13880
13881
# File 'lib/prism/node.rb', line 13879

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



13871
13872
13873
13874
13875
13876
# File 'lib/prism/node.rb', line 13871

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



13884
13885
13886
# File 'lib/prism/node.rb', line 13884

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 }



13892
13893
13894
# File 'lib/prism/node.rb', line 13892

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

#each_child_node {|constant_path| ... } ⇒ Object

def each_child_node: () { (Prism::node) -> void } -> void | () -> Enumerator

Yields:



13863
13864
13865
13866
13867
13868
# File 'lib/prism/node.rb', line 13863

def each_child_node
  return to_enum(:each_child_node) unless block_given?

  yield constant_path
  yield body if body
end

#end_keywordObject

def end_keyword: () -> String



13940
13941
13942
# File 'lib/prism/node.rb', line 13940

def end_keyword
  end_keyword_loc.slice
end

#end_keyword_locObject

attr_reader end_keyword_loc: Location



13919
13920
13921
13922
13923
# File 'lib/prism/node.rb', line 13919

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



13945
13946
13947
# File 'lib/prism/node.rb', line 13945

def inspect
  InspectVisitor.compose(self)
end

#module_keywordObject

def module_keyword: () -> String



13935
13936
13937
# File 'lib/prism/node.rb', line 13935

def module_keyword
  module_keyword_loc.slice
end

#module_keyword_locObject

attr_reader module_keyword_loc: Location



13900
13901
13902
13903
13904
# File 'lib/prism/node.rb', line 13900

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.



13927
13928
13929
# File 'lib/prism/node.rb', line 13927

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.



13908
13909
13910
# File 'lib/prism/node.rb', line 13908

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`.



13950
13951
13952
# File 'lib/prism/node.rb', line 13950

def type
  :module_node
end