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.



11071
11072
11073
11074
11075
11076
# File 'lib/syntax_tree/node.rb', line 11071

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



11069
11070
11071
# File 'lib/syntax_tree/node.rb', line 11069

def comments
  @comments
end

#parenthesesObject (readonly) Also known as: parentheses?

boolean

whether or not parentheses were used



11065
11066
11067
# File 'lib/syntax_tree/node.rb', line 11065

def parentheses
  @parentheses
end

#statementObject (readonly)

nil | Node

the statement on which to operate



11062
11063
11064
# File 'lib/syntax_tree/node.rb', line 11062

def statement
  @statement
end

Instance Method Details

#===(other) ⇒ Object



11133
11134
11135
11136
# File 'lib/syntax_tree/node.rb', line 11133

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

#accept(visitor) ⇒ Object



11078
11079
11080
# File 'lib/syntax_tree/node.rb', line 11078

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

#child_nodesObject Also known as: deconstruct



11082
11083
11084
# File 'lib/syntax_tree/node.rb', line 11082

def child_nodes
  [statement]
end

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



11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
# File 'lib/syntax_tree/node.rb', line 11086

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



11100
11101
11102
11103
11104
11105
11106
11107
# File 'lib/syntax_tree/node.rb', line 11100

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

#format(q) ⇒ Object



11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
# File 'lib/syntax_tree/node.rb', line 11109

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