Class: SyntaxTree::Elsif

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

Elsif represents another clause in an if or unless chain.

if variable
elsif other_variable
end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#pretty_print, #to_json

Constructor Details

#initialize(predicate:, statements:, consequent:, location:, comments: []) ⇒ Elsif

Returns a new instance of Elsif.



3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
# File 'lib/syntax_tree/node.rb', line 3650

def initialize(
  predicate:,
  statements:,
  consequent:,
  location:,
  comments: []
)
  @predicate = predicate
  @statements = statements
  @consequent = consequent
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3648
3649
3650
# File 'lib/syntax_tree/node.rb', line 3648

def comments
  @comments
end

#consequentObject (readonly)

nil | Elsif | Else

the next clause in the chain



3645
3646
3647
# File 'lib/syntax_tree/node.rb', line 3645

def consequent
  @consequent
end

#predicateObject (readonly)

untyped

the expression to be checked



3639
3640
3641
# File 'lib/syntax_tree/node.rb', line 3639

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



3642
3643
3644
# File 'lib/syntax_tree/node.rb', line 3642

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



3664
3665
3666
# File 'lib/syntax_tree/node.rb', line 3664

def accept(visitor)
  visitor.visit_elsif(self)
end

#child_nodesObject Also known as: deconstruct



3668
3669
3670
# File 'lib/syntax_tree/node.rb', line 3668

def child_nodes
  [predicate, statements, consequent]
end

#deconstruct_keys(keys) ⇒ Object



3674
3675
3676
3677
3678
3679
3680
3681
3682
# File 'lib/syntax_tree/node.rb', line 3674

def deconstruct_keys(keys)
  {
    predicate: predicate,
    statements: statements,
    consequent: consequent,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
# File 'lib/syntax_tree/node.rb', line 3684

def format(q)
  q.group do
    q.group do
      q.text("elsif ")
      q.nest("elsif".length - 1) { q.format(predicate) }
    end

    unless statements.empty?
      q.indent do
        q.breakable(force: true)
        q.format(statements)
      end
    end

    if consequent
      q.group do
        q.breakable(force: true)
        q.format(consequent)
      end
    end
  end
end