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.



8829
8830
8831
8832
8833
8834
# File 'lib/syntax_tree/node.rb', line 8829

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



8827
8828
8829
# File 'lib/syntax_tree/node.rb', line 8827

def comments
  @comments
end

#parenthesesObject (readonly)

boolean

whether or not parentheses were used



8824
8825
8826
# File 'lib/syntax_tree/node.rb', line 8824

def parentheses
  @parentheses
end

#statementObject (readonly)

nil | untyped

the statement on which to operate



8821
8822
8823
# File 'lib/syntax_tree/node.rb', line 8821

def statement
  @statement
end

Instance Method Details

#accept(visitor) ⇒ Object



8836
8837
8838
# File 'lib/syntax_tree/node.rb', line 8836

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

#child_nodesObject Also known as: deconstruct



8840
8841
8842
# File 'lib/syntax_tree/node.rb', line 8840

def child_nodes
  [statement]
end

#deconstruct_keys(_keys) ⇒ Object



8846
8847
8848
8849
8850
8851
8852
8853
# File 'lib/syntax_tree/node.rb', line 8846

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

#format(q) ⇒ Object



8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
# File 'lib/syntax_tree/node.rb', line 8855

def format(q)
  parent = q.parents.take(2)[1]
  ternary =
    (parent.is_a?(If) || parent.is_a?(Unless)) &&
      Ternaryable.call(q, parent)

  q.text("not")

  if parentheses
    q.text("(")
  elsif ternary
    q.if_break { q.text(" ") }.if_flat { q.text("(") }
  else
    q.text(" ")
  end

  q.format(statement) if statement

  if parentheses
    q.text(")")
  elsif ternary
    q.if_flat { q.text(")") }
  end
end