Class: SyntaxTree::IfOp
Overview
IfOp represents a ternary clause.
predicate ? truthy : falsy
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#falsy ⇒ Object
readonly
- untyped
-
the expression to be executed if the predicate is falsy.
-
#predicate ⇒ Object
readonly
- untyped
-
the expression to be checked.
-
#truthy ⇒ Object
readonly
- untyped
-
the expression to be executed if the predicate is truthy.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(predicate: nil, truthy: nil, falsy: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(predicate:, truthy:, falsy:, location:) ⇒ IfOp
constructor
A new instance of IfOp.
Methods inherited from Node
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(predicate:, truthy:, falsy:, location:) ⇒ IfOp
Returns a new instance of IfOp.
6456 6457 6458 6459 6460 6461 6462 |
# File 'lib/syntax_tree/node.rb', line 6456 def initialize(predicate:, truthy:, falsy:, location:) @predicate = predicate @truthy = truthy @falsy = falsy @location = location @comments = [] end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
6454 6455 6456 |
# File 'lib/syntax_tree/node.rb', line 6454 def comments @comments end |
#falsy ⇒ Object (readonly)
- untyped
-
the expression to be executed if the predicate is falsy
6451 6452 6453 |
# File 'lib/syntax_tree/node.rb', line 6451 def falsy @falsy end |
#predicate ⇒ Object (readonly)
- untyped
-
the expression to be checked
6445 6446 6447 |
# File 'lib/syntax_tree/node.rb', line 6445 def predicate @predicate end |
#truthy ⇒ Object (readonly)
- untyped
-
the expression to be executed if the predicate is truthy
6448 6449 6450 |
# File 'lib/syntax_tree/node.rb', line 6448 def truthy @truthy end |
Instance Method Details
#===(other) ⇒ Object
6513 6514 6515 6516 |
# File 'lib/syntax_tree/node.rb', line 6513 def ===(other) other.is_a?(IfOp) && predicate === other.predicate && truthy === other.truthy && falsy === other.falsy end |
#accept(visitor) ⇒ Object
6464 6465 6466 |
# File 'lib/syntax_tree/node.rb', line 6464 def accept(visitor) visitor.visit_if_op(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
6468 6469 6470 |
# File 'lib/syntax_tree/node.rb', line 6468 def child_nodes [predicate, truthy, falsy] end |
#copy(predicate: nil, truthy: nil, falsy: nil, location: nil) ⇒ Object
6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 |
# File 'lib/syntax_tree/node.rb', line 6472 def copy(predicate: nil, truthy: nil, falsy: nil, location: nil) node = IfOp.new( predicate: predicate || self.predicate, truthy: truthy || self.truthy, falsy: falsy || self.falsy, location: location || self.location ) node.comments.concat(comments.map(&:copy)) node end |
#deconstruct_keys(_keys) ⇒ Object
6487 6488 6489 6490 6491 6492 6493 6494 6495 |
# File 'lib/syntax_tree/node.rb', line 6487 def deconstruct_keys(_keys) { predicate: predicate, truthy: truthy, falsy: falsy, location: location, comments: comments } end |
#format(q) ⇒ Object
6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 |
# File 'lib/syntax_tree/node.rb', line 6497 def format(q) force_flat = [ AliasNode, Assign, Break, Command, CommandCall, Heredoc, IfNode, IfOp, Lambda, MAssign, Next, OpAssign, RescueMod, ReturnNode, Super, Undef, UnlessNode, VoidStmt, YieldNode, ZSuper ] if q.parent.is_a?(Paren) || force_flat.include?(truthy.class) || force_flat.include?(falsy.class) q.group { format_flat(q) } return end q.group { q.if_break { format_break(q) }.if_flat { format_flat(q) } } end |