Class: SyntaxTree::DefNode
Overview
Def represents defining a regular method on the current self object.
def method(param) result end
def object.method(param) result end
Instance Attribute Summary collapse
-
#bodystmt ⇒ Object
readonly
- BodyStmt | untyped
-
the expressions to be executed by the method.
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#name ⇒ Object
readonly
- Backtick | Const | Ident | Kw | Op
-
the name of the method.
-
#operator ⇒ Object
readonly
- nil | Op | Period
-
the operator being used to declare the method.
-
#params ⇒ Object
readonly
- nil | Params | Paren
-
the parameter declaration for the method.
-
#target ⇒ Object
readonly
- nil | untyped
-
the target where the method is being defined.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #arity ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(target: nil, operator: nil, name: nil, params: nil, bodystmt: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
-
#endless? ⇒ Boolean
Returns true if the method was found in the source in the “endless” form, i.e.
- #format(q) ⇒ Object
-
#initialize(target:, operator:, name:, params:, bodystmt:, location:) ⇒ DefNode
constructor
A new instance of DefNode.
Methods inherited from Node
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(target:, operator:, name:, params:, bodystmt:, location:) ⇒ DefNode
4036 4037 4038 4039 4040 4041 4042 4043 4044 |
# File 'lib/syntax_tree/node.rb', line 4036 def initialize(target:, operator:, name:, params:, bodystmt:, location:) @target = target @operator = operator @name = name @params = params @bodystmt = bodystmt @location = location @comments = [] end |
Instance Attribute Details
#bodystmt ⇒ Object (readonly)
- BodyStmt | untyped
-
the expressions to be executed by the method
4031 4032 4033 |
# File 'lib/syntax_tree/node.rb', line 4031 def bodystmt @bodystmt end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
4034 4035 4036 |
# File 'lib/syntax_tree/node.rb', line 4034 def comments @comments end |
#name ⇒ Object (readonly)
- Backtick | Const | Ident | Kw | Op
-
the name of the method
4025 4026 4027 |
# File 'lib/syntax_tree/node.rb', line 4025 def name @name end |
#operator ⇒ Object (readonly)
- nil | Op | Period
-
the operator being used to declare the method
4022 4023 4024 |
# File 'lib/syntax_tree/node.rb', line 4022 def operator @operator end |
#params ⇒ Object (readonly)
- nil | Params | Paren
-
the parameter declaration for the method
4028 4029 4030 |
# File 'lib/syntax_tree/node.rb', line 4028 def params @params end |
#target ⇒ Object (readonly)
- nil | untyped
-
the target where the method is being defined
4019 4020 4021 |
# File 'lib/syntax_tree/node.rb', line 4019 def target @target end |
Instance Method Details
#===(other) ⇒ Object
4132 4133 4134 4135 4136 |
# File 'lib/syntax_tree/node.rb', line 4132 def ===(other) other.is_a?(DefNode) && target === other.target && operator === other.operator && name === other.name && params === other.params && bodystmt === other.bodystmt end |
#accept(visitor) ⇒ Object
4046 4047 4048 |
# File 'lib/syntax_tree/node.rb', line 4046 def accept(visitor) visitor.visit_def(self) end |
#arity ⇒ Object
4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 |
# File 'lib/syntax_tree/node.rb', line 4145 def arity case params when Params params.arity when Paren params.contents.arity else 0..0 end end |
#child_nodes ⇒ Object Also known as: deconstruct
4050 4051 4052 |
# File 'lib/syntax_tree/node.rb', line 4050 def child_nodes [target, operator, name, params, bodystmt] end |
#copy(target: nil, operator: nil, name: nil, params: nil, bodystmt: nil, location: nil) ⇒ Object
4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 |
# File 'lib/syntax_tree/node.rb', line 4054 def copy( target: nil, operator: nil, name: nil, params: nil, bodystmt: nil, location: nil ) node = DefNode.new( target: target || self.target, operator: operator || self.operator, name: name || self.name, params: params || self.params, bodystmt: bodystmt || self.bodystmt, location: location || self.location ) node.comments.concat(comments.map(&:copy)) node end |
#deconstruct_keys(_keys) ⇒ Object
4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 |
# File 'lib/syntax_tree/node.rb', line 4078 def deconstruct_keys(_keys) { target: target, operator: operator, name: name, params: params, bodystmt: bodystmt, location: location, comments: comments } end |
#endless? ⇒ Boolean
Returns true if the method was found in the source in the “endless” form, i.e. where the method body is defined using the ‘=` operator after the method name and parameters.
4141 4142 4143 |
# File 'lib/syntax_tree/node.rb', line 4141 def endless? !bodystmt.is_a?(BodyStmt) end |
#format(q) ⇒ Object
4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 |
# File 'lib/syntax_tree/node.rb', line 4090 def format(q) q.group do q.group do q.text("def ") if target q.format(target) q.format(CallOperatorFormatter.new(operator), stackable: false) end q.format(name) case params when Paren q.format(params) when Params q.format(params) if !params.empty? || params.comments.any? end end if endless? q.text(" =") q.group do q.indent do q.breakable_space q.format(bodystmt) end end else unless bodystmt.empty? q.indent do q.breakable_force q.format(bodystmt) end end q.breakable_force q.text("end") end end end |