Class: SyntaxTree::IfNode
Overview
If represents the first clause in an if chain.
if predicate
end
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#consequent ⇒ Object
readonly
- nil | Elsif | Else
-
the next clause in the chain.
-
#predicate ⇒ Object
readonly
- untyped
-
the expression to be checked.
-
#statements ⇒ Object
readonly
- Statements
-
the expressions to be executed.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(predicate: nil, statements: nil, consequent: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(predicate:, statements:, consequent:, location:) ⇒ IfNode
constructor
A new instance of IfNode.
-
#modifier? ⇒ Boolean
Checks if the node was originally found in the modifier form.
Methods inherited from Node
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(predicate:, statements:, consequent:, location:) ⇒ IfNode
Returns a new instance of IfNode.
6369 6370 6371 6372 6373 6374 6375 |
# File 'lib/syntax_tree/node.rb', line 6369 def initialize(predicate:, statements:, consequent:, location:) @predicate = predicate @statements = statements @consequent = consequent @location = location @comments = [] end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
6367 6368 6369 |
# File 'lib/syntax_tree/node.rb', line 6367 def comments @comments end |
#consequent ⇒ Object (readonly)
- nil | Elsif | Else
-
the next clause in the chain
6364 6365 6366 |
# File 'lib/syntax_tree/node.rb', line 6364 def consequent @consequent end |
#predicate ⇒ Object (readonly)
- untyped
-
the expression to be checked
6358 6359 6360 |
# File 'lib/syntax_tree/node.rb', line 6358 def predicate @predicate end |
#statements ⇒ Object (readonly)
- Statements
-
the expressions to be executed
6361 6362 6363 |
# File 'lib/syntax_tree/node.rb', line 6361 def statements @statements end |
Instance Method Details
#===(other) ⇒ Object
6414 6415 6416 6417 |
# File 'lib/syntax_tree/node.rb', line 6414 def ===(other) other.is_a?(IfNode) && predicate === other.predicate && statements === other.statements && consequent === other.consequent end |
#accept(visitor) ⇒ Object
6377 6378 6379 |
# File 'lib/syntax_tree/node.rb', line 6377 def accept(visitor) visitor.visit_if(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
6381 6382 6383 |
# File 'lib/syntax_tree/node.rb', line 6381 def child_nodes [predicate, statements, consequent] end |
#copy(predicate: nil, statements: nil, consequent: nil, location: nil) ⇒ Object
6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 |
# File 'lib/syntax_tree/node.rb', line 6385 def copy(predicate: nil, statements: nil, consequent: nil, location: nil) node = IfNode.new( predicate: predicate || self.predicate, statements: statements || self.statements, consequent: consequent || self.consequent, location: location || self.location ) node.comments.concat(comments.map(&:copy)) node end |
#deconstruct_keys(_keys) ⇒ Object
6400 6401 6402 6403 6404 6405 6406 6407 6408 |
# File 'lib/syntax_tree/node.rb', line 6400 def deconstruct_keys(_keys) { predicate: predicate, statements: statements, consequent: consequent, location: location, comments: comments } end |
#format(q) ⇒ Object
6410 6411 6412 |
# File 'lib/syntax_tree/node.rb', line 6410 def format(q) ConditionalFormatter.new("if", self).format(q) end |
#modifier? ⇒ Boolean
Checks if the node was originally found in the modifier form.
6420 6421 6422 |
# File 'lib/syntax_tree/node.rb', line 6420 def modifier? predicate.location.start_char > statements.location.start_char end |