Class: Prism::ModuleNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::ModuleNode
- 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
-
#body ⇒ Object
readonly
attr_reader body: Node?.
-
#constant_path ⇒ Object
readonly
attr_reader constant_path: Node.
-
#end_keyword_loc ⇒ Object
readonly
attr_reader end_keyword_loc: Location.
-
#locals ⇒ Object
readonly
attr_reader locals: Array.
-
#module_keyword_loc ⇒ Object
readonly
attr_reader module_keyword_loc: Location.
-
#name ⇒ Object
readonly
attr_reader name: Symbol.
Class Method Summary collapse
-
.type ⇒ Object
Similar to #type, this method returns a symbol that you can use for splitting on the type of the node without having to do a long === chain.
Instance Method Summary collapse
-
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void.
-
#child_nodes ⇒ Object
(also: #deconstruct)
def child_nodes: () -> Array[nil | Node].
-
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location].
-
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array.
-
#copy(**params) ⇒ Object
def copy: (**params) -> ModuleNode.
- #deconstruct_keys(keys) ⇒ Object
-
#end_keyword ⇒ Object
def end_keyword: () -> String.
-
#initialize(locals, module_keyword_loc, constant_path, body, end_keyword_loc, name, location) ⇒ ModuleNode
constructor
def initialize: (locals: Array, module_keyword_loc: Location, constant_path: Node, body: Node?, end_keyword_loc: Location, name: Symbol, location: Location) -> void.
-
#inspect(inspector = NodeInspector.new) ⇒ Object
def inspect(inspector: NodeInspector) -> String.
-
#module_keyword ⇒ Object
def module_keyword: () -> String.
-
#type ⇒ Object
Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform.
Constructor Details
#initialize(locals, module_keyword_loc, constant_path, body, end_keyword_loc, name, location) ⇒ ModuleNode
def initialize: (locals: Array, module_keyword_loc: Location, constant_path: Node, body: Node?, end_keyword_loc: Location, name: Symbol, location: Location) -> void
11474 11475 11476 11477 11478 11479 11480 11481 11482 |
# File 'lib/prism/node.rb', line 11474 def initialize(locals, module_keyword_loc, constant_path, body, end_keyword_loc, name, location) @locals = locals @module_keyword_loc = module_keyword_loc @constant_path = constant_path @body = body @end_keyword_loc = end_keyword_loc @name = name @location = location end |
Instance Attribute Details
#body ⇒ Object (readonly)
attr_reader body: Node?
11465 11466 11467 |
# File 'lib/prism/node.rb', line 11465 def body @body end |
#constant_path ⇒ Object (readonly)
attr_reader constant_path: Node
11462 11463 11464 |
# File 'lib/prism/node.rb', line 11462 def constant_path @constant_path end |
#end_keyword_loc ⇒ Object (readonly)
attr_reader end_keyword_loc: Location
11468 11469 11470 |
# File 'lib/prism/node.rb', line 11468 def end_keyword_loc @end_keyword_loc end |
#locals ⇒ Object (readonly)
attr_reader locals: Array
11456 11457 11458 |
# File 'lib/prism/node.rb', line 11456 def locals @locals end |
#module_keyword_loc ⇒ Object (readonly)
attr_reader module_keyword_loc: Location
11459 11460 11461 |
# File 'lib/prism/node.rb', line 11459 def module_keyword_loc @module_keyword_loc end |
#name ⇒ Object (readonly)
attr_reader name: Symbol
11471 11472 11473 |
# File 'lib/prism/node.rb', line 11471 def name @name end |
Class Method Details
.type ⇒ Object
Similar to #type, this method returns a symbol that you can use for splitting on the type of the node without having to do a long === chain. Note that like #type, it will still be slower than using == for a single class, but should be faster in a case statement or an array comparison.
def self.type: () -> Symbol
11580 11581 11582 |
# File 'lib/prism/node.rb', line 11580 def self.type :module_node end |
Instance Method Details
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void
11485 11486 11487 |
# File 'lib/prism/node.rb', line 11485 def accept(visitor) visitor.visit_module_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array[nil | Node]
11490 11491 11492 |
# File 'lib/prism/node.rb', line 11490 def child_nodes [constant_path, body] end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
11503 11504 11505 |
# File 'lib/prism/node.rb', line 11503 def comment_targets [module_keyword_loc, constant_path, *body, end_keyword_loc] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
11495 11496 11497 11498 11499 11500 |
# File 'lib/prism/node.rb', line 11495 def compact_child_nodes compact = [] compact << constant_path compact << body if body compact end |
#copy(**params) ⇒ Object
def copy: (**params) -> ModuleNode
11508 11509 11510 11511 11512 11513 11514 11515 11516 11517 11518 |
# File 'lib/prism/node.rb', line 11508 def copy(**params) ModuleNode.new( params.fetch(:locals) { locals }, params.fetch(:module_keyword_loc) { module_keyword_loc }, params.fetch(:constant_path) { constant_path }, params.fetch(:body) { body }, params.fetch(:end_keyword_loc) { end_keyword_loc }, params.fetch(:name) { name }, params.fetch(:location) { location }, ) end |
#deconstruct_keys(keys) ⇒ Object
11524 11525 11526 |
# File 'lib/prism/node.rb', line 11524 def deconstruct_keys(keys) { locals: locals, module_keyword_loc: module_keyword_loc, constant_path: constant_path, body: body, end_keyword_loc: end_keyword_loc, name: name, location: location } end |
#end_keyword ⇒ Object
def end_keyword: () -> String
11534 11535 11536 |
# File 'lib/prism/node.rb', line 11534 def end_keyword end_keyword_loc.slice end |
#inspect(inspector = NodeInspector.new) ⇒ Object
def inspect(inspector: NodeInspector) -> String
11539 11540 11541 11542 11543 11544 11545 11546 11547 11548 11549 11550 11551 11552 11553 11554 |
# File 'lib/prism/node.rb', line 11539 def inspect(inspector = NodeInspector.new) inspector << inspector.header(self) inspector << "├── locals: #{locals.inspect}\n" inspector << "├── module_keyword_loc: #{inspector.location(module_keyword_loc)}\n" inspector << "├── constant_path:\n" inspector << inspector.child_node(constant_path, "│ ") if (body = self.body).nil? inspector << "├── body: ∅\n" else inspector << "├── body:\n" inspector << body.inspect(inspector.child_inspector("│ ")).delete_prefix(inspector.prefix) end inspector << "├── end_keyword_loc: #{inspector.location(end_keyword_loc)}\n" inspector << "└── name: #{name.inspect}\n" inspector.to_str end |
#module_keyword ⇒ Object
def module_keyword: () -> String
11529 11530 11531 |
# File 'lib/prism/node.rb', line 11529 def module_keyword module_keyword_loc.slice end |
#type ⇒ Object
Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform. Usually this is done by calling ‘[cls1, cls2].include?(node.class)` or putting the node into a case statement and doing `case node; when cls1; when cls2; end`. Both of these approaches are relatively slow because of the constant lookups, method calls, and/or array allocations.
Instead, you can call #type, which will return to you a symbol that you can use for comparison. This is faster than the other approaches because it uses a single integer comparison, but also because if you’re on CRuby you can take advantage of the fact that case statements with all symbol keys will use a jump table.
def type: () -> Symbol
11570 11571 11572 |
# File 'lib/prism/node.rb', line 11570 def type :module_node end |