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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(statement:, parentheses:, location:) ⇒ Not

Returns a new instance of Not.



11104
11105
11106
11107
11108
11109
# File 'lib/syntax_tree/node.rb', line 11104

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



11102
11103
11104
# File 'lib/syntax_tree/node.rb', line 11102

def comments
  @comments
end

#parenthesesObject (readonly) Also known as: parentheses?

boolean

whether or not parentheses were used



11098
11099
11100
# File 'lib/syntax_tree/node.rb', line 11098

def parentheses
  @parentheses
end

#statementObject (readonly)

nil | Node

the statement on which to operate



11095
11096
11097
# File 'lib/syntax_tree/node.rb', line 11095

def statement
  @statement
end

Instance Method Details

#===(other) ⇒ Object



11166
11167
11168
11169
# File 'lib/syntax_tree/node.rb', line 11166

def ===(other)
  other.is_a?(Not) && statement === other.statement &&
    parentheses === other.parentheses
end

#accept(visitor) ⇒ Object



11111
11112
11113
# File 'lib/syntax_tree/node.rb', line 11111

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

#child_nodesObject Also known as: deconstruct



11115
11116
11117
# File 'lib/syntax_tree/node.rb', line 11115

def child_nodes
  [statement]
end

#copy(statement: nil, parentheses: nil, location: nil) ⇒ Object



11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
# File 'lib/syntax_tree/node.rb', line 11119

def copy(statement: nil, parentheses: nil, location: nil)
  node =
    Not.new(
      statement: statement || self.statement,
      parentheses: parentheses || self.parentheses,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



11133
11134
11135
11136
11137
11138
11139
11140
# File 'lib/syntax_tree/node.rb', line 11133

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

#format(q) ⇒ Object



11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
# File 'lib/syntax_tree/node.rb', line 11142

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?(IfNode) || grandparent.is_a?(UnlessNode)) &&
        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