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



10947
10948
10949
10950
10951
10952
# File 'lib/syntax_tree/node.rb', line 10947

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



10945
10946
10947
# File 'lib/syntax_tree/node.rb', line 10945

def comments
  @comments
end

#parenthesesObject (readonly) Also known as: parentheses?

boolean

whether or not parentheses were used



10941
10942
10943
# File 'lib/syntax_tree/node.rb', line 10941

def parentheses
  @parentheses
end

#statementObject (readonly)

nil | untyped

the statement on which to operate



10938
10939
10940
# File 'lib/syntax_tree/node.rb', line 10938

def statement
  @statement
end

Instance Method Details

#===(other) ⇒ Object



11009
11010
11011
11012
# File 'lib/syntax_tree/node.rb', line 11009

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

#accept(visitor) ⇒ Object



10954
10955
10956
# File 'lib/syntax_tree/node.rb', line 10954

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

#child_nodesObject Also known as: deconstruct



10958
10959
10960
# File 'lib/syntax_tree/node.rb', line 10958

def child_nodes
  [statement]
end

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



10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
# File 'lib/syntax_tree/node.rb', line 10962

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



10976
10977
10978
10979
10980
10981
10982
10983
# File 'lib/syntax_tree/node.rb', line 10976

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

#format(q) ⇒ Object



10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
# File 'lib/syntax_tree/node.rb', line 10985

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