Class: Prism::DefNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::DefNode
- Defined in:
- lib/prism/node.rb,
ext/prism/api_node.c
Overview
Represents a method definition.
def method
end
^^^^^^^^^^
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
attr_reader body: Node?.
-
#def_keyword_loc ⇒ Object
readonly
attr_reader def_keyword_loc: Location.
-
#end_keyword_loc ⇒ Object
readonly
attr_reader end_keyword_loc: Location?.
-
#equal_loc ⇒ Object
readonly
attr_reader equal_loc: Location?.
-
#locals ⇒ Object
readonly
attr_reader locals: Array.
-
#lparen_loc ⇒ Object
readonly
attr_reader lparen_loc: Location?.
-
#name ⇒ Object
readonly
attr_reader name: Symbol.
-
#name_loc ⇒ Object
readonly
attr_reader name_loc: Location.
-
#operator_loc ⇒ Object
readonly
attr_reader operator_loc: Location?.
-
#parameters ⇒ Object
readonly
attr_reader parameters: ParametersNode?.
-
#receiver ⇒ Object
readonly
attr_reader receiver: Node?.
-
#rparen_loc ⇒ Object
readonly
attr_reader rparen_loc: Location?.
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) -> DefNode.
- #deconstruct_keys(keys) ⇒ Object
-
#def_keyword ⇒ Object
def def_keyword: () -> String.
-
#end_keyword ⇒ Object
def end_keyword: () -> String?.
-
#equal ⇒ Object
def equal: () -> String?.
-
#initialize(name, name_loc, receiver, parameters, body, locals, def_keyword_loc, operator_loc, lparen_loc, rparen_loc, equal_loc, end_keyword_loc, location) ⇒ DefNode
constructor
def initialize: (name: Symbol, name_loc: Location, receiver: Node?, parameters: ParametersNode?, body: Node?, locals: Array, def_keyword_loc: Location, operator_loc: Location?, lparen_loc: Location?, rparen_loc: Location?, equal_loc: Location?, end_keyword_loc: Location?, location: Location) -> void.
- #inspect(inspector = NodeInspector.new) ⇒ Object
-
#lparen ⇒ Object
def lparen: () -> String?.
-
#operator ⇒ Object
def operator: () -> String?.
-
#rparen ⇒ Object
def rparen: () -> 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(name, name_loc, receiver, parameters, body, locals, def_keyword_loc, operator_loc, lparen_loc, rparen_loc, equal_loc, end_keyword_loc, location) ⇒ DefNode
def initialize: (name: Symbol, name_loc: Location, receiver: Node?, parameters: ParametersNode?, body: Node?, locals: Array, def_keyword_loc: Location, operator_loc: Location?, lparen_loc: Location?, rparen_loc: Location?, equal_loc: Location?, end_keyword_loc: Location?, location: Location) -> void
4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 |
# File 'lib/prism/node.rb', line 4567 def initialize(name, name_loc, receiver, parameters, body, locals, def_keyword_loc, operator_loc, lparen_loc, rparen_loc, equal_loc, end_keyword_loc, location) @name = name @name_loc = name_loc @receiver = receiver @parameters = parameters @body = body @locals = locals @def_keyword_loc = def_keyword_loc @operator_loc = operator_loc @lparen_loc = lparen_loc @rparen_loc = rparen_loc @equal_loc = equal_loc @end_keyword_loc = end_keyword_loc @location = location end |
Instance Attribute Details
#body ⇒ Object (readonly)
attr_reader body: Node?
4543 4544 4545 |
# File 'lib/prism/node.rb', line 4543 def body @body end |
#def_keyword_loc ⇒ Object (readonly)
attr_reader def_keyword_loc: Location
4549 4550 4551 |
# File 'lib/prism/node.rb', line 4549 def def_keyword_loc @def_keyword_loc end |
#end_keyword_loc ⇒ Object (readonly)
attr_reader end_keyword_loc: Location?
4564 4565 4566 |
# File 'lib/prism/node.rb', line 4564 def end_keyword_loc @end_keyword_loc end |
#equal_loc ⇒ Object (readonly)
attr_reader equal_loc: Location?
4561 4562 4563 |
# File 'lib/prism/node.rb', line 4561 def equal_loc @equal_loc end |
#locals ⇒ Object (readonly)
attr_reader locals: Array
4546 4547 4548 |
# File 'lib/prism/node.rb', line 4546 def locals @locals end |
#lparen_loc ⇒ Object (readonly)
attr_reader lparen_loc: Location?
4555 4556 4557 |
# File 'lib/prism/node.rb', line 4555 def lparen_loc @lparen_loc end |
#name ⇒ Object (readonly)
attr_reader name: Symbol
4531 4532 4533 |
# File 'lib/prism/node.rb', line 4531 def name @name end |
#name_loc ⇒ Object (readonly)
attr_reader name_loc: Location
4534 4535 4536 |
# File 'lib/prism/node.rb', line 4534 def name_loc @name_loc end |
#operator_loc ⇒ Object (readonly)
attr_reader operator_loc: Location?
4552 4553 4554 |
# File 'lib/prism/node.rb', line 4552 def operator_loc @operator_loc end |
#parameters ⇒ Object (readonly)
attr_reader parameters: ParametersNode?
4540 4541 4542 |
# File 'lib/prism/node.rb', line 4540 def parameters @parameters end |
#receiver ⇒ Object (readonly)
attr_reader receiver: Node?
4537 4538 4539 |
# File 'lib/prism/node.rb', line 4537 def receiver @receiver end |
#rparen_loc ⇒ Object (readonly)
attr_reader rparen_loc: Location?
4558 4559 4560 |
# File 'lib/prism/node.rb', line 4558 def rparen_loc @rparen_loc end |
Instance Method Details
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void
4584 4585 4586 |
# File 'lib/prism/node.rb', line 4584 def accept(visitor) visitor.visit_def_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array[nil | Node]
4589 4590 4591 |
# File 'lib/prism/node.rb', line 4589 def child_nodes [receiver, parameters, body] end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
4603 4604 4605 |
# File 'lib/prism/node.rb', line 4603 def comment_targets [name_loc, *receiver, *parameters, *body, def_keyword_loc, *operator_loc, *lparen_loc, *rparen_loc, *equal_loc, *end_keyword_loc] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
4594 4595 4596 4597 4598 4599 4600 |
# File 'lib/prism/node.rb', line 4594 def compact_child_nodes compact = [] compact << receiver if receiver compact << parameters if parameters compact << body if body compact end |
#copy(**params) ⇒ Object
def copy: (**params) -> DefNode
4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 |
# File 'lib/prism/node.rb', line 4608 def copy(**params) DefNode.new( params.fetch(:name) { name }, params.fetch(:name_loc) { name_loc }, params.fetch(:receiver) { receiver }, params.fetch(:parameters) { parameters }, params.fetch(:body) { body }, params.fetch(:locals) { locals }, params.fetch(:def_keyword_loc) { def_keyword_loc }, params.fetch(:operator_loc) { operator_loc }, params.fetch(:lparen_loc) { lparen_loc }, params.fetch(:rparen_loc) { rparen_loc }, params.fetch(:equal_loc) { equal_loc }, params.fetch(:end_keyword_loc) { end_keyword_loc }, params.fetch(:location) { location }, ) end |
#deconstruct_keys(keys) ⇒ Object
4630 4631 4632 |
# File 'lib/prism/node.rb', line 4630 def deconstruct_keys(keys) { name: name, name_loc: name_loc, receiver: receiver, parameters: parameters, body: body, locals: locals, def_keyword_loc: def_keyword_loc, operator_loc: operator_loc, lparen_loc: lparen_loc, rparen_loc: rparen_loc, equal_loc: equal_loc, end_keyword_loc: end_keyword_loc, location: location } end |
#def_keyword ⇒ Object
def def_keyword: () -> String
4635 4636 4637 |
# File 'lib/prism/node.rb', line 4635 def def_keyword def_keyword_loc.slice end |
#end_keyword ⇒ Object
def end_keyword: () -> String?
4660 4661 4662 |
# File 'lib/prism/node.rb', line 4660 def end_keyword end_keyword_loc&.slice end |
#equal ⇒ Object
def equal: () -> String?
4655 4656 4657 |
# File 'lib/prism/node.rb', line 4655 def equal equal_loc&.slice end |
#inspect(inspector = NodeInspector.new) ⇒ Object
4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 |
# File 'lib/prism/node.rb', line 4664 def inspect(inspector = NodeInspector.new) inspector << inspector.header(self) inspector << "├── name: #{name.inspect}\n" inspector << "├── name_loc: #{inspector.location(name_loc)}\n" if (receiver = self.receiver).nil? inspector << "├── receiver: ∅\n" else inspector << "├── receiver:\n" inspector << receiver.inspect(inspector.child_inspector("│ ")).delete_prefix(inspector.prefix) end if (parameters = self.parameters).nil? inspector << "├── parameters: ∅\n" else inspector << "├── parameters:\n" inspector << parameters.inspect(inspector.child_inspector("│ ")).delete_prefix(inspector.prefix) end if (body = self.body).nil? inspector << "├── body: ∅\n" else inspector << "├── body:\n" inspector << body.inspect(inspector.child_inspector("│ ")).delete_prefix(inspector.prefix) end inspector << "├── locals: #{locals.inspect}\n" inspector << "├── def_keyword_loc: #{inspector.location(def_keyword_loc)}\n" inspector << "├── operator_loc: #{inspector.location(operator_loc)}\n" inspector << "├── lparen_loc: #{inspector.location(lparen_loc)}\n" inspector << "├── rparen_loc: #{inspector.location(rparen_loc)}\n" inspector << "├── equal_loc: #{inspector.location(equal_loc)}\n" inspector << "└── end_keyword_loc: #{inspector.location(end_keyword_loc)}\n" inspector.to_str end |
#lparen ⇒ Object
def lparen: () -> String?
4645 4646 4647 |
# File 'lib/prism/node.rb', line 4645 def lparen lparen_loc&.slice end |
#operator ⇒ Object
def operator: () -> String?
4640 4641 4642 |
# File 'lib/prism/node.rb', line 4640 def operator operator_loc&.slice end |
#rparen ⇒ Object
def rparen: () -> String?
4650 4651 4652 |
# File 'lib/prism/node.rb', line 4650 def rparen rparen_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
4710 4711 4712 |
# File 'lib/prism/node.rb', line 4710 def type :def_node end |