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.



8990
8991
8992
8993
8994
8995
# File 'lib/syntax_tree/node.rb', line 8990

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



8988
8989
8990
# File 'lib/syntax_tree/node.rb', line 8988

def comments
  @comments
end

#parenthesesObject (readonly)

boolean

whether or not parentheses were used



8985
8986
8987
# File 'lib/syntax_tree/node.rb', line 8985

def parentheses
  @parentheses
end

#statementObject (readonly)

nil | untyped

the statement on which to operate



8982
8983
8984
# File 'lib/syntax_tree/node.rb', line 8982

def statement
  @statement
end

Instance Method Details

#accept(visitor) ⇒ Object



8997
8998
8999
# File 'lib/syntax_tree/node.rb', line 8997

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

#child_nodesObject Also known as: deconstruct



9001
9002
9003
# File 'lib/syntax_tree/node.rb', line 9001

def child_nodes
  [statement]
end

#deconstruct_keys(_keys) ⇒ Object



9007
9008
9009
9010
9011
9012
9013
9014
# File 'lib/syntax_tree/node.rb', line 9007

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

#format(q) ⇒ Object



9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
# File 'lib/syntax_tree/node.rb', line 9016

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