Class: SyntaxTree::DoBlock

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

Overview

DoBlock represents passing a block to a method call using the do and end keywords.

method do |value|
end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Constructor Details

#initialize(keyword:, block_var:, bodystmt:, location:, comments: []) ⇒ DoBlock

Returns a new instance of DoBlock.



4099
4100
4101
4102
4103
4104
4105
# File 'lib/syntax_tree/node.rb', line 4099

def initialize(keyword:, block_var:, bodystmt:, location:, comments: [])
  @keyword = keyword
  @block_var = block_var
  @bodystmt = bodystmt
  @location = location
  @comments = comments
end

Instance Attribute Details

#block_varObject (readonly)

nil | BlockVar

the optional variable declaration within this block



4091
4092
4093
# File 'lib/syntax_tree/node.rb', line 4091

def block_var
  @block_var
end

#bodystmtObject (readonly)

BodyStmt

the expressions to be executed within this block



4094
4095
4096
# File 'lib/syntax_tree/node.rb', line 4094

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4097
4098
4099
# File 'lib/syntax_tree/node.rb', line 4097

def comments
  @comments
end

#keywordObject (readonly)

Kw

the do keyword that opens this block



4088
4089
4090
# File 'lib/syntax_tree/node.rb', line 4088

def keyword
  @keyword
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



4107
4108
4109
# File 'lib/syntax_tree/node.rb', line 4107

def child_nodes
  [keyword, block_var, bodystmt]
end

#deconstruct_keys(keys) ⇒ Object



4113
4114
4115
4116
4117
4118
4119
4120
4121
# File 'lib/syntax_tree/node.rb', line 4113

def deconstruct_keys(keys)
  {
    keyword: keyword,
    block_var: block_var,
    bodystmt: bodystmt,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



4123
4124
4125
# File 'lib/syntax_tree/node.rb', line 4123

def format(q)
  BlockFormatter.new(self, keyword, "end", bodystmt).format(q)
end

#pretty_print(q) ⇒ Object



4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
# File 'lib/syntax_tree/node.rb', line 4127

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("do_block")

    if block_var
      q.breakable
      q.pp(block_var)
    end

    q.breakable
    q.pp(bodystmt)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
# File 'lib/syntax_tree/node.rb', line 4143

def to_json(*opts)
  {
    type: :do_block,
    keyword: keyword,
    block_var: block_var,
    bodystmt: bodystmt,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end