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
- Node
-
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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid
Constructor Details
#initialize(predicate:, statements:, consequent:, location:) ⇒ IfNode
Returns a new instance of IfNode.
6429 6430 6431 6432 6433 6434 6435 |
# File 'lib/syntax_tree/node.rb', line 6429 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
6427 6428 6429 |
# File 'lib/syntax_tree/node.rb', line 6427 def comments @comments end |
#consequent ⇒ Object (readonly)
- nil | Elsif | Else
-
the next clause in the chain
6424 6425 6426 |
# File 'lib/syntax_tree/node.rb', line 6424 def consequent @consequent end |
#predicate ⇒ Object (readonly)
- Node
-
the expression to be checked
6418 6419 6420 |
# File 'lib/syntax_tree/node.rb', line 6418 def predicate @predicate end |
#statements ⇒ Object (readonly)
- Statements
-
the expressions to be executed
6421 6422 6423 |
# File 'lib/syntax_tree/node.rb', line 6421 def statements @statements end |
Instance Method Details
#===(other) ⇒ Object
6474 6475 6476 6477 |
# File 'lib/syntax_tree/node.rb', line 6474 def ===(other) other.is_a?(IfNode) && predicate === other.predicate && statements === other.statements && consequent === other.consequent end |
#accept(visitor) ⇒ Object
6437 6438 6439 |
# File 'lib/syntax_tree/node.rb', line 6437 def accept(visitor) visitor.visit_if(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
6441 6442 6443 |
# File 'lib/syntax_tree/node.rb', line 6441 def child_nodes [predicate, statements, consequent] end |
#copy(predicate: nil, statements: nil, consequent: nil, location: nil) ⇒ Object
6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 |
# File 'lib/syntax_tree/node.rb', line 6445 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
6460 6461 6462 6463 6464 6465 6466 6467 6468 |
# File 'lib/syntax_tree/node.rb', line 6460 def deconstruct_keys(_keys) { predicate: predicate, statements: statements, consequent: consequent, location: location, comments: comments } end |
#format(q) ⇒ Object
6470 6471 6472 |
# File 'lib/syntax_tree/node.rb', line 6470 def format(q) ConditionalFormatter.new("if", self).format(q) end |
#modifier? ⇒ Boolean
Checks if the node was originally found in the modifier form.
6480 6481 6482 |
# File 'lib/syntax_tree/node.rb', line 6480 def modifier? predicate.location.start_char > statements.location.start_char end |