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:) ⇒ Not

Returns a new instance of Not.



10912
10913
10914
10915
10916
10917
# File 'lib/syntax_tree/node.rb', line 10912

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



10910
10911
10912
# File 'lib/syntax_tree/node.rb', line 10910

def comments
  @comments
end

#parenthesesObject (readonly) Also known as: parentheses?

boolean

whether or not parentheses were used



10906
10907
10908
# File 'lib/syntax_tree/node.rb', line 10906

def parentheses
  @parentheses
end

#statementObject (readonly)

nil | untyped

the statement on which to operate



10903
10904
10905
# File 'lib/syntax_tree/node.rb', line 10903

def statement
  @statement
end

Instance Method Details

#===(other) ⇒ Object



10974
10975
10976
10977
# File 'lib/syntax_tree/node.rb', line 10974

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

#accept(visitor) ⇒ Object



10919
10920
10921
# File 'lib/syntax_tree/node.rb', line 10919

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

#child_nodesObject Also known as: deconstruct



10923
10924
10925
# File 'lib/syntax_tree/node.rb', line 10923

def child_nodes
  [statement]
end

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



10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
# File 'lib/syntax_tree/node.rb', line 10927

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



10941
10942
10943
10944
10945
10946
10947
10948
# File 'lib/syntax_tree/node.rb', line 10941

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

#format(q) ⇒ Object



10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
# File 'lib/syntax_tree/node.rb', line 10950

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