Class: SyntaxTree::Unless

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

Overview

Unless represents the first clause in an unless chain.

unless predicate
end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Unless.



10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
# File 'lib/syntax_tree/node.rb', line 10185

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



10183
10184
10185
# File 'lib/syntax_tree/node.rb', line 10183

def comments
  @comments
end

#consequentObject (readonly)

nil, Elsif, Else

the next clause in the chain



10180
10181
10182
# File 'lib/syntax_tree/node.rb', line 10180

def consequent
  @consequent
end

#predicateObject (readonly)

untyped

the expression to be checked



10174
10175
10176
# File 'lib/syntax_tree/node.rb', line 10174

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



10177
10178
10179
# File 'lib/syntax_tree/node.rb', line 10177

def statements
  @statements
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



10199
10200
10201
# File 'lib/syntax_tree/node.rb', line 10199

def child_nodes
  [predicate, statements, consequent]
end

#deconstruct_keys(keys) ⇒ Object



10205
10206
10207
10208
10209
10210
10211
10212
10213
# File 'lib/syntax_tree/node.rb', line 10205

def deconstruct_keys(keys)
  {
    predicate: predicate,
    statements: statements,
    consequent: consequent,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



10215
10216
10217
# File 'lib/syntax_tree/node.rb', line 10215

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

#pretty_print(q) ⇒ Object



10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
# File 'lib/syntax_tree/node.rb', line 10219

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



10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
# File 'lib/syntax_tree/node.rb', line 10238

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