Class: SyntaxTree::Not

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

Overview

Not represents the unary not method being called on an expression.

not value

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(statement:, parentheses:, location:, comments: []) ⇒ Not

Returns a new instance of Not.



9225
9226
9227
9228
9229
9230
# File 'lib/syntax_tree/node.rb', line 9225

def initialize(statement:, parentheses:, location:, comments: [])
  @statement = statement
  @parentheses = parentheses
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9223
9224
9225
# File 'lib/syntax_tree/node.rb', line 9223

def comments
  @comments
end

#parenthesesObject (readonly) Also known as: parentheses?

boolean

whether or not parentheses were used



9219
9220
9221
# File 'lib/syntax_tree/node.rb', line 9219

def parentheses
  @parentheses
end

#statementObject (readonly)

nil | untyped

the statement on which to operate



9216
9217
9218
# File 'lib/syntax_tree/node.rb', line 9216

def statement
  @statement
end

Instance Method Details

#accept(visitor) ⇒ Object



9232
9233
9234
# File 'lib/syntax_tree/node.rb', line 9232

def accept(visitor)
  visitor.visit_not(self)
end

#child_nodesObject Also known as: deconstruct



9236
9237
9238
# File 'lib/syntax_tree/node.rb', line 9236

def child_nodes
  [statement]
end

#deconstruct_keys(_keys) ⇒ Object



9242
9243
9244
9245
9246
9247
9248
9249
# File 'lib/syntax_tree/node.rb', line 9242

def deconstruct_keys(_keys)
  {
    statement: statement,
    parentheses: parentheses,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
# File 'lib/syntax_tree/node.rb', line 9251

def format(q)
  q.text("not")

  if parentheses
    q.text("(")
    q.format(statement) if statement
    q.text(")")
  else
    grandparent = q.grandparent
    ternary =
      (grandparent.is_a?(If) || grandparent.is_a?(Unless)) &&
        Ternaryable.call(q, grandparent)

    if ternary
      q.if_break { q.text(" ") }.if_flat { q.text("(") }
      q.format(statement) if statement
      q.if_flat { q.text(")") } if ternary
    else
      q.text(" ")
      q.format(statement) if statement
    end
  end
end