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

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(statements:, rescue_clause:, else_keyword:, else_clause:, ensure_clause:, location:) ⇒ BodyStmt

Returns a new instance of BodyStmt.



2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
# 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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2295
2296
2297
# File 'lib/syntax_tree/node.rb', line 2295

def comments
  @comments
end

#else_clauseObject (readonly)

nil | Statements

the optional set of statements inside the else clause



2289
2290
2291
# File 'lib/syntax_tree/node.rb', line 2289

def else_clause
  @else_clause
end

#else_keywordObject (readonly)

nil | Kw

the optional else keyword



2286
2287
2288
# File 'lib/syntax_tree/node.rb', line 2286

def else_keyword
  @else_keyword
end

#ensure_clauseObject (readonly)

nil | Ensure

the optional ensure clause



2292
2293
2294
# File 'lib/syntax_tree/node.rb', line 2292

def ensure_clause
  @ensure_clause
end

#rescue_clauseObject (readonly)

nil | Rescue

the optional rescue chain attached to the begin clause



2283
2284
2285
# File 'lib/syntax_tree/node.rb', line 2283

def rescue_clause
  @rescue_clause
end

#statementsObject (readonly)

Statements

the list of statements inside the begin clause



2280
2281
2282
# File 'lib/syntax_tree/node.rb', line 2280

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



2428
2429
2430
2431
2432
2433
2434
# 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) ⇒ Object



2352
2353
2354
# 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) ⇒ Object



2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
# 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_nodesObject Also known as: deconstruct



2356
2357
2358
# File 'lib/syntax_tree/node.rb', line 2356

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

#copy(statements: nil, rescue_clause: nil, else_keyword: nil, else_clause: nil, ensure_clause: nil, location: nil) ⇒ Object



2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
# 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_keys(_keys) ⇒ Object



2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
# 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?Boolean

Returns:

  • (Boolean)


2348
2349
2350
# File 'lib/syntax_tree/node.rb', line 2348

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

#format(q) ⇒ Object



2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
# 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