class SyntaxTree::BodyStmt

bodystmt can’t actually determine its bounds appropriately because it doesn’t necessarily know where it started. So the parent node needs to report back down into this one where it goes.

Attributes

comments[R]
Array[ Comment | EmbDoc ]

the comments attached to this node

else_clause[R]
nil | Statements

the optional set of statements inside the else clause

else_keyword[R]
nil | Kw

the optional else keyword

ensure_clause[R]
nil | Ensure

the optional ensure clause

rescue_clause[R]
nil | Rescue

the optional rescue chain attached to the begin clause

statements[R]
Statements

the list of statements inside the begin clause

Public Class Methods

new( statements:, rescue_clause:, else_keyword:, else_clause:, ensure_clause:, location: ) click to toggle source
# File lib/syntax_tree/node.rb, line 2297
def initialize(
  statements:,
  rescue_clause:,
  else_keyword:,
  else_clause:,
  ensure_clause:,
  location:
)
  @statements = statements
  @rescue_clause = rescue_clause
  @else_keyword = else_keyword
  @else_clause = else_clause
  @ensure_clause = ensure_clause
  @location = location
  @comments = []
end

Public Instance Methods

===(other) click to toggle source
# File lib/syntax_tree/node.rb, line 2428
def ===(other)
  other.is_a?(BodyStmt) && statements === other.statements &&
    rescue_clause === other.rescue_clause &&
    else_keyword === other.else_keyword &&
    else_clause === other.else_clause &&
    ensure_clause === other.ensure_clause
end
accept(visitor) click to toggle source
# File lib/syntax_tree/node.rb, line 2352
def accept(visitor)
  visitor.visit_bodystmt(self)
end
bind(parser, start_char, start_column, end_char, end_column) click to toggle source
# File lib/syntax_tree/node.rb, line 2314
def bind(parser, start_char, start_column, end_char, end_column)
  rescue_clause = self.rescue_clause

  @location =
    Location.new(
      start_line: location.start_line,
      start_char: start_char,
      start_column: start_column,
      end_line: location.end_line,
      end_char: end_char,
      end_column: end_column
    )

  # Here we're going to determine the bounds for the statements
  consequent = rescue_clause || else_clause || ensure_clause
  statements.bind(
    parser,
    start_char,
    start_column,
    consequent ? consequent.location.start_char : end_char,
    consequent ? consequent.location.start_column : end_column
  )

  # Next we're going to determine the rescue clause if there is one
  if rescue_clause
    consequent = else_clause || ensure_clause

    rescue_clause.bind_end(
      consequent ? consequent.location.start_char : end_char,
      consequent ? consequent.location.start_column : end_column
    )
  end
end
child_nodes() click to toggle source
# File lib/syntax_tree/node.rb, line 2356
def child_nodes
  [statements, rescue_clause, else_keyword, else_clause, ensure_clause]
end
Also aliased as: deconstruct
copy( statements: nil, rescue_clause: nil, else_keyword: nil, else_clause: nil, ensure_clause: nil, location: nil ) click to toggle source
# File lib/syntax_tree/node.rb, line 2360
def copy(
  statements: nil,
  rescue_clause: nil,
  else_keyword: nil,
  else_clause: nil,
  ensure_clause: nil,
  location: nil
)
  node =
    BodyStmt.new(
      statements: statements || self.statements,
      rescue_clause: rescue_clause || self.rescue_clause,
      else_keyword: else_keyword || self.else_keyword,
      else_clause: else_clause || self.else_clause,
      ensure_clause: ensure_clause || self.ensure_clause,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end
deconstruct()
Alias for: child_nodes
deconstruct_keys(_keys) click to toggle source
# File lib/syntax_tree/node.rb, line 2384
def deconstruct_keys(_keys)
  {
    statements: statements,
    rescue_clause: rescue_clause,
    else_keyword: else_keyword,
    else_clause: else_clause,
    ensure_clause: ensure_clause,
    location: location,
    comments: comments
  }
end
empty?() click to toggle source
# File lib/syntax_tree/node.rb, line 2348
def empty?
  statements.empty? && !rescue_clause && !else_clause && !ensure_clause
end
format(q) click to toggle source
# File lib/syntax_tree/node.rb, line 2396
def format(q)
  q.group do
    q.format(statements) unless statements.empty?

    if rescue_clause
      q.nest(-2) do
        q.breakable_force
        q.format(rescue_clause)
      end
    end

    if else_clause
      q.nest(-2) do
        q.breakable_force
        q.format(else_keyword)
      end

      unless else_clause.empty?
        q.breakable_force
        q.format(else_clause)
      end
    end

    if ensure_clause
      q.nest(-2) do
        q.breakable_force
        q.format(ensure_clause)
      end
    end
  end
end