Class: SyntaxTree::BodyStmt

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

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(statements:, rescue_clause:, else_clause:, ensure_clause:, location:, comments: []) ⇒ BodyStmt

Returns a new instance of BodyStmt.



2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
# File 'lib/syntax_tree/node.rb', line 2209

def initialize(
  statements:,
  rescue_clause:,
  else_clause:,
  ensure_clause:,
  location:,
  comments: []
)
  @statements = statements
  @rescue_clause = rescue_clause
  @else_clause = else_clause
  @ensure_clause = ensure_clause
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2207
2208
2209
# File 'lib/syntax_tree/node.rb', line 2207

def comments
  @comments
end

#else_clauseObject (readonly)

nil | Statements

the optional set of statements inside the else clause



2198
2199
2200
# File 'lib/syntax_tree/node.rb', line 2198

def else_clause
  @else_clause
end

#ensure_clauseObject (readonly)

nil | Ensure

the optional ensure clause



2201
2202
2203
# File 'lib/syntax_tree/node.rb', line 2201

def ensure_clause
  @ensure_clause
end

#locationObject (readonly)

Location

the location of this node



2204
2205
2206
# File 'lib/syntax_tree/node.rb', line 2204

def location
  @location
end

#rescue_clauseObject (readonly)

nil | Rescue

the optional rescue chain attached to the begin clause



2195
2196
2197
# File 'lib/syntax_tree/node.rb', line 2195

def rescue_clause
  @rescue_clause
end

#statementsObject (readonly)

Statements

the list of statements inside the begin clause



2192
2193
2194
# File 'lib/syntax_tree/node.rb', line 2192

def statements
  @statements
end

Instance Method Details

#bind(start_char, end_char) ⇒ Object



2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
# File 'lib/syntax_tree/node.rb', line 2225

def bind(start_char, end_char)
  @location =
    Location.new(
      start_line: location.start_line,
      start_char: start_char,
      end_line: location.end_line,
      end_char: end_char
    )

  parts = [rescue_clause, else_clause, ensure_clause]

  # Here we're going to determine the bounds for the statements
  consequent = parts.compact.first
  statements.bind(
    start_char,
    consequent ? consequent.location.start_char : end_char
  )

  # Next we're going to determine the rescue clause if there is one
  if rescue_clause
    consequent = parts.drop(1).compact.first
    rescue_clause.bind_end(
      consequent ? consequent.location.start_char : end_char
    )
  end
end

#child_nodesObject Also known as: deconstruct



2256
2257
2258
# File 'lib/syntax_tree/node.rb', line 2256

def child_nodes
  [statements, rescue_clause, else_clause, ensure_clause]
end

#deconstruct_keys(keys) ⇒ Object



2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
# File 'lib/syntax_tree/node.rb', line 2262

def deconstruct_keys(keys)
  {
    statements: statements,
    rescue_clause: rescue_clause,
    else_clause: else_clause,
    ensure_clause: ensure_clause,
    location: location,
    comments: comments
  }
end

#empty?Boolean

Returns:



2252
2253
2254
# File 'lib/syntax_tree/node.rb', line 2252

def empty?
  statements.empty? && !rescue_clause && !else_clause && !ensure_clause
end

#format(q) ⇒ Object



2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
# File 'lib/syntax_tree/node.rb', line 2273

def format(q)
  q.group do
    q.format(statements) unless statements.empty?

    if rescue_clause
      q.nest(-2) do
        q.breakable(force: true)
        q.format(rescue_clause)
      end
    end

    if else_clause
      q.nest(-2) do
        q.breakable(force: true)
        q.text("else")
      end
      q.breakable(force: true)
      q.format(else_clause)
    end

    if ensure_clause
      q.nest(-2) do
        q.breakable(force: true)
        q.format(ensure_clause)
      end
    end
  end
end

#pretty_print(q) ⇒ Object



2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
# File 'lib/syntax_tree/node.rb', line 2302

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

    q.breakable
    q.pp(statements)

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

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

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

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

#to_json(*opts) ⇒ Object



2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
# File 'lib/syntax_tree/node.rb', line 2328

def to_json(*opts)
  {
    type: :bodystmt,
    stmts: statements,
    rsc: rescue_clause,
    els: else_clause,
    ens: ensure_clause,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end