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.



8940
8941
8942
8943
8944
8945
# File 'lib/syntax_tree/node.rb', line 8940

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

Instance Attribute Details

#comments ⇒ Object (readonly)

[Array[ Comment | EmbDoc ]] the comments attached to this node



8938
8939
8940
# File 'lib/syntax_tree/node.rb', line 8938

def comments
  @comments
end

#parentheses ⇒ Object (readonly)

[boolean] whether or not parentheses were used



8935
8936
8937
# File 'lib/syntax_tree/node.rb', line 8935

def parentheses
  @parentheses
end

#statement ⇒ Object (readonly)

[nil | untyped] the statement on which to operate



8932
8933
8934
# File 'lib/syntax_tree/node.rb', line 8932

def statement
  @statement
end

Instance Method Details

#accept(visitor) ⇒ Object



8947
8948
8949
# File 'lib/syntax_tree/node.rb', line 8947

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

#child_nodes ⇒ Object Also known as: deconstruct



8951
8952
8953
# File 'lib/syntax_tree/node.rb', line 8951

def child_nodes
  [statement]
end

#deconstruct_keys(_keys) ⇒ Object



8957
8958
8959
8960
8961
8962
8963
8964
# File 'lib/syntax_tree/node.rb', line 8957

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

#format(q) ⇒ Object



8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
# File 'lib/syntax_tree/node.rb', line 8966

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