Class: SyntaxTree::Unless

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

Overview

Unless represents the first clause in an unless chain.

unless predicate
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Unless.



12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
# File 'lib/syntax_tree.rb', line 12341

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



12339
12340
12341
# File 'lib/syntax_tree.rb', line 12339

def comments
  @comments
end

#consequentObject (readonly)

nil, Elsif, Else

the next clause in the chain



12333
12334
12335
# File 'lib/syntax_tree.rb', line 12333

def consequent
  @consequent
end

#locationObject (readonly)

Location

the location of this node



12336
12337
12338
# File 'lib/syntax_tree.rb', line 12336

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



12327
12328
12329
# File 'lib/syntax_tree.rb', line 12327

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



12330
12331
12332
# File 'lib/syntax_tree.rb', line 12330

def statements
  @statements
end

Instance Method Details

#child_nodesObject



12355
12356
12357
# File 'lib/syntax_tree.rb', line 12355

def child_nodes
  [predicate, statements, consequent]
end

#format(q) ⇒ Object



12359
12360
12361
# File 'lib/syntax_tree.rb', line 12359

def format(q)
  ConditionalFormatter.new("unless", self).format(q)
end

#pretty_print(q) ⇒ Object



12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
# File 'lib/syntax_tree.rb', line 12363

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

    q.breakable
    q.pp(predicate)

    q.breakable
    q.pp(statements)

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

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

#to_json(*opts) ⇒ Object



12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
# File 'lib/syntax_tree.rb', line 12382

def to_json(*opts)
  {
    type: :unless,
    pred: predicate,
    stmts: statements,
    cons: consequent,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end