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.



13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
# File 'lib/prism/node.rb', line 13033

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



13102
13103
13104
# File 'lib/prism/node.rb', line 13102

def body
  @body
end

#constant_pathObject (readonly)

attr_reader constant_path: ConstantReadNode | ConstantPathNode | MissingNode



13099
13100
13101
# File 'lib/prism/node.rb', line 13099

def constant_path
  @constant_path
end

#localsObject (readonly)

attr_reader locals: Array



13083
13084
13085
# File 'lib/prism/node.rb', line 13083

def locals
  @locals
end

#nameObject (readonly)

attr_reader name: Symbol



13118
13119
13120
# File 'lib/prism/node.rb', line 13118

def name
  @name
end

Class Method Details

.typeObject

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



13141
13142
13143
# File 'lib/prism/node.rb', line 13141

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.



13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
# File 'lib/prism/node.rb', line 13147

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



13047
13048
13049
# File 'lib/prism/node.rb', line 13047

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



13052
13053
13054
# File 'lib/prism/node.rb', line 13052

def child_nodes
  [constant_path, body]
end

#comment_targetsObject

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



13065
13066
13067
# File 'lib/prism/node.rb', line 13065

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



13057
13058
13059
13060
13061
13062
# File 'lib/prism/node.rb', line 13057

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



13070
13071
13072
# File 'lib/prism/node.rb', line 13070

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 }



13078
13079
13080
# File 'lib/prism/node.rb', line 13078

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



13126
13127
13128
# File 'lib/prism/node.rb', line 13126

def end_keyword
  end_keyword_loc.slice
end

#end_keyword_locObject

attr_reader end_keyword_loc: Location



13105
13106
13107
13108
13109
# File 'lib/prism/node.rb', line 13105

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



13131
13132
13133
# File 'lib/prism/node.rb', line 13131

def inspect
  InspectVisitor.compose(self)
end

#module_keywordObject

def module_keyword: () -> String



13121
13122
13123
# File 'lib/prism/node.rb', line 13121

def module_keyword
  module_keyword_loc.slice
end

#module_keyword_locObject

attr_reader module_keyword_loc: Location



13086
13087
13088
13089
13090
# File 'lib/prism/node.rb', line 13086

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.



13113
13114
13115
# File 'lib/prism/node.rb', line 13113

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.



13094
13095
13096
# File 'lib/prism/node.rb', line 13094

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



13136
13137
13138
# File 'lib/prism/node.rb', line 13136

def type
  :module_node
end