Class: SyntaxTree::Defined

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

Defined represents the use of the defined? operator. It can be used with and without parentheses.

defined?(variable)

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(value:, location:, comments: []) ⇒ Defined

Returns a new instance of Defined.



3693
3694
3695
3696
3697
# File 'lib/syntax_tree/node.rb', line 3693

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3691
3692
3693
# File 'lib/syntax_tree/node.rb', line 3691

def comments
  @comments
end

#valueObject (readonly)

untyped

the value being sent to the keyword



3688
3689
3690
# File 'lib/syntax_tree/node.rb', line 3688

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



3699
3700
3701
# File 'lib/syntax_tree/node.rb', line 3699

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

#child_nodesObject Also known as: deconstruct



3703
3704
3705
# File 'lib/syntax_tree/node.rb', line 3703

def child_nodes
  [value]
end

#deconstruct_keys(_keys) ⇒ Object



3709
3710
3711
# File 'lib/syntax_tree/node.rb', line 3709

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

#format(q) ⇒ Object



3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
# File 'lib/syntax_tree/node.rb', line 3713

def format(q)
  q.text("defined?(")
  q.group do
    q.indent do
      q.breakable_empty
      q.format(value)
    end
    q.breakable_empty
  end
  q.text(")")
end