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 | Node
-
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 | Node
-
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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid
Constructor Details
#initialize(target:, operator:, name:, params:, bodystmt:, location:) ⇒ DefNode
Returns a new instance of DefNode.
4132 4133 4134 4135 4136 4137 4138 4139 4140 |
# File 'lib/syntax_tree/node.rb', line 4132 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 | Node
-
the expressions to be executed by the method
4127 4128 4129 |
# File 'lib/syntax_tree/node.rb', line 4127 def bodystmt @bodystmt end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
4130 4131 4132 |
# File 'lib/syntax_tree/node.rb', line 4130 def comments @comments end |
#name ⇒ Object (readonly)
- Backtick | Const | Ident | Kw | Op
-
the name of the method
4121 4122 4123 |
# File 'lib/syntax_tree/node.rb', line 4121 def name @name end |
#operator ⇒ Object (readonly)
- nil | Op | Period
-
the operator being used to declare the method
4118 4119 4120 |
# File 'lib/syntax_tree/node.rb', line 4118 def operator @operator end |
#params ⇒ Object (readonly)
- nil | Params | Paren
-
the parameter declaration for the method
4124 4125 4126 |
# File 'lib/syntax_tree/node.rb', line 4124 def params @params end |
#target ⇒ Object (readonly)
- nil | Node
-
the target where the method is being defined
4115 4116 4117 |
# File 'lib/syntax_tree/node.rb', line 4115 def target @target end |
Instance Method Details
#===(other) ⇒ Object
4232 4233 4234 4235 4236 |
# File 'lib/syntax_tree/node.rb', line 4232 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
4142 4143 4144 |
# File 'lib/syntax_tree/node.rb', line 4142 def accept(visitor) visitor.visit_def(self) end |
#arity ⇒ Object
4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 |
# File 'lib/syntax_tree/node.rb', line 4245 def arity params = self.params case params when Params params.arity when Paren params.contents.arity else 0..0 end end |
#child_nodes ⇒ Object Also known as: deconstruct
4146 4147 4148 |
# File 'lib/syntax_tree/node.rb', line 4146 def child_nodes [target, operator, name, params, bodystmt] end |
#copy(target: nil, operator: nil, name: nil, params: nil, bodystmt: nil, location: nil) ⇒ Object
4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 |
# File 'lib/syntax_tree/node.rb', line 4150 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
4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 |
# File 'lib/syntax_tree/node.rb', line 4174 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.
4241 4242 4243 |
# File 'lib/syntax_tree/node.rb', line 4241 def endless? !bodystmt.is_a?(BodyStmt) end |
#format(q) ⇒ Object
4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 |
# File 'lib/syntax_tree/node.rb', line 4186 def format(q) params = self.params bodystmt = self.bodystmt q.group do q.group do q.text("def") q.text(" ") if target || name.comments.empty? 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 |